#!/bin/ksh

ACTION=$1
SHOPSITE_DIRECTORY=$2
SHOPPING_CART_DIRECTORY=$3
SHOPSITE_STORE_ID=$4
DATA_DIRECTORY=$5
HTML_DIRECTORY=$6
SELLER_USER_ID=$7
SELLER_GROUP_ID=$8
UNIX_WEB_USER_ID=$9
UNIX_WEB_GROUP_ID=${10}
PATH_TO_TAR=${11}
PATH_TO_SENDMAIL=${12}
LOG_DEBUG=${13}
DEBUG_LOG_DIRECTORY=${14}
DEBUG_LOG_FILE=${15}

LACKING_AUTHORIZATION_FILE=1001
TRYING_TO_INSTALL_USING_EXISTING_STORE_ID=801
TRYING_TO_UPDATE_NON_EXISTING_STORE_ID=802
TRYING_TO_INSTALL_USING_INVALID_SHOPSITE_DIR=104
TRYING_TO_INSTALL_USING_INVALID_SHOPPING_CART_DIR=204
TRYING_TO_INSTALL_OVER_EXISTING_STORE_HTML_DIR=401
TRYING_TO_UPDATE_NON_EXISTING_STORE_HTML_DIR=402
UNABLE_TO_CREATE_STORE_HTML_DIR=403
TRYING_TO_INSTALL_OVER_EXISTING_STORE_DATA_DIR=501
TRYING_TO_UPDATE_NON_EXISTING_STORE_DATA_DIR=502
UNABLE_TO_CREATE_STORE_DATA_DIR=503
INCORRECT_TAR_PATH=602
INCORRECT_SENDMAIL_PATH=603
UNABLE_TO_CREATE_LOG_FILE_DIR=701
UNABLE_TO_CREATE_LOG_FILE=702
FATAL_ERROR=1000



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



case $# in
  15)  ## 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 "Store installation aborted."
       print_debug "(check_setup_store_sc.ksh)"
       print_debug ""
       ERROR=`./error_handler.ksh $FATAL_ERROR`
       printf "ERROR: $ERROR\n"
       exit 1
       ;;
esac




## Check authorization file
if ! test -e "./${SHOPSITE_STORE_ID}.auth"
then
    ERROR=`./error_handler.ksh $LACKING_AUTHORIZATION_FILE`
    print_debug "ERROR: $ERROR"
    error_found=TRUE
fi


## Check ShopSite directory
if test -e "$SHOPSITE_DIRECTORY/start.cgi"
then
    ## Make sure the SHOPSITE_STORE_ID is unique (SHOPSITE_STORE_ID.aa does not
    ## already exist in the ShopSite directory)
    if test "X$ACTION" = "XINSTALL"
    then
        if test -e "$SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa"
        then
            ## Trying to install using a store id that already exists.
            ERROR=`./error_handler.ksh $TRYING_TO_INSTALL_USING_EXISTING_STORE_ID`
            print_debug "ERROR: $ERROR"
            error_found=TRUE
        fi
    elif test "X$ACTION" = "XUPDATE"
    then
        if ! test -e "$SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa"
        then
            ## Trying to update using a store id that does not exist.
            ERROR=`./error_handler.ksh $TRYING_TO_UPDATE_NON_EXISTING_STORE_ID`
            print_debug "ERROR: $ERROR"
            error_found=TRUE
        fi
    fi
else
    ## Trying to install using an invalid ShopSite directory
    ERROR=`./error_handler.ksh $TRYING_TO_INSTALL_USING_INVALID_SHOPSITE_DIR`
    print_debug "ERROR: $ERROR"
    error_found=TRUE
fi


## Check shopping cart directory
if ! test -e "$SHOPPING_CART_DIRECTORY/thankyou.cgi"
then
    ## Trying to install using and invalid shopping cart directory
    ERROR=`./error_handler.ksh $TRYING_TO_INSTALL_USING_INVALID_SHOPPING_CART_DIR`
    print_debug "ERROR: $ERROR"
    error_found=TRUE
fi


## Check HTML directory
if test -e "$HTML_DIRECTORY/thankyou.cgi"
then
    if test "X$ACTION" = "XINSTALL"
    then
        ## Trying to install over existing store.
        ERROR=`./error_handler.ksh $TRYING_TO_INSTALL_OVER_EXISTING_STORE_HTML_DIR`
        print_debug "ERROR: $ERROR"
        error_found=TRUE
    fi
else
    if test "X$ACTION" = "XUPDATE"
    then
        ## Trying to update a store that does not exist.
        ERROR=`./error_handler.ksh $TRYING_TO_UPDATE_NON_EXISTING_STORE_HTML_DIR`
        print_debug "ERROR: $ERROR"
        error_found=TRUE
    else
        ##  Create the HTML directory
        if ! (./create_directory.ksh $HTML_DIRECTORY $SELLER_USER_ID $SELLER_GROUP_ID "771" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
        then
            ERROR=`./error_handler.ksh $UNABLE_TO_CREATE_STORE_HTML_DIR`
            print_debug "ERROR: $ERROR"
            error_found=TRUE
        fi
	##  Create the HTML/smarthtml directory
        if ! (./create_directory.ksh $HTML_DIRECTORY/smarthtml $SELLER_USER_ID $SELLER_GROUP_ID "771" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
        then
            ERROR=`./error_handler.ksh $UNABLE_TO_CREATE_STORE_HTML_DIR`
            print_debug "ERROR: $ERROR"
            error_found=TRUE
        fi
    fi
fi


## Check Data directory
if test -e "$DATA_DIRECTORY/thankyou.cgi"
then
    if test "X$ACTION" = "XINSTALL"
    then
        ## Trying to install over existing store.
        ERROR=`./error_handler.ksh $TRYING_TO_INSTALL_OVER_EXISTING_STORE_DATA_DIR`
        print_debug "ERROR: $ERROR"
        error_found=TRUE
    fi
else
    if test "X$ACTION" = "XUPDATE"
    then
        ## Trying to update a store that does not exist.
        ERROR=`./error_handler.ksh $TRYING_TO_UPDATE_NON_EXISTING_STORE_DATA_DIR`
        print_debug "ERROR: $ERROR"
        error_found=TRUE
    else
        ##  Create the DATA directory
        if ! (./create_directory.ksh $DATA_DIRECTORY $UNIX_WEB_USER_ID $UNIX_WEB_GROUP_ID "775" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
        then
            ERROR=`./error_handler.ksh $UNABLE_TO_CREATE_STORE_DATA_DIR`
            print_debug "ERROR: $ERROR"
            error_found=TRUE
        fi
        if ! (./create_directory.ksh $DATA_DIRECTORY/log $UNIX_WEB_USER_ID $UNIX_WEB_GROUP_ID "775" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
        then
            ERROR=`./error_handler.ksh $UNABLE_TO_CREATE_STORE_DATA_DIR/log`
            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


## Check path to Sendmail
if ! test -e "$PATH_TO_SENDMAIL"
then
    ERROR=`./error_handler.ksh $INCORRECT_SENDMAIL_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
