#!/bin/sh
#
# Version 1.06
# If you encounter ANY bugs please email them to: kate@oc3networks.com
#

SETDEV="eth1"
SETDEV=${1}

findgw() {
  if [ "${GW3}" = "" ]
  then
    echo "ERROR: Private Network not detected."
    exit 0
  fi
  if [ ${GW4} -lt 255 ]
  then
    GWG=193
  fi
  if [ ${GW4} -lt 192 ]
  then
    GWG=129
  fi
  if [ ${GW4} -lt 128 ]
  then
    GWG=65
  fi
  if [ ${GW4} -lt 64 ]
  then
    GWG=1
  fi
}

echo "Welcome to the Private Network configurator!"
echo ""
echo ""
echo "I am going to:"
echo "1. Configure your Private Network routing."
echo "2. Change some mirrors to go through the private network."
echo ""
echo ""
if [ -r /etc/rc.conf ]
then
  echo "I have detected your OS as: FreeBSD. If this isn't correct, hit Ctrl+C NOW!"
  echo "Waiting 10 seconds for before starting ..."
  sleep 10

  GW2=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $2}' | head -1`
  GW3=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $3}' | head -1`
  GW4=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $4}' | head -1`

  findgw

  LOCALGATEWAY="172.${GW2}.${GW3}.${GWG}"
  
  if [ "`grep ${LOCALGATEWAY} /etc/rc.conf`" ]
  then
    echo "Machine already configured for Private Network"
    exit 0
  fi
  echo "Private network gateway detected as: ${LOCALGATEWAY}"
  echo "static_routes=\"internalnetwork\"" >> /etc/rc.conf
  echo "route_internalnetwork=\"-net 172.16.0.0/12 ${LOCALGATEWAY}\"" >> /etc/rc.conf
  route add -net 172.16.0.0/12 ${LOCALGATEWAY}
  echo "Private Network configured. Done."
  exit 0
fi

if [ -r /etc/sysconfig/network ]
then
  echo "Your private network port to be configured is: ${SETDEV}."
  echo "You may override this setting by appending the run string with the interface name."
  echo "I have detected your OS as: CentOS/Redhat/Fedora based. If this isn't correct, hit Ctrl+C NOW!"
  echo "Waiting 10 seconds for before starting ..."
  sleep 10

  GW2=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $2}' | head -1`
  GW3=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $3}' | head -1`
  GW4=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $4}' | head -1`
  findgw

  LOCALGATEWAY="172.${GW2}.${GW3}.${GWG}"

  echo "Private network gateway detected as: ${LOCALGATEWAY}"
  echo "172.16.0.0/12 via ${LOCALGATEWAY}" > /etc/sysconfig/network-scripts/route-${SETDEV}
  ip route add 172.16.0.0/12 via ${LOCALGATEWAY}
  sed -i.bak 's/mirror.centos.org/repos.lax-noc.com/g' /etc/yum.repos.d/CentOS-Base.repo
  sed -i 's/mirrorlist.centos.org/mirrorlist.lax-noc.com/g' /etc/yum.repos.d/CentOS-Base.repo
  grep -q "local=" /etc/yum.repos.d/CentOS-Base.repo || sed -ri 's/(^mirrorlist.*)/\1\&local=1/g' /etc/yum.repos.d/CentOS-Base.repo
  yum -y clean all > /dev/null 2>&1
  echo "Private Network configured. Done."
  exit 0
fi

if [ -r /etc/network/interfaces ]
then
  echo "Your private network port to be configured is: ${SETDEV}."
  echo "You may override this setting by appending the run string with the interface name."
  echo "I have detected your OS as: Debian/Ubuntu based. If this isn't correct, hit Ctrl+C NOW!"
  echo "Waiting 10 seconds for before starting ..."
  sleep 10

  GW2=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $2}' | head -1`
  GW3=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $3}' | head -1`
  GW4=`ifconfig | awk '{print $2}' | grep -Eo '172.[1-3][0-9].[0-9]+\.[0-9]+' | awk -F. '{print $4}' | head -1`
  findgw

  LOCALGATEWAY="172.${GW2}.${GW3}.${GWG}"

  echo "Private network gateway detected as: ${LOCALGATEWAY}"
  route add -net 172.16.0.0/12 gw ${LOCALGATEWAY} dev ${SETDEV}
  sed -ri.bak 's/(.*http:\/\/)[-\.a-z0-9]+(.*)/\1repos.lax-noc.com\2/g' /etc/apt/sources.list
  apt-get -y clean
  if [ "`grep ${LOCALGATEWAY} /etc/network/interfaces`" ]
  then
    echo "Machine already configured for Private Network"
    exit 0
  fi
  echo "up route add -net 172.16.0.0/12 gw ${LOCALGATEWAY} dev ${SETDEV}" >> /etc/network/interfaces
  echo "Private Network configured. Done."
  exit 0
fi

echo "Done. You may now delete me."
