#!/bin/ksh

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

TRYING_TO_INSTALL_OVER_EXISTING_MALL_CC_DIR=101
TRYING_TO_UPDATE_NON_EXISTING_MALL_CC_DIR=102
UNABLE_TO_CREATE_MALL_CC_DIR=103
TRYING_TO_INSTALL_OVER_EXISTING_MALL_OF_DIR=201
TRYING_TO_UPDATE_NON_EXISTING_MALL_OF_DIR=202
UNABLE_TO_CREATE_MALL_OF_DIR=203
INCORRECT_TAR_PATH=602
UNABLE_TO_CREATE_LOG_FILE_DIR=701
UNABLE_TO_CREATE_LOG_FILE=702
UNABLE_TO_CREATE_SHOPSITE_IMAGE_DIR=703
FATAL_ERROR=1000



#
# 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"
}



case $# in
  10)  ## This is the number we should expect.
       ;;
  *)   ## Wrong number of arguments passed to this script.
       print_debug
       print_debug "Illegal number of arguments passed."
       print_debug "Mall installation aborted."
       print_debug "(check_setup_mall_sc.ksh)"
       print_debug
       ERROR=`./error_handler.ksh $FATAL_ERROR`
       printf "ERROR: $ERROR\n"
       exit 1
       ;;
esac



## Check ShopSite directory
if test -e "$SHOPSITE_DIRECTORY/start.cgi"
then
    if test "X$ACTION" = "XINSTALL"
    then
        ## Trying to install over existing mall.
        ERROR=`./error_handler.ksh $TRYING_TO_INSTALL_OVER_EXISTING_MALL_CC_DIR`
        print_debug "ERROR: $ERROR"
        error_found=TRUE
    fi
else
    if test "X$ACTION" = "XUPDATE"
    then
        ## Trying to update a mall that does not exist.
        ERROR=`./error_handler.ksh $TRYING_TO_UPDATE_NON_EXISTING_MALL_CC_DIR`
        print_debug "ERROR: $ERROR"
        error_found=TRUE
    else
        ##  Create the ShopSite directory
        if ! (./create_directory.ksh $SHOPSITE_DIRECTORY $UNIX_WEB_USER_ID $UNIX_WEB_GROUP_ID "750" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
        then
            ERROR=`./error_handler.ksh $UNABLE_TO_CREATE_MALL_CC_DIR`
            print_debug "ERROR: $ERROR"
            error_found=TRUE
        fi
    fi
fi


## Check images directory
if ! (./create_directory.ksh $SHOPSITE_IMAGE_DIR/en-US  $UNIX_WEB_USER_ID $UNIX_WEB_GROUP_ID "750" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
   then
     ERROR=`./error_handler.ksh $UNABLE_TO_CREATE_SHOPSITE_IMAGE_DIR`
     print_debug "ERROR: $ERROR"
     error_found=TRUE
fi

## Check shopping cart directory
if test -e "$SHOPPING_CART_DIRECTORY/thankyou.cgi"
then
    if test "X$ACTION" = "XINSTALL"
    then
        ## Trying to install over existing mall.
        ERROR=`./error_handler.ksh $TRYING_TO_INSTALL_OVER_EXISTING_MALL_OF_DIR`
        print_debug "ERROR: $ERROR"
        error_found=TRUE
    fi
else
    if test "X$ACTION" = "XUPDATE"
    then
        ## Trying to update a mall that does not exist.
        ERROR=`./error_handler.ksh $TRYING_TO_UPDATE_NON_EXISTING_MALL_OF_DIR`
        print_debug "ERROR: $ERROR"
        error_found=TRUE
    else
        ##  Create the shopping cart directory
        if ! (./create_directory.ksh $SHOPPING_CART_DIRECTORY  $UNIX_WEB_USER_ID $UNIX_WEB_GROUP_ID "750" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
        then
            ERROR=`./error_handler.ksh $UNABLE_TO_CREATE_MALL_OF_DIR`
            print_debug "ERROR: $ERROR"
            error_found=TRUE
        fi
    fi
fi


## Check path to Tar
if ! test -e "$PATH_TO_TAR"
then
    ERROR=`./error_handler.ksh $INCORRECT_TAR_PATH`
    print_debug "ERROR: $ERROR"
    error_found=TRUE
fi


if test "X$error_found" = "XTRUE"
then
    ERROR=`./error_handler.ksh $FATAL_ERROR`
    printf "ERROR: $ERROR\n"
    exit 1
fi

exit 0
