#!/bin/sh
# Copyright 2005-11 NoZone, Inc.
#
# This script will update your network configuration to make it use
# a static IP instead of a dynamic IP address based on the currently
# obtained IP information.
# 
# Currently supported OSes:
#  * Any Red Hat Based OS
#  * Any Debian Based OS

DATE=`date +%s`;

function netmask() {
  SLASH=$1
  if (( $SLASH < 8 )); then
    A=$(( 256 - (2 ** (8 - $SLASH)) ));
  else
    A=255;
  fi
  if (( $SLASH < 16 )); then
    B=$(( 256 - (2 ** (16 - $SLASH)) ));
  else
    B=255;
  fi
  if (( $SLASH < 24 )); then
    C=$(( 256 - (2 ** (24 - $SLASH)) ));
  else
    C=255;
  fi
  D=$(( 256 - (2 ** (32 - $SLASH)) ));
  export NETMASK=$A.$B.$C.$D
}

# Detect the Linux Distribution by RPM or Deb Package Version
if [ -e "/usr/bin/rpmquery" ] ; then

  # RPM-based Linux
  RELEASE=`rpmquery fedora-release redhat-release centos-release | grep -v 'not installed'`
  OS=`echo ${RELEASE} | awk -F- "{ print \\$1; }"`;
  VER=`echo ${RELEASE} | awk -F- "{ print \\$3; }"`;
  if [[ ${OS} == "" ]] ; then
    OS='rpm-based-unknown'
  fi

elif [ -e "/usr/bin/dpkg-query" ] ; then

  # Deb-based Linux
  RELEASE=`dpkg-query -W base-files | awk "{ print \\$2; }" | grep -v 'No packages found'`
  DISTRO=`cat /etc/issue | awk "{ print \\$1; }"`
  if [[ ${DISTRO} == "Debian" && ${RELEASE} != "" ]] ; then
    VER=`echo ${RELEASE} | awk -F. "{ if ( \\$2 == \"\" ) { \\$2 = 0; } print \\$1\".\"\\$2; }"`
    OS='debian';
  else
    OS='deb-based-unknown'
  fi

fi

# If no match is found, OS is completely unrecognized
if [[ ${OS} == "" ]] ; then
  OS='unknown'
fi

if [ -e '/sbin/ip' ] ; then

  if [[ $1 == "" ]] ; then
    DEVICE=`ip r l | grep default | cut -d' ' -f5`
    if [[ ${DEVICE} == "" ]] ; then    
      DEVICE=eth0
    fi
  else
   DEVICE=$1
  fi

  echo Staticizing ${DEVICE}...

  TEST=`ip link show ${DEVICE} 2>&1 | grep "not exist"`
  if [[ ${TEST} != "" ]] ; then
    echo " * Error: Device ${DEVICE} does not exist!";
    exit 1;
  fi

  GATEWAY=`ip route show dev ${DEVICE} scope global | tr " " "\n" | egrep "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"`

  CIDR=`ip addr show dev ${DEVICE} | grep 'inet ' | awk '{ print $2; }'`
  IPADDR=`echo ${CIDR} | awk -F/ '{ print $1; }'`
  SLASH=`echo ${CIDR} | awk -F/ '{ print $2; }'`

  netmask ${SLASH};  
else
  echo " * Error: Please install the iproute package!";
  exit 1;
fi

if [[ ${OS} == "centos" || ${OS} == "fedora" || ${OS} == "redhat" ]] ; then

  echo " * This is a Red Hat system layout (${OS}/${VER})...";

  echo " * Adding the default gateway (${GATEWAY})..."
  cd /etc/sysconfig
  mv network network.staticize.${DATE}
  grep -v GATEWAY network.staticize.${DATE} > network
  echo "GATEWAY=${GATEWAY}" >> network

  echo " * Adding the ip address (${IPADDR}) and netmask (${NETMASK})... "
  cd /etc/sysconfig/network-scripts
  mv ifcfg-${DEVICE} staticize-ifcfg-${DEVICE}.${DATE}
  grep -e '\(DEVICE\|TYPE\|ONBOOT\|HWADDR\|IPV6INIT\|IPV6ADDR\|IPV6_DEFAULTGW\)' staticize-ifcfg-${DEVICE}.${DATE} > ifcfg-${DEVICE}
  echo "BOOTPROTO=static" >> ifcfg-${DEVICE}
  echo "IPADDR=${IPADDR}" >> ifcfg-${DEVICE}
  echo "NETMASK=${NETMASK}" >> ifcfg-${DEVICE}

  echo " * Rewriting /etc/resolv.conf..."
  mv /etc/resolv.conf /etc/resolv.staticize.${DATE}
  touch /etc/resolv.conf
  echo "nameserver 216.86.146.8" >> /etc/resolv.conf
  echo "nameserver 216.86.146.9" >> /etc/resolv.conf
  echo "options rotate timeout:1" >> /etc/resolv.conf
  
  echo " * Restarting network service to complete configuration..."
  service network restart;

  echo " * Complete! ";

elif [[ ${OS} == "debian" ]] ; then

  echo " * This is a Debian system layout (${OS}/${VER})...";

  echo " * Adding the settings (${IPADDR}; gw: ${GATEWAY}; nm: ${NETMASK})..."
  cd /etc/network
  cp interfaces interfaces.staticize.${DATE}
  sed -i "s/allow-hotplug ${DEVICE}/auto ${DEVICE}/" interfaces
  sed -i "s/iface ${DEVICE} inet dhcp/\
iface ${DEVICE} inet static\
\n	address ${IPADDR}\
\n	netmask ${NETMASK}\
\n	gateway ${GATEWAY}\
\n	dns-nameservers 216.86.146.8 216.86.146.9\
\n/" interfaces

  echo " * Rewriting /etc/resolv.conf..."
  mv /etc/resolv.conf /etc/resolv.staticize.${DATE}
  touch /etc/resolv.conf
  echo "nameserver 216.86.146.8" >> /etc/resolv.conf
  echo "nameserver 216.86.146.9" >> /etc/resolv.conf

  echo " * Restarting network service to complete configuration..."
  /etc/init.d/networking restart

  echo " * Complete! ";

else

  echo " * This is ${OS} ${VER}...";
  echo " * Error: Unsupported OS: ${OS}";

fi