#!/bin/ksh

SHOPSITE_STORE_ID=$1
SHOPSITE_DIRECTORY=$2
SHOPPING_CART_DIRECTORY=$3
HTML_DIRECTORY=$4
DATA_DIRECTORY=$5
DEBUG_LOG_DIRECTORY=$6
DEBUG_LOG_FILE=$7
LOG_DEBUG=$8

TRYING_TO_REMOVE_STORE_FROM_AN_INVALID_MALL_CC_DIR=105
TRYING_TO_REMOVE_STORE_FROM_AN_INVALID_MALL_OF_DIR=205
TRYING_TO_REMOVE_STORE_USING_AN_INVALID_HTML_DIR=404
TRYING_TO_REMOVE_STORE_USING_AN_INVALID_DATA_DIR=504
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
  8)  ## 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 removal aborted."
      print_debug "(check_remove_store.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
    ## Trying to remove a store from an invalid ShopSite directory.
    ERROR=`./error_handler.ksh $TRYING_TO_REMOVE_STORE_FROM_AN_INVALID_MALL_CC_DIR`
    print_debug "ERROR: $ERROR"
    print_debug "$SHOPSITE_DIRECTORY"
    error_found=TRUE
fi


## Check shopping cart directory
if ! test -e $SHOPPING_CART_DIRECTORY/thankyou.cgi
then
    ## Trying to remove a store from an invalid shopping cart directory.
    ERROR=`./error_handler.ksh $TRYING_TO_REMOVE_STORE_FROM_AN_INVALID_MALL_OF_DIR`
    print_debug "ERROR: $ERROR"
    print_debug "$SHOPPING_CART_DIRECTORY"
    error_found=TRUE
fi


## Check HTML directory
if ! test -e $HTML_DIRECTORY/media
then
    ## Trying to remove a store using an invalid HTML directory.
    ERROR=`./error_handler.ksh $TRYING_TO_REMOVE_STORE_USING_AN_INVALID_HTML_DIR`
    print_debug "ERROR: $ERROR"
    print_debug "$HTML_DIRECTORY"
    error_found=TRUE
fi


## Check Data directory
if ! test -e $DATA_DIRECTORY/$SHOPSITE_STORE_ID.auth
then
    ## Trying to remove a store using an invalid data directory.
    ERROR=`./error_handler.ksh $TRYING_TO_REMOVE_STORE_USING_AN_INVALID_DATA_DIR`
    print_debug "ERROR: $ERROR"
    print_debug "$DATA_DIRECTORY"
    error_found=TRUE
fi


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