#!/bin/sh
############################################################
# autoutil:	Automation support utility script
# 9/16/09	initial code, boot to automation using hard coded values
# 10/2/09	add boot to production, check for current running environment,
#		still using hard coded values, currently disabled
# 10/19/09	add 'copy file to/from automation', enable boot to production,
#		first cut - from production only
# 11/10/09	look for grub.conf first then menu.lst for grub config file,
#		use grub.conf-? files for temp configs, make sure we booted
#		from automation directory before trying to make changes
# 11/23/09	check if file exists while checking if link file, check for
#		grub conf files in non ./boot relative path in automation
# 12/2/09	automation directory installs are now relative to /boot/altiris
#		with /boot/altiris/default being the first (default) install
# 12/14/09	check current grub.conf with -p, if -p exists and not same then
#		autoutil -a may have been run twice, else error
############################################################

WORK_LOG="/tmp/autoutil.log"

#---------------------------------------
## record in WORK_LOG
function WriteLog()
{
  echo "[autoutil `date +\"%Y-%m-%d %H:%M:%S\"`] :$*" >> $WORK_LOG
}

#---------------------------------------
SameFile()
{
  if [ $# -eq 2 ]; then
    if [ -e "$1" ]; then
      if [ -e "$2" ]; then
        f1=`md5sum $1 | awk '{print $1}'`
        f2=`md5sum $2 | awk '{print $1}'`
        if [ "$f1" != "$f2" ]; then
          return 1
        else
          return 0
        fi;
      else
        echo "file not found $2"
        exit 1
      fi;
    else
      echo "file not found $1"
      exit 1
    fi;
  else
    echo "bad args: $0"
    exit 1
  fi;
}

#===========================================================
if [ "$1" == "-a" ]; then
  ## set nextboot to Automation
  if [ -e /etc/linuxpe-release ]; then
    ## we're in automation
    #  setting the nextboot to automation while in automation, we should be
    #  able to ignore but better make sure the right menu file is being used
    if [ "$ALTIRIS_BOOTDEVICE" == "DIR" ]; then
      if [ -d /mnt/atrsboot/boot ]; then
        if [ -e /mnt/atrsboot/boot/altiris/default/grub.conf-a ]; then
          if [ ! -L /mnt/atrsboot/boot/grub/grub.conf -a -e /mnt/atrsboot/boot/grub/grub.conf ]; then
            if ! SameFile /mnt/atrsboot/boot/altiris/default/grub.conf-a /mnt/atrsboot/boot/grub/grub.conf; then
              cp -p /mnt/atrsboot/boot/altiris/default/grub.conf-a /mnt/atrsboot/boot/grub/grub.conf
            fi;
          elif [ ! -L /mnt/atrsboot/boot/grub/menu.lst -a -e /mnt/atrsboot/boot/grub/menu.lst ]; then
            if ! SameFile /mnt/atrsboot/boot/altiris/default/grub.conf-a /mnt/atrsboot/boot/grub/menu.lst; then
              cp -p /mnt/atrsboot/boot/altiris/default/grub.conf-a /mnt/atrsboot/boot/grub/menu.lst
            fi;
          else
            WriteLog "Production Grub configuration file not found, unable to continue (boot)"
            exit 1
          fi;
        else
          WriteLog "Automation Grub config not found, unable to set nextboot to Automation (boot)"
          exit 1
        fi;
      else
        if [ -e /mnt/atrsboot/altiris/default/grub.conf-a ]; then
          if [ ! -L /mnt/atrsboot/grub/grub.conf -a -e /mnt/atrsboot/grub/grub.conf ]; then
            if ! SameFile /mnt/atrsboot/altiris/default/grub.conf-a /mnt/atrsboot/grub/grub.conf; then
              cp -p /mnt/atrsboot/altiris/default/grub.conf-a /mnt/atrsboot/grub/grub.conf
            fi;
          elif [ ! -L /mnt/atrsboot/grub/menu.lst -a -e /mnt/atrsboot/grub/menu.lst ]; then
            if ! SameFile /mnt/atrsboot/altiris/default/grub.conf-a /mnt/atrsboot/grub/menu.lst; then
              cp -p /mnt/atrsboot/altiris/default/grub.conf-a /mnt/atrsboot/grub/menu.lst
            fi;
          else
            WriteLog "Production Grub configuration file not found, unable to continue"
            exit 1
          fi;
        else
          WriteLog "Automation Grub config not found, unable to set nextboot to Automation"
          exit 1
        fi;
      fi;
    else
      WriteLog "Must boot from Automation Directory to set next boot"
      exit 1
    fi;
  else
    ## we're in production
    #Check /boot file system, if ext4 we fail since our Preboot does not support ext4 yet
    bootPart=`mount | grep "/boot type"`;
    if [ "${bootPart}" == "" ]; then
      #/boot partition not found, looks like /boot is on / partition
      bootPart=`mount | grep "/ type"`; 
    fi				
    bootExt4=`echo "${bootPart}" | grep "ext4"`;
    if [ "${bootExt4}" != "" ]; then
      #we have found /boot partition as ext4.
      WriteLog "Unsupported /boot partition (ext4) found, unable to continue"
      exit 2		
    fi	
    if [ -e /boot/altiris/default/grub.conf-a ]; then
      if [ ! -e /boot/grub/grub.conf-sav ]; then
        if [ ! -L /boot/grub/grub.conf -a -e /boot/grub/grub.conf ]; then
          cp -p /boot/grub/grub.conf /boot/grub/grub.conf-sav
        elif [ ! -L /boot/grub/menu.lst -a -e /boot/grub/menu.lst ]; then
          cp -p /boot/grub/menu.lst /boot/grub/grub.conf-sav
        else
          WriteLog "Production Grub configuration file not found, unable to continue (-sav)"
          exit 1
        fi;
      fi;
      if [ ! -L /boot/grub/grub.conf -a -e /boot/grub/grub.conf ]; then
        ## if grub.conf file and -p are not the same it is possible the '-a'
        #  operation has been done previously... but not sure so don't continue
        if [ -e /boot/grub/grub.conf-p ]; then
          if ! SameFile /boot/grub/grub.conf /boot/grub/grub.conf-p; then
            if SameFile /boot/grub/grub.conf /boot/altiris/default/grub.conf-a; then
              WriteLog "autoutil -a has been run more than once, ignoring"
              exit 0
            else
              WriteLog "Backup copy of grub.conf has been modified.. not good! Check file!"
              exit 1
            fi;
          else
            WriteLog "Backup copy of grub.conf exists, backup not needed but allowed"
          fi;
        else
          WriteLog "Saving backup copy of grub.conf"
        fi;
        mv -f /boot/grub/grub.conf /boot/grub/grub.conf-p
        cp -p /boot/altiris/default/grub.conf-a /boot/grub/grub.conf
      elif [ ! -L /boot/grub/menu.lst -a -e /boot/grub/menu.lst ]; then
        ## if menu.lst file and -p are not the same it is possible the '-a'
        #  operation has been done previously... but not sure so don't continue
        if [ -e /boot/grub/grub.conf-p ]; then
          if ! SameFile /boot/grub/menu.lst /boot/grub/grub.conf-p; then
            if SameFile /boot/grub/menu.lst /boot/altiris/default/grub.conf-a; then
              WriteLog "autoutil -a has been run more than once, ignoring"
              exit 0
            else
              WriteLog "Backup copy of menu.lst has been modified.. not good! Check file!"
              exit 1
            fi;
          else
            WriteLog "Backup copy of menu.lst exists, backup not needed but allowed"
          fi;
        else
          WriteLog "Saving backup copy of menu.lst"
        fi;
        mv -f /boot/grub/menu.lst /boot/grub/grub.conf-p
        cp -p /boot/altiris/default/grub.conf-a /boot/grub/menu.lst
      else
        WriteLog "Production Grub configuration file not found, unable to continue"
        exit 1
      fi;
      exit 0
    else
      WriteLog "* * * Boot folder config file /boot/altiris/default/grub.conf-a not found * * *"
      exit 1
    fi;
  fi;
elif [ "$1" == "-p" ]; then
  ## set nextboot to Production
  if [ -e /etc/linuxpe-release ]; then
    ## we're in automation
    if [ "$ALTIRIS_BOOTDEVICE" == "DIR" ]; then
      if [ -e /mnt/atrsboot/boot/grub/grub.conf-p -a -d /mnt/atrsboot/boot ]; then
        if [ ! -L /mnt/atrsboot/boot/grub/grub.conf -a -e /mnt/atrsboot/boot/grub/grub.conf ]; then
          cp -pf /mnt/atrsboot/boot/grub/grub.conf-p /mnt/atrsboot/boot/grub/grub.conf
        elif [ ! -L /mnt/atrsboot/boot/grub/menu.lst -a -e /mnt/atrsboot/boot/grub/menu.lst ]; then
          cp -pf /mnt/atrsboot/boot/grub/grub.conf-p /mnt/atrsboot/boot/grub/menu.lst
        else
          WriteLog "Grub configuration file not found, unable to continue"
          exit 1
        fi;
        exit 0
      elif [ -e /mnt/atrsboot/grub/grub.conf-p ]; then
        if [ ! -L /mnt/atrsboot/grub/grub.conf -a -e /mnt/atrsboot/grub/grub.conf ]; then
          cp -pf /mnt/atrsboot/grub/grub.conf-p /mnt/atrsboot/grub/grub.conf
        elif [ ! -L /mnt/atrsboot/grub/menu.lst -a -e /mnt/atrsboot/grub/menu.lst ]; then
          cp -pf /mnt/atrsboot/grub/grub.conf-p /mnt/atrsboot/grub/menu.lst
        else
          WriteLog "Grub configuration file not found, unable to continue"
          exit 1
        fi;
        exit 0
      else
        mkdir /mnt/tmp 2>/dev/null
        for device in /dev/[hs]d[a-z][1-9]; do
          mount $device /mnt/tmp 2>/dev/null
          if [ -e /mnt/tmp/boot/grub/grub.conf-p -a -d /mnt/tmp/boot ]; then
            if [ ! -L /mnt/tmp/boot/grub/grub.conf -a -e /mnt/tmp/boot/grub/grub.conf ]; then
              cp -p /mnt/tmp/boot/grub/grub.conf-p /mnt/tmp/boot/grub/grub.conf
              umount $device 2>/dev/null
              exit 0
            elif [ ! -L /mnt/tmp/boot/grub/menu.lst -a -e /mnt/tmp/boot/grub/menu.lst ]; then
              cp -p /mnt/tmp/boot/grub/grub.conf-p /mnt/tmp/boot/grub/menu.lst
              umount $device 2>/dev/null
              exit 0
            fi;
          elif [ -e /mnt/tmp/grub/grub.conf-p ]; then
            if [ ! -L /mnt/tmp/grub/grub.conf -a -e /mnt/tmp/grub/grub.conf ]; then
              cp -p /mnt/tmp/grub/grub.conf-p /mnt/tmp/grub/grub.conf
              umount $device 2>/dev/null
              exit 0
            elif [ ! -L /mnt/tmp/grub/menu.lst -a -e /mnt/tmp/grub/menu.lst ]; then
              cp -p /mnt/tmp/grub/grub.conf-p /mnt/tmp/grub/menu.lst
              umount $device 2>/dev/null
              exit 0
            fi;
          fi;
          umount $device 2>/dev/null
        done;
      fi;
      ## fell through without finding and copying, returning an error
      WriteLog "Production Grub configuration file not found during search, unable to continue"
      exit 1
    else
      WriteLog "Must boot from Automation Directory to set next boot"
      exit 1
    fi;
  else
    ## we're in production
    #  in production, if we made it to this point, we should be able
    #  to ignore this but will make sure the right menu file is in place
    if [ -e /boot/grub/grub.conf-p ]; then
      if [ ! -L /boot/grub/grub.conf -a -e /boot/grub/grub.conf ]; then
        if ! SameFile /boot/grub/grub.conf-p /boot/grub/grub.conf; then
          cp -pf /boot/grub/grub.conf-p /boot/grub/grub.conf
        fi;
      elif [ ! -L /boot/grub/menu.lst -a -e /boot/grub/menu.lst ]; then
        if ! SameFile /boot/grub/grub.conf-p /boot/grub/menu.lst; then
          cp -pf /boot/grub/grub.conf-p /boot/grub/menu.lst
        fi;
      else
        WriteLog "Production Grub configuration file not found, unable to continue (-p)"
        exit 1
      fi;
    else
      if [ ! -L /boot/grub/grub.conf -a -e /boot/grub/grub.conf ]; then
        cp -p /boot/grub/grub.conf /boot/grub/grub.conf-p
      elif [ ! -L /boot/grub/menu.lst -a -e /boot/grub/menu.lst ]; then
        cp -p /boot/grub/menu.lst /boot/grub/grub.conf-p
      else
        WriteLog "Production Grub configuration file not found, unable to continue"
        exit 1
      fi;
    fi;
  fi;
elif [ "$1" == "-c" ]; then
  ## copy file to/from automation
  if [ -e /etc/linuxpe-release ]; then
    ## we're in automation
    WriteLog "function not implemented"
    exit 0
  else
    ## we're in production
    #  first param should be the file to copy, second is destination
    if [ -e $2 ]; then
      FILE=$2
      if echo $3 | egrep "{dot}" >/dev/null 2>&1; then
        cp -p $2 /boot/altiris/default
      else
        WriteLog "Unsupported destination in production"
        exit 1
      fi;
    else
      WriteLog "File to copy not found: $2"
      exit 1
    fi;
  fi;
fi; 

exit 0
