#!/bin/sh
#set -x
#
# Install the new configuration files
# takes one argument - the PATCHDIR, typically underinstall/patch/<patchnum>
#

#
# usage
#
usage()
{
  echo ""
  echo "Usage: $0 [-h] <patch-directory>\n"
  echo "This program installs the new patch configuration into UWC deployment.\n"
  echo "  Where,"
  echo "    <patch-directory>: The patch directory area created during patchaadd."
  echo "                       e.g. $basedir/install/patch/116568-05"
  echo ""
  echo "    -h : prints help"
  echo ""
  exit 1
}

exit_with_msg()
{
  ExitCode=1
  ExitMessage="\nExiting ..."
  if [ "" != "$*" ]; then
    ExitCode="$*"
    ExitMessage="\nExiting with status=$* ...\n"
  fi

  if [ "0" = "$IS_LOG_INITIALIZED" ]; then
    echo "$ExitMessage"
    exit "$ExitCode"
  else
    log_msg "$ExitMessage"
    exit "$ExitCode"
  fi
}

#   
# initialize LOGFILE
#
log_init()
{
  touch $LOGFILE

  echo "============ LOGFILE started `date` ==========" >> $LOGFILE
  echo "" >> $LOGFILE

  echo Log file is: $LOGFILE
  echo ""

  IS_LOG_INITIALIZED=1
}

#
# log a message to the screen and to the LOGFILE
#
log_msg() {
  echo "$*"
  echo "$*" >> $LOGFILE
}

#
# log a message to the LOGFILE only
#
log_only() {
  echo "$*" >> $LOGFILE
}

#
# determine basedir
# - sets the variable basedir
#
get_basedir()
{
  if [ "`/bin/uname`" = "SunOS" ]; then
    basedir=`pkgparam SUNWuwc BASEDIR`
  else
    cur_dir=`pwd`
    cd $PATCHDIR
    tmp=`pwd`
    basedir=`echo $tmp | sed 's/\/install\/patch\/.*$//'`
    cd $cur_dir
  fi  

  if [ -z "$basedir" ]; then
    log_msg ""
    log_msg "ERROR: Could not determine pkg base directory for SUNWuwc."
    log_msg ""
    exit_with_msg 11;
  fi

  if [ ! -d "$basedir" ]; then
    log_msg ""
    log_msg "ERROR: pkg base directory for SUNWuwc \"$basedir\" doesn't exists"
    log_msg ""
    exit_with_msg 12;
  fi

  log_msg "-- Package base directory is: $basedir "
  log_msg "--"
}

#
# check to make sure config is accessible
#
check_configaccess() {
  if [ ! -d "$basedir/staging" ]; then
    log_msg "-- ERROR: Could not access the staging area \"$basedir/staging\""
    log_msg ""
    log_msg "Application is not configured."
    log_msg "Please run uwc product configuration first, then run install-newconfig tool."
    log_msg ""
    exit_with_msg 21;
  elif [ ! -d "$basedir/WEB-INF/config" ]; then
    log_msg "-- ERROR: Could not access config directory \"$basedir/WEB-INF/config\""
    log_msg ""
    log_msg "Application is not configured."
    log_msg "Please run uwc product configuration first, then run install-newconfig tool."
    log_msg ""
    exit_with_msg 22;
  fi
}

