#!/bin/sh
#
# "SystemImager" - Copyright (C) 1999-2001 Brian Elliott Finley <brian@systemimager.org>
#   Others who have contributed to this code (in alphabetical order):
#     dann frazier <dannf@ldl.fc.hp.com>
#     Michael R. Nolta <mrnolta@princeton.edu>
#
#
# This file is: installserver
#   Interactively installs the SystemImager server files, and takes care of 
#   upgrade issues.

# set some variables
DESTDIR=""
COMMAND_PATH=`dirname $0`
test "x$COMMAND_PATH" != "x" && cd $COMMAND_PATH

if [ ! -f ./install_lib ]; then
  echo "Couldn't find ./install_lib, exiting."
  exit 1
fi  
. ./install_lib

### BEGIN introduction ###
# you sure you want to install?
echo -n "Install SystemImager server? (y/[n]) "
read REPLY
case $REPLY in
  y|Y|Yes|yes|YES ) clear; echo Ok.  Installing SystemImager server... ;;
                * ) echo Install cancelled.  No files installed. && exit 1 ;;
esac

echo
sleep 2
### END introduction ###

### BEGIN clean up old files ###
echo "Checking for old utilities..."

OLD_FILES="/usr/sbin/addclients /usr/sbin/cpimage /usr/sbin/getimage /usr/sbin/makedhcpserver /usr/sbin/makedhcpstatic /usr/sbin/makeautoinstallcd /usr/sbin/makeautoinstalldiskette /usr/sbin/mvimage /usr/sbin/pushupdate /usr/sbin/rmimage /usr/sbin/lsimage"
remove_old_files

CONFIG_FILES="/etc/systemimager/systemimager.conf:./etc/systemimager/systemimager.conf /etc/systemimager/rsyncd.conf:./etc/systemimager/rsyncd.conf /etc/init.d/systemimager:./etc/init.d/systemimager"
upgrade_config_files
### END clean up old files ###

### BEGIN update log files ###
if [ -f /var/log/systemimager ]; then
    echo "You appear to be using the old style /var/log/systemimager file."
    echo "I will automatically update your system to use /var/log/systemimager/rsyncd instead."
    echo "Press <Enter> to continue..."
    read
    echo -n "Moving /var/log/systemimager to /var/log/systemimager/rsyncd... "
    mv /var/log/systemimager /var/log/systemimager.beforesystemimager$VERSION
    mkdir /var/log/systemimager
    mv /var/log/systemimager.beforesystemimager$VERSION /var/log/systemimager/rsyncd
    echo "done."
fi
### END update log files ###

### BEGIN adjust directories to match system ###
if [ ! -d /etc/init.d ]; then
  if [ ! -d /etc/rc.d/init.d ]; then
    echo "Couldn't find init.d directory."
    exit 1
  else
    echo "Moving ./etc/init.d ./etc/rc.d/init.d, to match your system..."
    mkdir ./etc/rc.d
    mv ./etc/init.d ./etc/rc.d
  fi
fi

# keep a doc directory for each version - this is just easier than keeping
# a database of all the files that would need to be removed to insure
# no out-of-date documents.
cp -a ./usr/local/share/doc/systemimager-doc \
  ./usr/local/share/doc/systemimager-$VERSION

### END adjust directories to match system ###
  
### BEGIN install files ###
echo "Installing..."
find . -type d -mindepth 2 -exec install -d {} $DESTDIR/{} \;
find . -type f -mindepth 2 -exec install -D {} $DESTDIR/{} \;
if [ $? -ne 0 ]; then
    echo "Install failed."
    exit 1
fi
### END install files ###

### BEGIN copy over existing rsyncd.conf entries ###
echo "Looking for existing rsyncd.conf files..."

