cs()  #Check Status of last run command and log it in the event log
{
    COMMAND=$1
    DESCRIPTION=$2
   echo " "
   echo "STARTING: $DESCRIPTION" | tee -a $eventlog
    `$COMMAND` >> /dev/tty2 2>&1
    exitstat=$?
      if [ $exitstat -eq 0 ] ; then
                echo -n `date +"%Y-%m-%d %H:%M:%S"`"=> " | tee -a $eventlog
                echo "COMPLETED: $DESCRIPTION (Exit status was good doing: $COMMAND)" | tee -a $eventlog
                echo " " | tee -a $eventlog
        else
                echo -n `date +"%H:%M:%S"`"=> " | tee -a $eventlog
                echo "FAILED: $DESCRIPTION (Exit status was bad doing: $COMMAND)" | tee -a $eventlog
                echo "Cannot continue safely, aborting" | tee -a $eventlog
                echo " " | tee -a $eventlog
                exit;
        fi
}

log()   #Log a message in event log without executing a command
{

    DESCRIPTION=$1 
    echo -n `date +"%H:%M:%S"`"=> " | tee -a $eventlog
    echo "INFO: $DESCRIPTION" | tee -a $eventlog
}

init_logs()  #Initialize log files/delete old ones if needed.
{

        echo "* $hostname LSC Datagrid Installation Log:  created on" `date` > $DGL

        if [ -e $lf ]
        then
                rm -f $lf
                echo "---------------------------------------------------------------------" | tee -a $eventlog
                echo "    "`date +"%Y-%m-%d %H:%M:%S"` "Starting postinstall script for $hostname " | tee -a $eventlog
                echo "---------------------------------------------------------------------" | tee -a $eventlog
                log "Found and Removed old Log File"
        else
                echo "-----------------------------------------------------------------------" | tee -a $eventlog
                echo "    "`date +"%Y-%m-%d %H:%M:%S"` "Starting postinstall script for $hostname " | tee -a $eventlog
                echo "-----------------------------------------------------------------------" | tee -a $eventlog
        fi

echo "* GC Post Install General Event Log:  created on" `date` > $eventlog
echo "* GC Post Install rpm output Log: created on" `date` > $rpm_output_log
echo "* GC Post Install rpm and config  download log:  created on" `date` > $dloadlog
echo "* GC Post Install Auto-Update output Log: Created on" `date` > $auto_update_log
echo "* GC Post Install General Error Log:  created on" `date` > $errorlog
echo "* GC Post Install LSC Datagrid Installation Log:  created on" `date` > $Dgridlog


}
do_firewall()   #Set up the firewall rules
{

	log "Firewall currently disabled"
        #Comment these back in eventually
        # set firewall rules
        #iptables -P INPUT DROP
        #iptables -P FORWARD DROP
        #iptables -A INPUT -s 127.0.0.0/255.0.0.0 -j ACCEPT
        #iptables -A INPUT -s 129.89.57.0/255.255.255.0 -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port   ssh -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port   smtp -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port   53 -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port   pop3 -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port  www -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port ntp -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port 5190 -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port smtps -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port https -j ACCEPT
        #iptables -A INPUT -s 0/0  -p TCP --destination-port http -j ACCEPT
        #iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

        # save firewall rules
        #/etc/rc.d/init.d/iptables save


        #chkconfig iptables on

}

mount_share()
{
log "Mounting install share"
mkdir $BD
mount 129.89.57.123:/export1/fc3/gc $BD
}

umount_share()
{
log "Unmounting install share"
cd /
cs "umount $BD" "trying to unmount"
cs "rmdir $BD" "trying to delete mount point"
}

copy_configs()
{

   dialog --backtitle "UWMLSC GC post-install for Fedora Core 3" --yesno "Is
           this a standard GC machine which will always be on the UWM Physics subnet?
           \n\nAnswer \"No\" if your machine is a laptop which will be used outside
           of UWM." 20 60
           sel=$?

           if [ $sel -eq 0 ] ;
           then
		log "User elected to install GC config files"
		cs "cp -rf $CONFDIR/* /" "Installing General Computing Custom Config Files"
		log "Changing  root mail alias to gc@gravity.phys.uwm.edu"
		  sed 's/^.*root:.*$/\n/' /etc/aliases > /tmp/aliases.foo.bar
	          echo "root:    gc@gravity.phys.uwm.edu" >> /tmp/aliases.foo.bar
	          mv -f  /tmp/aliases.foo.bar /etc/aliases 
	 	  /usr/bin/newaliases
		 #Remove CVS directories from config file upgrades
		 log "finding CVS dirs and removing--this takes a LOOOOONG time"
		 cs "rm -rf `find / -xdev | grep CVS`" "Removing CVS directories from config upgrade"
	 else
             log "User said DO NOT install GC config files, so we wont"
             laptop=1
         fi


}