#
# Install new configuration
#
install_newconfig() {
  log_msg "-- Install new configuration begin run..."
  log_msg "--"

  INSTALL_LIST_FILE=$PATCHDIR/save/install.list

  if [ \( ! -f "$INSTALL_LIST_FILE" \) -o \( ! -r "$INSTALL_LIST_FILE" \) ]; then
    log_msg "-- ERROR: Can't open \"$INSTALL_LIST_FILE\" file."
    log_msg "--"
    log_msg "-- Nothing to be done regarding the installation of new configuration."
    log_msg "--"
    return
  fi

  log_msg "-- Installing config files listed in $INSTALL_LIST_FILE ..."
  log_msg "--"

  NumberOfFilesModified=0
  NumberOfNewFiles=0

  echo "$NumberOfFilesModified" > "$Tempfile_NumFilesModified"
  echo "$NumberOfNewFiles" > "$Tempfile_NumNewFiles"

  cat $INSTALL_LIST_FILE | egrep "^$COPY_FILE |^$COPY_NEW_FILE " |
     while read theFile
     do
       CopyType=`echo $theFile | awk -F' ' '{print $1}'`
       SourceFile=`echo $theFile | awk -F' ' '{print $2}'`
       DestFile=`echo $theFile | awk -F' ' '{print $3}'`
       
       if [ "$COPY_FILE" = "$CopyType" ]; then

         log_msg  "-- Modifying : $DestFile" 
         log_only ""

         theDestDir=`dirname $DestFile`
         if [ "" != "$theDestDir" ]; then
           if [ ! -d "$theDestDir" ]; then
             log_only "/bin/mkdir -p $theDestDir"
             log_only ""

             /bin/mkdir -p "$theDestDir"
           fi
         fi

# While overwriting an existing file, we can overwrite the contents but should
# not overwrite permission settings.
#
         log_only "/bin/cp -f $SourceFile $DestFile"
         log_only ""

         /bin/cp -f $SourceFile $DestFile

         NumberOfFilesModified=`expr $NumberOfFilesModified + 1`
         echo "$NumberOfFilesModified" > "$Tempfile_NumFilesModified"

       elif [ "$COPY_NEW_FILE" = "$CopyType" ]; then

         log_msg  "-- NEW       : $DestFile" 
         log_only ""

         theDestDir=`dirname $DestFile`
         if [ "" != "$theDestDir" ]; then
           if [ ! -d "$theDestDir" ]; then
             log_only "/bin/mkdir -p $theDestDir"
             log_only ""

             /bin/mkdir -p "$theDestDir"
           fi
         fi

         log_only "/bin/cp -pf $SourceFile $DestFile"
         log_only ""

         /bin/cp -pf $SourceFile $DestFile

         NumberOfNewFiles=`expr $NumberOfNewFiles + 1`
         echo "$NumberOfNewFiles" > "$Tempfile_NumNewFiles"

       fi

     done

  NUM_FILES_MODIFIED=`cat $Tempfile_NumFilesModified`
  NUM_NEW_FILES=`cat $Tempfile_NumNewFiles`
}


###########################################################################
#
# main program
#
###########################################################################
#

# Definitions
#
COPY_FILE=copyfile
COPY_NEW_FILE=copynewfile

# NUM_FILES_MODIFIED - number of modified files deployed
# NUM_NEW_FILES - number of new files deployed
#
NUM_FILES_MODIFIED=0
NUM_NEW_FILES=0

# Is log initialized
#
IS_LOG_INITIALIZED=0

# Process arguments.
#
while getopts h theOption
do
    case $theOption in
    h)
        usage;;
    \?)
        echo "\nERROR: Invalid option"
        usage;;
    esac
done

shift `expr $OPTIND - 1`

if [ $# -ne 1 ]; then
  echo "\nERROR: Patch directory not specified"
 usage;
fi

PATCHDIR=$*

if [ ! -d "$PATCHDIR" ]; then
    echo "\nERROR: Patch directory \"$PATCHDIR\" doesn't exists"          
    echo "\nRerun the program and specify a valid patch directory"
    exit_with_msg 2;
fi
 
#
# Make sure patch-config run already
#
cat "$PATCHDIR/save/.patch-config.status" | grep "patch-config=DONE" 2>&1 > /dev/null
if [ "$?" != "0" ]; then
    echo "\nERROR: patch-config is not run."
    echo "\nIt is a requirement that \"patch-config\" program should be run"
    echo "prior to the running of this program."
    echo "\nPlease run patch-config program, and then rerun this program."
    exit_with_msg 4;
fi

#
# initialize logging
#
LOGFILE=$PATCHDIR/install-newconfig_`date +%Y%m%d%H%M%S`.log
log_init

#
# must be root
#
set `/usr/bin/id`
if [ $1 != "uid=0(root)" ]; then
  log_msg ""
  log_msg "ERROR: Not a system's root user"
  log_msg ""
  log_msg "This utility installs new patch/config files for the Communications Express"
  log_msg "To use this utility you need to be the system's root user."
  exit_with_msg 3;
fi

get_basedir

# Set temp files for file counts
#
Tempfile_NumFilesModified=/tmp/.uwc_install_new_config_tempfile_NumFilesModified_`date +%Y%m%d%H%M%S`
Tempfile_NumNewFiles=/tmp/.uwc_install_new_config_tempfile_NumNewFiles_`date +%Y%m%d%H%M%S`

touch "$Tempfile_NumFilesModified"
touch "$Tempfile_NumNewFiles"

# check to make sure config is accessible
#
check_configaccess
install_newconfig

log_msg "--"
log_msg "--"
log_msg "-- Number of modified files deployed = $NUM_FILES_MODIFIED "
log_msg "-- Number of new files deployed = $NUM_NEW_FILES "
log_msg "--"

if [ \( "0" != "$NUM_FILES_MODIFIED" \) -o \( "0" != "$NUM_NEW_FILES" \) ]; then
  log_msg "--"
  log_msg "-- Restart your web container for the changes to take effect."
  log_msg "--"
fi

echo ""
echo "For more details, please check the log file availablte at $LOGFILE "
echo ""


# Cleanup
#
/bin/rm -f "$Tempfile_NumFilesModified"
/bin/rm -f "$Tempfile_NumNewFiles"


exit 0