for file in /etc/rsyncd.conf /etc/systemimager/rsyncd.conf.beforesystemimager$VERSION; do
  if [ -f $file ]; then
    echo "SystemImager now uses /etc/systemimager/rsyncd.conf instead of /etc/rsyncd.conf"
    echo
    delim
    echo "WARNING:  $file was found on your system."
    delim
    echo
    echo "Shall I copy any image entries from $file to "
    echo -n "/etc/systemimager/rsyncd.conf? ([y]/n) "
    read REPLY
    case $REPLY in
      n|N|no|NO|No ) echo "Not adding old entries, as requested." ;;
      * ) start_copying="false"
          while /bin/true; do
            read line
            ## check if input is exhausted
            if [ $? -ne 0 ]; then
              break
            fi
            if [ "$start_copying" == "true" ]; then
              ## check to see if this is a module section header
              echo "$line" | grep "^\[.*\]$" > /dev/null
              if [ $? -eq 0 ]; then
                line_length=$(echo $line | wc | cut -c 16-23)
                image=$(echo $line | cut -c 2-$(expr $line_length - 2))
                ## check to see if this module section header already exists
                grep "\[$image\]" /etc/systemimager/rsyncd.conf > /dev/null
                if [ $? -eq 0 ]; then
                  echo "An entry for image $image already exists, skipping..."
                  ## skip past the next 2 lines
                  read line
                  read line
                  continue
                fi
              fi
              echo "$line" >> /etc/systemimager/rsyncd.conf
              continue
            else
              echo "$line" | \
                grep "^# only image entries below this line" > /dev/null
              if [ $? -eq 0 ]; then
                start_copying="true"
              fi
            fi
          done < $file
          if [ "$start_copying" == "false" ]; then
            delim
            echo "WARNING:  $file doesn't look like it was created by SystemImager."
            delim
            echo "No image entries were copied over."
            echo "Press <Enter> to continue..."
            read
          fi
    esac
  fi
done
### END copy over existing rsyncd.conf entries ###

### if 
if [ -f /etc/rsyncd.conf ]; then
  ## if it's not our rsyncd.conf, leave it alone.
  grep "^# only image entries below this line" < /etc/rsyncd.conf > /dev/null
  if [ $? -ne 0 ]; then
    break
  fi
  delim
  echo "Warning:  /etc/rsyncd.conf is no longer used by SystemImager"
  delim
  echo "Moving /etc/rsyncd.conf to /etc/rsyncd.conf-no-longer-used-by-systemimager..."
  mv /etc/rsyncd.conf /etc/rsyncd.conf-no-longer-used-by-systemimager
  if [ $? -ne 0 ]; then
    echo "Move failed."
    exit 1
  fi
fi