install_rpms()
{
#Make sure these are in place before we install rpms, since some may
#need to modify them.
#cs "cp -f $CONFDIR/etc/passwd /etc" "Copying Password File"
#cs "cp -f $CONFDIR/etc/shadow /etc" "Copying Shadow File"
#cs "cp -f $CONFDIR/etc/group /etc"  "Copying Group File"

#cs "rpm -Uvh --nodeps --force $RPMDIR/*rpm" "Installing Custom RPMS"
rpm -Uvh --nodeps --force $RPMDIR/*rpm
}

install_splash_screen()
{
 clear
	log "Installing Custom Gnome Splash Screen"

        if [ -e /tmp/LSC_splash.png ] ;
        then

                log "Found existing splash screen in /tmp, deleting and replacing with new one"
                rm -f /tmp/LSC_splash.png
        fi
	#if this script gets run twice we the real original one gets overwritten..no big deal
        mv /usr/share/pixmaps/splash/gnome-splash.png /usr/share/pixmaps/splash/gnome-splash.png.orig
        cs "cp -a $EXTRASDIR/LSC_splash.png  /usr/share/pixmaps/splash/gnome-splash.png" "Copying UWMLSC splash screen"

}


fix_permissions()
{
        log "Changing permissions of files in /etc/profile.d"
        chmod +x /etc/profile.d/*.sh /etc/profile.d/*.csh 

}

install_printing()
{
 log "Setting up printing with Bogus redhat tool"
        /usr/sbin/printconf-tui --Ximport < /root/printer_settings.xml
        rm /root/printer_settings.xml
}
setup_automount()
{
        log "     --Setting up Auto Mount"
        rm -f /etc/auto.misc
        rm -rf /usr/local
        rm -rf /var/spool/mail
}


make_symlinks()
{
log  "     --Checking for existing symlinks..."
        if [ -h /usr/local ] ;
         then
          log "          Found symlink to /usr/local....REMOVING" 
          rm -f /usr/local >>$errorlog 2>&1
        fi
        if [ -h /ldcg ] ;
          then
           log "         Found symlink to /ldcg...REMOVING"
           rm -f /ldcg >>$errorlog 2>&1
        fi
        if [ -h /ldas ] ;
          then
           log "         Found symlink to /ldas...REMOVING" 
            rm -f /ldas >>$errorlog 2>&1
        fi
        if [ -h /ldas_outgoing ] ;
           then
            log"         Found symlink to /ldas_outgoing...REMOVING"
            rm -f /ldas_outgoing >>$errorlog 2>&1
        fi
        if [ -h /data ] ;
          then
           log "         Found symlink to /data...REMOVING"
           rm -f /data >>$errorlog 2>&1
        fi
        if [ -h /var/spool/mail ] ;
          then
           log "         Found symlink to /var/spool/mail...REMOVING"
           rm -f /var/spool/mail >>$errorlog 2>&1
        fi
        if [ -h /scratch ] ;
          then
           log "         Found symlink to /scratch...REMOVING"
           rm -f /scratch >>$errorlog 2>&1
        fi

        if [ -h /scratch2 ] ;
          then
           log "         Found symlink to /scratch2...REMOVING"
           rm -f /scratch2 >>$errorlog 2>&1
        fi
        if [ -h /scratch3 ] ;
          then
           log "         Found symlink to /scratch3...REMOVING"
           rm -f /scratch3 >>$errorlog 2>&1
        fi
        if [ -h /scratch4 ] ;
          then
           log "         Found symlink to /scratch4...REMOVING"
           rm -f /scratch4 >>$errorlog 2>&1
        fi

        mkdir /etc/grid-security
        log "     --Creating new symlinks"
        ln -sf /mnt/usr_local /usr/local
        ln -sf /mnt/ldcg /ldcg 
        ln -sf /mnt/ldas /ldas 
        ln -sf /mnt/ldas_outgoing /ldas_outgoing 
        ln -sf /mnt/data /data 
        ln -sf /mnt/mail /var/spool/mail 
        ln -sf /mnt/scratch /scratch
        ln -sf /mnt/scratch2 /scratch2
        ln -sf /mnt/scratch3 /scratch3 
        ln -sf /mnt/scratch4 /scratch4 
        ln -sf /home/globus/grid-mapfile /etc/grid-security/grid-mapfile 

}

do_yum_update()
{

   if [ $laptop -eq 1 ] ;
        then
         log "******* SKIPPING Yum-UPDATE  STAGE BECAUSE this is a laptop ********"
        else

                
                log "-=-=-=-=-------Entering Yum Update Stage------=-=-=-=-"

                dialog --backtitle "UWMLSC GC post-install for Fedora Core 3" --yesno "We Strongly 
                recommend you run Yum update to get security updates. This will take several
                minutes, however you can switch to a different virtual console or X terminal
                to do other tasks while the update is running.
                \n\nWould you like to run yum now? " 20 60
                sel=$?
                clear
                if [ $sel -eq 0  ] ;
                then 
                        log "     --User Elected to Run Yum Update Now"
                        log "     --Running yum-update"
                        rpm --import /usr/share/rhn/RPM-GPG-KEY-fedora
			/usr/bin/yum update yum 
			/usr/bin/yum -y update 
			/usr/bin/yum groupinstall lscsoft
                else
                        log "     --User said DO NOT run yum update now, so we didnt"
                fi
                log  "-=-=-=-=-------Exiting Yum Update Stage------=-=-=-=-"
        fi

}

install_LDG3() 
{
# This installs the full LSC DataGrid Server 3.5.0.
# This function is designed to be used on UWM LSC-group General Computing machines (Could be called from the post-install or stand-alone).
# See http://www.lsc-group.phys.uwm.edu/lscdatagrid/doc/installserver.html for details. 

echo "--Downloading and copying pacman:" | tee -a $DGL
                if [ -e /tmp/pacman-3.12.1.tar.gz ] ;
                then
                        echo "--Found existing pacman file in /tmp, deleting and replacing with new one" | tee -a $DGL
                        rm -rf /tmp/pacman-3.12.1*  >> $DGL 2>&1
                fi	
#wget -nv -a $DGL --cut-dirs=5 -nH -P /tmp/ http://www.lsc-group.phys.uwm.edu/computing/external/system/linux/extras_9/pacman-2.121.tar.gz
 cp $EXTRASDIR/pacman-3.12.1.tar.gz /tmp
  cd  /tmp
  tar zxf pacman-3.12.1.tar.gz >>$DGL 2>&1
  cd pacman-3.12.1
  source setup.sh >> $DGL 2>&1

  cd /opt  >> $DGL 2>&1
  if [ ! -e ldg-3.5 ] ;
                then
                 mkdir ldg-3.5 >> $DGL 2>&1
                else
                echo "--ldg-3.5 directory already exists, not making a new one" | tee -a $DGL
                fi
  		cd /opt/ldg-3.5  >> $DGL 2>&1
  		echo "--Telling pacman where to find LSC DataGrid Server" | tee -a $DGL
                printf "%s\n%s\n" "y" "y" > /tmp/cache-respons
		pacman -cache:http://www.lsc-group.phys.uwm.edu/LSC-DataGrid-Server < /tmp/cache-respons
  printf "%s\n" "y" "y" > /tmp/pacman-response
  echo "Using pacman to get LSC DataGrid Server" | tee -a $DGL
  pacman -get http://www.lsc-group.phys.uwm.edu/LSC-DataGrid-Server:3.5 < /tmp/pacman-response | tee -a $DGL
  sleep 2
                echo "--LSC Datagrid server installation completed" | tee -a $DGL
                echo "--Installing GSI enabled OpenSSH" | tee -a $DGL
		/sbin/chkconfig --del sshd  >> $DGL 2>&1
                cp /etc/init.d/sshd /etc/init.d/sshd.bak  >> $DGL 2>&1
                cp /opt/ldg-3.5/globus/sbin/SXXsshd /etc/init.d/sshd  >> $DGL 2>&1
                /etc/init.d/sshd start  >> $DGL 2>&1
                /sbin/chkconfig --add sshd  >> $DGL 2>&1
                echo "--Appending Location variables to /etc/profile:" | tee -a $DGL
		echo "export LSC_DATAGRID_SERVER_LOCATION=/opt/ldg-3.5" >> /etc/profile
                echo ")xxx{:::::::> DONT FORGET TO INSTALL THE SSH KEYS <:::::::}xxx(" | tee -a $DGL

}






