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

#
# usage
#
usage()
{
  echo ""
  echo "Usage: $0 [-R <root_dir> (Only for Solaris)] [-h] <patch-directory>\n"
  echo "This program installs the new patch configuration into UWC deployment.\n"
  echo "  Where,"
  echo "   <root_dir>:	client_root_path is the directory that contains the bootable "
  echo "   root of a client from the server's perspective."
  echo "   client from the server's perspective. Specify client_root_path as the"
  echo "   absolute client_root_path as the absolute path name to the beginning"  
  echo "   of the directory tree under which all patch files generated by "
  echo "   patchadd are to be located"
  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 $ROOTDIR_OPT SUNWuwc BASEDIR`
    basedir=$ROOTDIR$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
  NumberOfFilesRemoved=0

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

  cat $INSTALL_LIST_FILE | egrep "^$COPY_FILE |^$COPY_NEW_FILE |^$REMOVE_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
    
#If the file to be replaced is symbolic link remove it

      	 if [ $test_link $DestFile ]; then
      	    
	    /bin/rm -f $DestFile
	 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
         
         # jcapi.jar is now has 600 has permission, we need to change it as 644 
         # otherwise non root deployment will have issues. See bug 6192589
         
         if [ -f $DestFile -a `basename $DestFile` = "jcapi.jar" ]; then
            log_msg "-- /bin/chmod 644 $DestFile"
            /bin/chmod 644 $DestFile
         fi

         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"
         
       elif [ "$REMOVE_FILE" = "$CopyType" ]; then
       
             
         
         if [ -f $DestFile -o $test_link $DestFile ]; then         
           
           log_msg  "-- Removing : $DestFile" 
           log_only ""
           
           log_only "/bin/rm -f $DestFile"
           log_only ""
           
           /bin/rm -f $DestFile
           
          NumberOfFilesRemoved=`expr $NumberOfFilesRemoved + 1`
          echo "$NumberOfFilesRemoved" > "$Tempfile_NumFilesRemoved"
        fi  
           
           shared_comp_file=`basename $DestFile`
           os=`uname`
           
           if [ $os = "SunOS" ]; then
           
              if [ $shared_comp_file = "jato.jar" -o $shared_comp_file = "jato.tld" ]; then
                 log_msg "-- Creating symlink : /bin/ln -sf /usr/share/lib/jato/$shared_comp_file $DestFile"
                 log_msg ""
           	 /bin/ln -sf /usr/share/lib/jato/$shared_comp_file $DestFile
           	 
           	 if [ ! -r /usr/share/lib/jato/$shared_comp_file ]; then
                      log_msg "WARNING : /usr/share/lib/jato/$shared_comp_file not installed. /usr/share/lib/jato/$shared_comp_file is required for UWC to work"
                      log_msg ""
                 fi
              elif [ ! \( \( $shared_comp_file = "am_logging.jar" \) -o \( $shared_comp_file = "am_sdk.jar" \) -o \( $shared_comp_file = "am_services.jar" \) -o \( $shared_comp_file = "jss3.jar" \) -o \( $shared_comp_file = "xtype.jar" \) -o \( $shared_comp_file = "jaxp-api.jar" \) -o \( $shared_comp_file = "sax.jar" \) -o \( $shared_comp_file = "xmlutil.jar" \) -o \( $shared_comp_file = "xercesImpl.jar" \) \) ]; then
                    log_msg "-- Creating symlink : /bin/ln -sf /usr/share/lib/$shared_comp_file $DestFile"
                    log_msg ""
                   /bin/ln -sf /usr/share/lib/$shared_comp_file $DestFile
                   if [ ! -r /usr/share/lib/$shared_comp_file ]; then
                      log_msg "WARNING : /usr/share/lib/$shared_comp_file not installed. /usr/share/lib/$shared_comp_file is required for UWC to work"
                      log_msg ""
                   fi
              fi
              
           elif [ $os = "Linux" ]; then
              if [ $shared_comp_file = "jato.jar" -o $shared_comp_file = "jato.tld" -o $shared_comp_file = "jcapi.jar" ]; then
                 log_msg "-- Creating symlink : /bin/ln -sf /opt/sun/private/share/lib/$shared_comp_file $DestFile"
                 log_msg ""
           	 /bin/ln -sf /opt/sun/private/share/lib/$shared_comp_file $DestFile
           	 if [ ! -r /opt/sun/private/share/lib/$shared_comp_file ]; then
           	    log_msg "WARNING : /opt/sun/private/share/lib/$shared_comp_file not installed. /opt/sun/private/share/lib/$shared_comp_file is required for UWC to work"
                    log_msg ""
           	 fi
              elif [ ! \( \( $shared_comp_file = "am_logging.jar" \) -o \( $shared_comp_file = "am_sdk.jar" \) -o \( $shared_comp_file = "am_services.jar" \) -o \( $shared_comp_file = "jss3.jar" \) -o \( $shared_comp_file = "xtype.jar" \) -o \( $shared_comp_file = "jaxp-api.jar" \) -o \( $shared_comp_file = "sax.jar" \) -o \( $shared_comp_file = "xmlutil.jar" \) -o \( $shared_comp_file = "xercesImpl.jar" \) \) ]; then
                   if [ \( $shared_comp_file = "activation.jar" \) -o \( $shared_comp_file = "mail.jar" \) ]; then
                      log_msg "-- Creating symlink : /bin/ln -sf /usr/share/lib/$shared_comp_file $DestFile"
                      log_msg ""
                      /bin/ln -sf /usr/share/lib/$shared_comp_file $DestFile
                      if [ ! -r /usr/share/lib/$shared_comp_file ]; then
                         log_msg "WARNING : /usr/share/lib/$shared_comp_file not installed./usr/share/lib/$shared_comp_file is required for UWC to work"
                         log_msg ""
                      fi
                   else
                      log_msg "-- Creating symlink : /bin/ln -sf /opt/sun/share/lib/$shared_comp_file $DestFile"
                      log_msg ""
                      /bin/ln -sf /opt/sun/share/lib/$shared_comp_file $DestFile
                      if [ ! -r /opt/sun/share/lib/$shared_comp_file ]; then
                         log_msg "WARNING : /opt/sun/share/lib/$shared_comp_file not installed. /opt/sun/share/lib/$shared_comp_file is required for UWC to work"
                         log_msg ""
                      fi
                   fi   
              fi
           fi    
       fi

     done

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


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

# Definitions
#
COPY_FILE=copyfile
COPY_NEW_FILE=copynewfile
REMOVE_FILE=removefile

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

# Is log initialized
#
IS_LOG_INITIALIZED=0

ROOTDIR=""
ROOTDIR_OPT=""


# Process arguments.
#
while getopts hR: theOption
do
    case $theOption in
    h)
        usage;;
    R) 
        ROOTDIR=$OPTARG
        if [ -d "$ROOTDIR" ]; then
         ROOTDIR_OPT="-R $ROOTDIR"
        fi
        ;;
    \?)
        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

os=`uname`
if [ "$os" = "SunOS" ]; then
  test_link=-h  
else
  test_link=-L
fi        

# 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`
Tempfile_NumFilesRemoved=/tmp/.uwc_install_new_config_tempfile_NumFilesRemoved_`date +%Y%m%d%H%M%S`

touch "$Tempfile_NumFilesModified"
touch "$Tempfile_NumNewFiles"
touch "$Tempfile_NumFilesRemoved"
echo "0" > "$Tempfile_NumFilesModified"
echo "0" > "$Tempfile_NumNewFiles"
echo "0" > "$Tempfile_NumFilesRemoved"

# 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 "-- Number of removed files = $NUM_FILES_REMOVED "
log_msg "--"

if [ \( "0" != "$NUM_FILES_MODIFIED" \) -o \( "0" != "$NUM_NEW_FILES" \) -o \( "0" != " $NUM_FILES_REMOVED" \) ]; then
  log_msg "--"
  log_msg "-- * It is recommended to remove the directory, which caches information for JSPs corresponding to this application"
  log_msg "--   Restart your web container for the changes to take effect."
  log_msg "--"
  log_msg "-- ** WARNING: Please run the latest comm_dssetup.pl ver 6.3 against Directory Server setup."
  log_msg "--             This is required for Address Book application to work."
fi

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


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

exit 0
