#!/bin/ksh

#
# set up internal parameters
#

SHOPSITE_DIRECTORY=$1
SHOPPING_CART_DIRECTORY=$2
SHOPSITE_IMAGE_DIR=$3
SHOPSITE_IMAGE_URL=$4
UNIX_WEB_USER_ID=$5
UNIX_WEB_GROUP_ID=$6
PATH_TO_TAR=$7
DEBUG_LOG_DIRECTORY=$8
DEBUG_LOG_FILE=$9
LOG_DEBUG=${10}

FATAL_ERROR=1000

#
# Functions
#



#
# print debug string to appropriate location
#
# $1=debug string to be printed
#
print_debug()
{
  ./debug_handler.ksh "$0" "mall" "$1" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG"
}



#
# Populate a directory from a tar file.
#
# populate_dir $1=directory $2=tar file name
#
populate_dir()
{
  INSTALL_DIRECTORY=$PWD
  cd $1

  if ! ($PATH_TO_TAR -xf $INSTALL_DIRECTORY/${2}.tar)
  then
      cd $INSTALL_DIRECTORY
      print_debug "Error: Could not untar the file: $INSTALL_DIRECTORY/$2.tar"
      print_debug "       Mall installation aborted."
      print_debug
      ERROR=`./error_handler.ksh $FATAL_ERROR`
      printf "ERROR: $ERROR\n"
      exit 1 
  else
      cd $INSTALL_DIRECTORY
      print_debug "Successfully populated the directory:"
      print_debug "$1"
      print_debug
  fi
}

#
# Create file links
#
# link_file $1=file to link to $2=symbolic link
#
link_file()
{
  if test -e ${2}
  then
      rm ${2}
  fi
  if test -e ${1}
  then
    if ! (ln -s ${1} ${2})
    then
      print_debug "Error: Could not link file:"
      print_debug "       $2 to"
      print_debug "       $1"
      print_debug "       Mall installation aborted."
      print_debug
      ERROR=`./error_handler.ksh $FATAL_ERROR`
      printf "ERROR: $ERROR\n"
      exit 1
    else
      print_debug "Successfully linked the file:"
      print_debug "$2 to"
      print_debug "$1"
      print_debug
    fi
  fi
}


##############################################
#########                            #########
#########  Actual code starts here.  #########
#########                            #########
##############################################
#
#
#

#  Untar sscgis.tar into the ShopSite directory.
print_debug "populate_dir $SHOPSITE_DIRECTORY sscgis"
populate_dir $SHOPSITE_DIRECTORY sscgis

#  Untar sccgis.tar into the shopping cart directory.
print_debug "populate_dir $SHOPPING_CART_DIRECTORY sccgis"
populate_dir $SHOPPING_CART_DIRECTORY sccgis

#  Create a link to $SHOPSITE_DIRECTORY/global.aa in the shopping cart directory.
print_debug "link_file $SHOPSITE_DIRECTORY/global.aa $SHOPPING_CART_DIRECTORY/global.aa"
link_file $SHOPSITE_DIRECTORY/global.aa $SHOPPING_CART_DIRECTORY/global.aa

#  Create a link to $SHOPSITE_DIRECTORY/libsscommon.so.1 in the shopping cart directory.
print_debug "link_file $SHOPSITE_DIRECTORY/libsscommon.so.1 $SHOPPING_CART_DIRECTORY/libsscommon.so.1"
link_file $SHOPSITE_DIRECTORY/libsscommon.so.1 $SHOPPING_CART_DIRECTORY/libsscommon.so.1

#  Create links to various shopping cart files in the ShopSite directory.
link_file $SHOPPING_CART_DIRECTORY/libpfpro.so $SHOPSITE_DIRECTORY/libpfpro.so
link_file $SHOPPING_CART_DIRECTORY/certs $SHOPSITE_DIRECTORY/certs

#  Create links to ShopSite files in the shopping cart directory
link_file $SHOPSITE_DIRECTORY/localeinfo.dat $SHOPPING_CART_DIRECTORY/localeinfo.dat

##  Set permissions and ownership on all ShopSite mall files.
if ! (./file_settings_mall_sc.ksh "$SHOPSITE_DIRECTORY" "$SHOPPING_CART_DIRECTORY" "$SHOPSITE_IMAGE_DIR" "$UNIX_WEB_USER_ID" "$UNIX_WEB_GROUP_ID" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
then
    print_debug "Aborting installation:"
    print_debug "FAILED: ./file_settings_mall_sc.ksh"
    ERROR=`./error_handler.ksh $FATAL_ERROR`
    printf "ERROR: $ERROR\n"
    exit 1
fi

# Setup the global.aa file in the ss directory
print_debug "Setting Currency token in the ${SHOPSITE_DIRECTORY}/global.aa file"
touch $SHOPSITE_DIRECTORY/global.aa.new
while read -r field value
do
    if ! test "X$field" = "Ximage_dir:" && ! test "X$field" = "Ximage_url:"
    then
        printf "%s %s\n" "$field" "$value" >> ${SHOPSITE_DIRECTORY}/global.aa.new
    fi
done < ${SHOPSITE_DIRECTORY}/global.aa

#These have moved to the storeid.aa file
#printf "image_dir: %s\n" "${SHOPSITE_IMAGE_DIR}" >> $SHOPSITE_DIRECTORY/global.aa.new
#printf "image_url: %s\n" "${SHOPSITE_IMAGE_URL}" >> $SHOPSITE_DIRECTORY/global.aa.new

#  Copy the new file over the old file.
mv -f ${SHOPSITE_DIRECTORY}/global.aa.new ${SHOPSITE_DIRECTORY}/global.aa

# Install a language pack

exit 0
