#!/bin/sh

#script to change ips on a VestaCP server.
#usage:
# $0 <oldip> <newip>

#########################
# Configuration Options #
#########################

HAVE_APACHE2=1;
HAVE_HTTPD=0;
HAVE_NGINX=1;
VERBOSE=0;

# DON'T CHANGE ANYTHING BELOW THIS LINE
# -------------------------------------

clear;

LOG=/var/log/vesta/system.log;

MYUID=`/usr/bin/id -u`;
if [ "$MYUID" != 0 ]; then
        echo "You require Root Access to run this script";
        exit 0;
fi;

if [ $# != 2 ] && [ $# != 3 ]; then
        echo "Usage:";
        echo "$0 <oldip> <newip> [<file>]";
        echo "you gave #$#: $0 $1 $2 $3";
        exit 0;
fi;

echo "";
echo "######################################";
echo "# Changing VestaCP Server IP Address #";
echo "######################################";
echo "";
sleep 5;

OLD_IP=$1;
NEW_IP=$2;

if [ "${VERBOSE}" -eq 1 ]; then
   echo "Old IP: $OLD_IP";
        echo "New IP: $NEW_IP";
        echo "";

        sleep 5;
fi;

DATE=`date '+%F %X'`;
BIN=`echo $0 | awk -F/ '{print $NF}'`;

log()
{
        echo -e "$1";
        echo -e "$1" >> $LOG;

   if [ "${VERBOSE}" -eq 1 ]; then
                echo "";
        fi;
}

swapfile()
{
   if [ "${VERBOSE}" -eq 1 ]; then
      echo "Replacing Old IP with New IP in $1...";
           sleep 5;
   fi;

        if [ ! -e $1 ]; then
                log "Cannot Find $1 to change the IPs. Skipping...";
                return;
        fi;

        TEMP="perl -pi -e 's/${OLD_IP}/${NEW_IP}/g' $1";
        eval $TEMP;

        log "$DATE $BIN $1\t: $OLD_IP -> $NEW_IP";
}

if [ $# = 3 ]; then
        swapfile $3;
        exit 0;
fi;

IPFILE_OLD=/usr/local/vesta/data/ips/$OLD_IP;
IPFILE_NEW=/usr/local/vesta/data/ips/$NEW_IP;

if [ ! -e $IPFILE_OLD ]; then
        echo -n "$IPFILE_OLD does not exist.  Do you want to continue anyway? (y/n) : ";
        read YESNO;

        echo "";

        if [ "$YESNO" != "y" ]; then
                exit 0;
        fi;
else
        mv -f $IPFILE_OLD $IPFILE_NEW;
        log "$DATE $0 $IPFILE_OLD\t: $OLD_IP -> $NEW_IP";
fi;

if [ "${HAVE_APACHE2}" -eq 1 ]; then
        if [ -e /etc/apache2/conf.d/${OLD_IP}.conf ]; then
      swapfile /etc/apache2/conf.d/${OLD_IP}.conf;
                mv -f /etc/apache2/conf.d/$OLD_IP.conf /etc/apache2/conf.d/${NEW_IP}.conf;
        fi;
fi

if [ "${HAVE_HTTPD}" -eq 1 ]; then
        if [ -e /etc/httpd/conf.d/${OLD_IP}.conf ]; then
                swapfile /etc/httpd/conf.d/${OLD_IP}.conf;
                mv -f /etc/httpd/conf.d/$OLD_IP.conf /etc/httpd/conf.d/${NEW_IP}.conf;
        fi;

        swapfile /etc/httpd/conf.d/mod_extract_forwarded.conf;
fi;

if [ "${HAVE_NGINX}" -eq 1 ]; then
        if [ -e /etc/nginx/conf.d/${OLD_IP}.conf ]; then
                swapfile /etc/nginx/conf.d/${OLD_IP}.conf;
                mv -f /etc/nginx/conf.d/$OLD_IP.conf /etc/nginx/conf.d/${NEW_IP}.conf;
        fi;
fi;

swapfile /etc/hosts;

ULDDU=/usr/local/vesta/data/users;

for i in `ls $ULDDU`; do
{

        if [ ! -d $ULDDU/$i ]; then
                continue;
        fi;

        swapfile $ULDDU/$i/web.conf;
        swapfile $ULDDU/$i/dns.conf;

        if [ `find $ULDDU/$i/dns/ -type f | wc -l` -gt 0 ]; then
                for j in `ls $ULDDU/$i/dns/*.conf`; do
                {
                        swapfile $j;
                };
                done;

                for j in `ls /home/$i/conf/dns/*.db`; do
                {
                        swapfile $j;
                };
                done;
        fi;

        if [ "${HAVE_APACHE2}" -eq 1 ]; then
                swapfile /home/$i/conf/web/apache2.conf;
        fi;

        if [ "${HAVE_HTTPD}" -eq 1 ]; then
                swapfile /home/$i/conf/web/httpd.conf;
        fi;

        if [ "${HAVE_NGINX}" -eq 1 ]; then
                swapfile /home/$i/conf/web/nginx.conf;
        fi;

};
done;

#this is needed to update the serial in the db files.
if [ "${HAVE_APACHE2}" -eq 1 ]; then
   if [ "${VERBOSE}" -eq 1 ]; then
      echo "Restarting service: apache2...";
   fi;

        service apache2 restart;

   if [ "${VERBOSE}" -eq 1 ]; then
      echo " done.";
      echo "";
   fi;
fi;

if [ "${HAVE_HTTPD}" -eq 1 ]; then
   if [ "${VERBOSE}" -eq 1 ]; then
      echo "Restarting service: httpd...";
   fi;

        service httpd restart;

   if [ "${VERBOSE}" -eq 1 ]; then
      echo " done.";
      echo "";
   fi;
fi;

if [ "${HAVE_NGINX}" -eq 1 ]; then
   if [ "${VERBOSE}" -eq 1 ]; then
      echo "Restarting service: nginx...";
   fi;

        service nginx restart;

   if [ "${VERBOSE}" -eq 1 ]; then
      echo " done.";
      echo "";
   fi;
fi;

if [ "${VERBOSE}" -eq 0 ]; then
        echo "";
fi;

echo "*** Done swapping $OLD_IP to $NEW_IP ***";
echo "";