#!/bin/ksh

MALL_CONFIGURATION_FILE=$1
SHOPSITE_STORE_ID=$2

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



#
#
#
#
print_abort_message()
{
  printf "\n"
  printf "\n"
  printf "Aborting installation\n"
  printf "\n"
}



case $# in
  0)   ## No parameters passed in.  Improper use of script.
       printf "No configuration files indicated.\n"
       print_abort_message
       exit 1
       ;;
  1)   ## Only a mall config file was passed in.
       ## We still need a store_id.
       printf "No store_id indicated.\n"
       print_abort_message
       exit 1
       ;;
  2)   ## A mall config file, and a store_id were passed in.
       ## Check that the mall config file actually exist.
       if ! test -e $MALL_CONFIGURATION_FILE
       then
           printf "The following mall configuration file does not exist:\n"
           printf "%s\n" $MALL_CONFIGURATION_FILE
           print_abort_message
           exit 1
       fi
       ;;
  *)   printf "Illegal number of arguents passed.\n"
       print_abort_message
       exit 1
       ;;
esac



## Prepare for logging debug info.
## First, retrieve values from mall config file.
VALUE_FOUND="FALSE"
while read -r field value
do
    if test "X$field" = "XLOG_DEBUG:"
    then
        LOG_DEBUG=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XDEBUG_LOG_DIRECTORY:"
    then
        DEBUG_LOG_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XDEBUG_LOG_FILE:"
    then
        DEBUG_LOG_FILE=$value
        VALUE_FOUND="TRUE"
    fi

    if test "$VALUE_FOUND" = "TRUE"
    then
        printf "%s %s\n" "$field" "$value"
        VALUE_FOUND="FALSE"
    fi
done < $MALL_CONFIGURATION_FILE



## Second, setup debug log file if needed.
if test "X$LOG_DEBUG" = "XYES"
then
    if ! (./setup_debug_log_file.ksh $DEBUG_LOG_DIRECTORY $DEBUG_LOG_FILE)
    then
        print_abort_message
        printf "Unable to setup the log file.\n"
        ERROR=`./error_handler.ksh $FATAL_ERROR`
        printf "ERROR: $ERROR\n"
        exit 1
    fi
fi



VALUE_FOUND="FALSE"
##  Retrieve values from the $MALL_CONFIGURATION_FILE
while read -r field value
do
    if test "X$field" == "XSHOPSITE_DIRECTORY:"
    then
        SHOPSITE_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    fi

    if test "$VALUE_FOUND" = "TRUE"
    then
        print_debug "$field $value"
        VALUE_FOUND="FALSE"
    fi
done < $MALL_CONFIGURATION_FILE



## Check that the $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa
## file actually exist.
if ! test -e $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa
then
    printf "The following store_id file does not exist:\n"
    printf "%s/%s\n" "$SHOPSITE_DIRECTORY" "$SHOPSITE_STORE_ID.aa"
    print_abort_message
    exit 1
fi



##  Retrieve values from $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa
VALUE_FOUND="FALSE"
while read -r field value
do
    if test "X$field" == "Xsb_dir:"
    then
        SHOPPING_CART_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" == "Xoutputdir:"
    then
        HTML_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" == "Xdatadir:"
    then
        DATA_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    fi

    if test "$VALUE_FOUND" = "TRUE"
    then
        print_debug "$field $value"
        VALUE_FOUND="FALSE"
    fi
done < $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa


if ! (./check_remove_store.ksh "$SHOPSITE_STORE_ID" "$SHOPSITE_DIRECTORY" "$SHOPPING_CART_DIRECTORY" "$HTML_DIRECTORY" "$DATA_DIRECTORY" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
then
    print_debug "Aborting installation:\n"
    print_debug "FAILED: ./check_remove_store.ksh"
    ERROR=`./error_handler.ksh $FATAL_ERROR`
    printf "ERROR: $ERROR\n"
    exit 1
fi


print_debug "Removing the HTML directory for store: $SHOPSITE_STORE_ID"
rm -rf $HTML_DIRECTORY

print_debug "Removing the DATA directory for store: $SHOPSITE_STORE_ID"
rm -rf $DATA_DIRECTORY

rm -f $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa
rm -f $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.auth
rm -f $SHOPPING_CART_DIRECTORY/$SHOPSITE_STORE_ID.aa
rm -f $SHOPPING_CART_DIRECTORY/$SHOPSITE_STORE_ID.auth

print_debug
print_debug
print_debug "The store removal process has finished."
print_debug "Check all previous output for possible warnings or errors."
print_debug

## Successfully completed this portion of the install.
exit 0