if [ -d /tftpboot/systemimager ]; then
  # warn about the relocation of the tftpboot stuff
  delim
  echo "NOTE: The files that were stored in /tftpboot in previous releases have moved."
  echo "Autoinstall scripts have moved to /var/lib/systemimager/scripts, and"
  echo "most everything else is now in /usr/local/share/systemimager/<arch>-boot"
  delim
  echo "Press <Enter> to continue..."
  read
  echo "Shall I copy any master scripts/links you have over to the new"
  echo -n "location? ([y]/n) "
  read REPLY
  case $REPLY in
    n|N|No|NO ) echo "Ok, not copying over master scripts." ;;
            * ) echo "Copying scripts..."
	        cp -af /tftpboot/systemimager/*.master /var/lib/systemimager/scripts
	        cp -af /tftpboot/systemimager/*.sh /var/lib/systemimager/scripts
		;;
  esac
fi
		
### BEGIN init scripts ###
# find where init.d lives
[ -d /etc/rc.d/init.d ] && INITDIR_PREFIX="/etc/rc.d"
[ -d /etc/init.d ] && INITDIR_PREFIX="/etc"
if [ "$INITDIR_PREFIX" == "" ]; then
  echo "Couldn't find the directory where initscripts are stored."
  exit 1
fi

if [ -f $INITDIR_PREFIX/init.d/rsync ]; then
  delim
  echo "WARNING:  $INITDIR_PREFIX/init.d/rsync exists."
  delim
  echo "SystemImager now uses $INITDIR_PREFIX/init.d/systemimager instead."
  echo "I can go ahead and remove the symlinks to $INITDIR_PREFIX/init.d/rsync from the various"
  echo "runlevels.  This will prevent a conflict between the new and old initscripts."
  echo "Shall I remove the symlinks to /etc/init.d/rsync from the various"
  echo -n "runlevels? ([y]/n) "
  read REPLY
  case $REPLY in
    n|N|No|no|NO ) echo "Ok - not removing symlinks to $INITDIR_PREFIX/init.d/rsync." ;;
               * ) echo -n "Removing symlinks to $INITDIR_PREFIX/init.d/rsync..."
	           find $INITDIR_PREFIX -type l -name "[K,S]20rsync" \
	             -exec rm -f {} \;
		   echo "done."
		   echo -n "Should I go ahead and remove $INITDIR_PREFIX/init.d/rsync too? ([y]/n) "
		   read REPLY
		   case $REPLY in
		     n|N|No|no|NO ) echo "Ok - not removing $INITDIR_PREFIX/init.d/rsync" ;;
		                * ) echo "Removing $INITDIR_PREFIX/init.d/rsync..."
				    rm -f $INITDIR_PREFIX/init.d/rsync
				    ;;
		   esac
		   ;;
  esac
fi		   

### BEGIN create softlinks to initscript ###
# create soft links to rsync init script
echo "Creating soft links to systemimager init script..."

if [ -f $INITDIR_PREFIX/init.d/systemimager ]; then
  cd $INITDIR_PREFIX/rc0.d; ln -sf ../init.d/systemimager K20systemimager || exit 1
  cd $INITDIR_PREFIX/rc1.d; ln -sf ../init.d/systemimager K20systemimager || exit 1
  cd $INITDIR_PREFIX/rc2.d; ln -sf ../init.d/systemimager S20systemimager || exit 1
  cd $INITDIR_PREFIX/rc3.d; ln -sf ../init.d/systemimager S20systemimager || exit 1
  cd $INITDIR_PREFIX/rc4.d; ln -sf ../init.d/systemimager S20systemimager || exit 1
  cd $INITDIR_PREFIX/rc5.d; ln -sf ../init.d/systemimager S20systemimager || exit 1
  cd $INITDIR_PREFIX/rc6.d; ln -sf ../init.d/systemimager K20systemimager || exit 1
else
  echo "$INITDIR_PREFIX/init.d/systemimager not found, symlinks not created."
fi

### END init scripts ###

### BEGIN inetd stuff ###

# now that we run rsync in standalone mode, and mkbootserver
# exists,  i don't think we still need to mess w/ /etc/services
# or inetd.conf

### END inetd stuff ###

echo "Running initscript to start rsync daemon..."
$INITDIR_PREFIX/init.d/systemimager restart
if [ $? -ne 0 ]; then
  echo "Error:  The initscript returned a non-zero status"
  exit 1
fi  

echo ""
delim
echo "Note:  SystemImager now uses the AppConfig perl module.  This module is"
echo "available at:"
echo "  http://www.perl.com/CPAN/authors/id/ABW/AppConfig-1.52.tar.gz"
echo "and must be installed before using the SystemImager utilities."
echo "AppConfig is supplied by the libappconfig-perl deb package, which is"
echo "in the Debian archive.  A backported version for Debian 2.2 (potato)"
echo "can be found at:"
echo "  http://www.dannf.org/testing"
echo "The RPM package is available from:"
echo "  http://sf.net/projects/systemconfig"
delim
echo -n "Press <Enter> to continue..."
read

### BEGIN dhcp stuff ###
# Hey! Want dhcp?
clear
echo -n "Would you like me to configure this computer as a dhcp server now? (y/[n]) "
read REPLY
case $REPLY in
  y|Y|Yes|yes|YES ) mkdhcpserver ;;
esac
### END dhcp stuff ###

exit 0
