#!/bin/sh

#
# $Id: rmcmp,v 1.6.4.1 2005/08/25 17:40:02 akeenan Exp $
# $Log: rmcmp,v $
# Revision 1.6.4.1  2005/08/25 17:40:02  akeenan
# Changes for Linux Tux
#
# Revision 1.6  2002/02/28 18:39:36  jrumplik
# nge vps component to variable component
#
# Revision 1.5  2002/02/22 19:42:20  jrumplik
# vps to mps chars
#
# Revision 1.4  2000/07/18 18:00:22  miker
# Changed mvcmp to rmcmp for error messages
#
# Revision 1.3  2000/07/18 15:21:17  miker
# Added rmcmp to rel 6
#
# Revision 1.2  2000/03/03 20:56:18  miker
# *** empty log message ***
#
# Revision 1.1  2000/03/03 20:54:56  miker
# *** empty log message ***
#
# 

#-----------------------------------------------------------------------------

#
# This variable is for iterating through port numbers.
# It starts at 0 and is bumped each time we write out
# a new line in the cfg file.
#
portIncr=0


#
# A generic function which exits with a non-zero status if any unix command
# fails.  Basically, if any unix commandf fails, bail out.
#
terminate () {
	echo
	echo Operation failed, terminating
	exit 1
}


#
# See if vps numbers are even legal numeric values.
#
chkVpsNumbers () {
	if [ "$src" = "" -o "$src" -lt 1 -o "$src" -gt 899 ]; then
		echo source $comptp number $src is out of range
		terminate
	fi
}



#
# A function which actually does the move.
#
moveVps () {
# . /etc/vpsrc.sh
#	echo Updating release directory...
        echo '$VPSHOME'
        cd $VPSHOME
        \rm -rf $VPSHOME/$comptp$src
	cd .. || terminate
  
        echo Updating vpshosts file...
        /bin/echo "\tvhman -c$src -d -t${comptp}"
        vhman -c$src -d -t${comptp} || vhman_rc



}

vhman_rc () {
                        echo
                        echo "***************************************************"
                        echo "***************************************************"
                        echo "CORRECT HOSTNAME IN THE ABOVE VHMAN AND RERUN VHMAN"
                        echo "***************************************************"
                        echo "***************************************************"
                        echo
}

#
# If we're not root, forget it.
#
SysType=`uname -s`
if [ "$SysType" != "Linux" ]; then
	UserId=`/usr/ucb/whoami`
else
	UserId=`id -un`
fi

if [ "$UserId" != root ]; then
	echo Please run rmcmp as root
	exit 1
fi

#
# Load our environment (if available and not done already)
#
if [ "${VPSHOME:-undefined}" = undefined ]; then
	if [ -f /etc/vpsrc.sh ]; then
		. /etc/vpsrc.sh
	else
		echo \$VPSHOME not set.
		exit 1
	fi
fi

#
# Make sure srp isn't up!  That would be rude.
#
if [ "$comptp" != "oscar" ]; then
srpup
if [ $? != 0 ]; then
	echo Please take srp down before running rmcmp.
	exit 1
fi
fi

#
# Get any vps numbers that were specified.
#
case $# in
	0)
		src=none
		comptp=none
		;;
        1) 
                src=$1
                comptp=none
                ;;

        2)      
                src=$1
                comptp=$2
                ;;
	*)
		echo Invalid vps specification.
		echo usage: rmcmp src# component-type
		exit 1
		;;
esac

#
# Get any vps numbers that were not specified.
#
if [ "$src" = none ]; then
	/bin/echo "Please enter source component number: \c"
	read src
fi
if [ "$comptp" = none ]; then
		/bin/echo "Please enter component type: \c"
		read comptp
fi


#
# Validate vps numbers.  We check the destination several ways, if it is
# a local file in $VPSHOME, or if there is an entry for it in vpshosts.
#
#chkVpsNumbers
cd $VPSHOME
if [ ! -d $comptp$src ]; then
	echo Source $comptp$src does not exist.
	exit 1
fi



#
# We're done with sanity checks, now actually go do stuff that
# move the vps.
#
moveVps

echo Updating /etc/ethers...
FILE=/etc/ethers
BACKUP=/etc/ethers.bak

#Comment entries in /etc/ethers file

if [ -f ${BACKUP} ]; then
      /bin/rm -f ${BACKUP} > /dev/null
fi

/bin/mv ${FILE} ${BACKUP} > /dev/null

if [ -f ${BACKUP} ]; then

     exec 4< ${BACKUP}         # Read from the new backup file
     exec 5> ${FILE}           # Create a new /etc/ethers file

     while true
     do
             read entry <&4 || break

             if [ -z "$entry" ]; then
                     continue
             fi

             #
             # Check for the entry we want to comment out
             #

             if [ -n "`echo "$entry" | grep -w cps${src}`" ]; then
                   echo "#$entry" >&5

             else

                   echo "$entry" >&5
             fi
     done
fi

if [ "$SysType" != "Linux" ]; then
	PERICRONTAB=/var/spool/cron/crontabs/peri
	CRONBACKUP=/home/peri/pericron
else
	PERICRONTAB=/var/spool/cron/peri
	CRONBACKUP=${PERICRONTAB}~
fi


#
## Flag to indicate we need to retstart cron
#
restartCron=1

#
## Remove entries from Peri's crontab
#

echo "Modifying peri's crontab..."

if [ -f ${CRONBACKUP} ]; then 
      /bin/rm -f ${CRONBACKUP} > /dev/null
fi

/bin/mv ${PERICRONTAB} ${CRONBACKUP} > /dev/null

if [ -f ${CRONBACKUP} ]; then
       exec 4< ${CRONBACKUP}              #Read from the new backup file
       exec 5> ${PERICRONTAB}             #Create a new cron file

       while true
       do

              read entry <&4 || break
          
              if [ -z "$entry" ]; then
                      continue
              fi

              if [ -n "`echo "$entry" | grep -w $comptp${src}`" ]; then
                       continue
		else
                        echo "$entry" >&5
              fi
       done
fi

#
## Now restart cron 
#
if [ "$SysType" != "Linux" ]; then
    echo
    if [ `expr $restartCron \> 0` = 1 ] ; then
	echo "Restarting cron to initialize new crontab..."
	echo

	cronPid=`/usr/bin/ps -e | grep cron | grep -v grep | cut -c 1-6`

	if [ -z "$cronPid" ]; then
	    echo "Cron not running, restarting." 
              #Start cron with Unblocked SIGALRM
	    cron_all_sigs &
	else
	    kill $cronPid
              #
              ## Give cron a chance to clean up and remove it's FIFO
              #
	    sleep 5
	    echo "Starting up cron..."
	    cron_all_sigs&
	fi

      #
      ## Give cron a chance to get back up
      #
	sleep 5
	cronPid=`/usr/bin/ps -e | grep cron | grep -v grep | cut -c 1-6`
	echo "new cron pid: $cronPid"
    else
	echo "Peri's crontab not modified."
    fi
else
    if [ -f ${CRONBACKUP} ]; then 
	/bin/rm -f ${CRONBACKUP} > /dev/null
    fi
fi    
exit 0
