#!/bin/ksh

MALL_CONFIGURATION_FILE=$1
STORE_CONFIGURATION_FILE=$2

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



#
#
#
#
print_abort_message()
{
  printf "\n"
  printf "\n"
  printf "Store installation aborted.\n"
  printf "\n"
}



case $# in
  0)   ## No parameters passed in.  Improper use of script.
       print_abort_message
       printf "No configuration files indicated.\n"
       printf "usage: start_install_store_sc.ksh config_mall.aa config_store.aa\n"
       ERROR=`./error_handler.ksh $FATAL_ERROR`
       printf "ERROR: $ERROR\n"
       exit 1
       ;;
  1)   ## Only a mall config file was passed in.
       ## We still need a store config file.
       print_abort_message
       printf "No store configuration file indicated.\n"
       printf "usage: start_install_store_sc.ksh config_mall.aa config_store.aa\n"
       ERROR=`./error_handler.ksh $FATAL_ERROR`
       printf "ERROR: $ERROR\n"
       exit 1
       ;;
  2)   ## A mall config file, and a store config file were passed in.
       ## Check that both files actually exist.
       abort="FALSE"
       if ! test -e "$MALL_CONFIGURATION_FILE"
       then
           printf "The following mall configuration file does not exist:\n"
           printf "%s\n" $MALL_CONFIGURATION_FILE
           abort="TRUE"
           ERROR=`./error_handler.ksh $FATAL_ERROR`
           printf "ERROR: $ERROR\n"
           exit 1
       fi
       if ! test -e "$STORE_CONFIGURATION_FILE"
       then
           printf "The following store configuration file does not exist:\n"
           printf "%s\n" $STORE_CONFIGURATION_FILE
           abort="TRUE"
           ERROR=`./error_handler.ksh $FATAL_ERROR`
           printf "ERROR: $ERROR\n"
           exit 1
       fi
       if test "$abort" = "TRUE"
       then
           print_abort_message
       fi
       ;;
  *)   ## Illegal number of arguments
       print_abort_message
       printf "Illegal number of arguments passed.\n"
       printf "usage: start_install_store_sc.ksh config_mall.aa config_store.aa\n"
       ERROR=`./error_handler.ksh $FATAL_ERROR`
       printf "ERROR: $ERROR\n"
       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, retrieve values from store config file.
while read -r field value
do
    if test "X$field" = "XSHOPSITE_STORE_ID:"
    then
        SHOPSITE_STORE_ID=$value
        printf "%s %s\n" "$field" "$value"
    fi
done < $STORE_CONFIGURATION_FILE

## Third, 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


## Retrieve values from mall config file.
VALUE_FOUND="FALSE"
while read -r field value
do
    if test "X$field" = "XSHOPSITE_DIRECTORY:"
    then
        SHOPSITE_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSHOPSITE_URL:"
    then
        SHOPSITE_URL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSHOPPING_CART_DIRECTORY:"
    then
        SHOPPING_CART_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSHOPPING_CART_URL:"
    then
        SHOPPING_CART_URL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XUNIX_WEB_USER_ID:"
    then
        UNIX_WEB_USER_ID=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XUNIX_WEB_GROUP_ID:"
    then
        UNIX_WEB_GROUP_ID=$value
        TRUE="TRUE"
    elif test "X$field" = "XPATH_TO_TAR:"
    then
        PATH_TO_TAR=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XPATH_TO_SENDMAIL:"
    then
        PATH_TO_SENDMAIL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSERVER_LOG_PATH:"
    then
        SERVER_LOG_PATH=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSERVER_LOG_NAME:"
    then
        SERVER_LOG_NAME=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSHOPSITE_IMAGE_DIR:"
    then
        SHOPSITE_IMAGE_DIR=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSHOPSITE_IMAGE_URL:"
    then
        SHOPSITE_IMAGE_URL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XMALL_LOCALE:"
    then
        MERCHANT_LOCALE=$value
        VALUE_FOUND="TRUE"
    fi

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

## Retrieve values from store config file.
VALUE_FOUND="FALSE"
while read -r field value
do
    if test "X$field" = "XSHOPSITE_SELLER_PASSWORD:"
    then
        SHOPSITE_SELLER_PASSWORD=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSTORE_FRONT_URL:"
    then
        STORE_FRONT_URL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSECURE_SHOPSITE_IMAGE_URL:"
    then
        SECURE_SHOPSITE_IMAGE_URL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSELLER_EMAIL:"
    then
        SELLER_EMAIL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XDATA_DIRECTORY:"
    then
        DATA_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XHTML_DIRECTORY:"
    then
        HTML_DIRECTORY=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSELLER_USER_ID:"
    then
        SELLER_USER_ID=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSELLER_GROUP_ID:"
    then
        SELLER_GROUP_ID=$value
        TRUE="TRUE"
    elif test "X$field" = "XPRODUCT_TYPE:"
    then
        PRODUCT_TYPE=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSTORE_TYPE:"
    then
        STORE_TYPE=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XPAGE_LIMIT:"
    then
        PAGE_LIMIT=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XPRODUCT_LIMIT:"
    then
        PRODUCT_LIMIT=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XUSE_SECURE_SHOPSITE:"
    then
        USE_SECURE_SHOPSITE=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSECURE_SHOPSITE_URL:"
    then
        SECURE_SHOPSITE_URL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XUSE_SECURE_SHOPPING_CART:"
    then
        USE_SECURE_SHOPPING_CART=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSECURE_SHOPPING_CART_URL:"
    then
        SECURE_SHOPPING_CART_URL=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XUPDATE_HTPASSWD:"
    then
        UPDATE_HTPASSWD=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSTORE_CURRENCY:"
    then
        STORE_CURRENCY=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XBUYER_LOCALE:"
    then
        BUYER_LOCALE=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XMERCHANT_LOCALE:"
    then
        MERCHANT_LOCALE=$value
        VALUE_FOUND="TRUE"
    elif test "X$field" = "XSTORE_LANG_PACK:"
    then
        STORE_LANG_PACK=$value
        VALUE_FOUND="TRUE"
    fi

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


if ! test "$SECURE_SHOPSITE_IMAGE_URL"
then
    SECURE_SHOPSITE_IMAGE_URL="https://localhost/shopsite-images"
fi

if ! test "$SHOPSITE_DIRECTORY"
then
    printf "Error: No value provided for SHOPSITE_DIRECTORY.\n"
    error_encountered="TRUE"
fi
if ! test "$SHOPSITE_URL"
then
    printf "Error: No value provided for SHOPSITE_URL.\n"
    error_encountered="TRUE"
fi
if ! test "$SHOPPING_CART_DIRECTORY"
then
    printf "Error: No value provided for SHOPPING_CART_DIRECTORY.\n"
    error_encountered="TRUE"
fi
if ! test "$SHOPPING_CART_URL"
then
    printf "Error: No value provided for SHOPPING_CART_URL.\n"
    error_encountered="TRUE"
fi
if ! test "$UNIX_WEB_USER_ID"
then
    printf "Error: No value provided for UNIX_WEB_USER_ID.\n"
    error_encountered="TRUE"
fi
if ! test "$UNIX_WEB_GROUP_ID"
then
    printf "Error: No value provided for UNIX_WEB_GROUP_ID.\n"
    error_encountered="TRUE"
fi
if ! test "$PATH_TO_TAR"
then
    printf "Error: No value provided for PATH_TO_TAR.\n"
    error_encountered="TRUE"
fi
if ! test "$PATH_TO_SENDMAIL"
then
    printf "Error: No value provided for PATH_TO_SENDMAIL.\n"
    error_encountered="TRUE"
fi
if ! test "$SHOPSITE_SELLER_PASSWORD"
then
    printf "Error: No value provided for SHOPSITE_SELLER_PASSWORD.\n"
    error_encountered="TRUE"
fi
if ! test "$STORE_FRONT_URL"
then
    printf "Error: No value provided for STORE_FRONT_URL.\n"
    error_encountered="TRUE"
fi
if ! test "$SELLER_EMAIL"
then
    printf "Error: No value provided for SELLER_EMAIL.\n"
    error_encountered="TRUE"
fi
if ! test "$DATA_DIRECTORY"
then
    printf "Error: No value provided for DATA_DIRECTORY.\n"
    error_encountered="TRUE"
fi
if ! test "$HTML_DIRECTORY"
then
    printf "Error: No value provided for HTML_DIRECTORY.\n"
    error_encountered="TRUE"
fi
if ! test "$SELLER_USER_ID"
then
    printf "Error: No value provided for SELLER_USER_ID.\n"
    error_encountered="TRUE"
fi
if ! test "$SELLER_GROUP_ID"
then
    printf "Error: No value provided for SELLER_GROUP_ID.\n"
    error_encountered="TRUE"
fi
if ! test "$PRODUCT_TYPE"
then
    printf "Error: No value provided for PRODUCT_TYPE.\n"
    error_encountered="TRUE"
fi
if ! test "$STORE_TYPE"
then
    printf "Error: No value provided for STORE_TYPE.\n"
    error_encountered="TRUE"
fi
if ! test "$PAGE_LIMIT"
then
    PAGE_LIMIT="-1"
fi
if ! test "$PRODUCT_LIMIT"
then
    PRODUCT_LIMIT="-1"
fi

if ! test "$STORE_CURRENCY"
then
    STORE_CURRENCY="USD"
fi

if ! test "$MERCHANT_LOCALE"
then
    MERCHANT_LOCALE="en-US"
fi

if ! test "$BUYER_LOCALE"
then
    BUYER_LOCALE="en-US"
fi

if ! test "$UPDATE_HTPASSWD"
then
    UPDATE_HTPASSWD="YES"
fi

if test "$error_encountered"
then
    printf "Values must be provided for the previously listed\n"
    printf "variables in the config_mall.aa file\n"
    print_abort_message
    ERROR=`./error_handler.ksh $FATAL_ERROR`
    printf "ERROR: $ERROR\n"
    exit 1
fi



#  Obtain an auth file if one doesn't already exist.
if  ! test -e "./${SHOPSITE_STORE_ID}.auth"
then
    if  test -e "./get_auth.ksh"
    then
        case $PRODUCT_TYPE in
          exp)   ## Express store.
                 PRODUCT_NUMBER="03"
                 ;;
          lte)   ## Starter store.
                 PRODUCT_NUMBER="04"
                 ;;
          mgr)   ## Manager store.
                 PRODUCT_NUMBER="01"
                 ;;
          pro)   ## Pro store.
                 PRODUCT_NUMBER="02"
                 ;;
            *)   ## Illegal store type.
                 print_debug "Aborting installation:"
                 print_debug "FAILED: ./start_install_store_sc.ksh"
                 print_debug "Invalid product type: $PRODUCT_TYPE"
                 print_debug "expected a value of exp|lte|mgr|pro"
                 print_debug "Modify the variable PRODUCT_TYPE in the"
                 print_debug "config_store.aa file."
                 ERROR=`./error_handler.ksh $FATAL_ERROR`
                 printf "ERROR: $ERROR\n"
                 exit 1
                 ;;
        esac

        if ! (./get_auth.ksh "$STORE_FRONT_URL" "$SHOPSITE_STORE_ID" "$SELLER_EMAIL" "$STORE_TYPE" "$PRODUCT_NUMBER" "$PRODUCT_LIMIT" "$PAGE_LIMIT")
        then
            print_debug "Aborting installation:"
            print_debug "FAILED: ./get_auth.ksh"
            ERROR=`./error_handler.ksh $FATAL_ERROR`
            printf "ERROR: $ERROR\n"
            exit 1
        fi
    fi
fi



##  Check user input to make sure it makes sense and is do-able.
if ! (./check_setup_store_sc.ksh "INSTALL" "$SHOPSITE_DIRECTORY" "$SHOPPING_CART_DIRECTORY" "$SHOPSITE_STORE_ID" "$DATA_DIRECTORY" "$HTML_DIRECTORY" "$SELLER_USER_ID" "$SELLER_GROUP_ID" "$UNIX_WEB_USER_ID" "$UNIX_WEB_GROUP_ID" "$PATH_TO_TAR" "$PATH_TO_SENDMAIL" "$LOG_DEBUG" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE")
then
    print_debug "Aborting installation:"
    print_debug "FAILED: ./check_setup_store_sc.ksh"
    ERROR=`./error_handler.ksh $FATAL_ERROR`
    printf "ERROR: $ERROR\n"
    exit 1
fi


##  Put all ShopSite store files in place.
if ! (./install_store_sc.ksh "$SHOPSITE_DIRECTORY" "$SHOPSITE_URL" "$SHOPPING_CART_DIRECTORY" "$SHOPPING_CART_URL" "$SHOPSITE_STORE_ID" "$STORE_FRONT_URL" "$DATA_DIRECTORY" "$HTML_DIRECTORY" "$SELLER_USER_ID" "$SELLER_GROUP_ID" "$UNIX_WEB_USER_ID" "$UNIX_WEB_GROUP_ID" "$PATH_TO_TAR" "$SELLER_EMAIL" "$PATH_TO_SENDMAIL" "$SERVER_LOG_PATH" "$SERVER_LOG_NAME" "$USE_SECURE_SHOPSITE" "$SECURE_SHOPSITE_URL" "$USE_SECURE_SHOPPING_CART" "$SECURE_SHOPPING_CART_URL" "$STORE_CURRENCY" "$MERCHANT_LOCALE" "$BUYER_LOCALE" "$SHOPSITE_IMAGE_DIR" "$SHOPSITE_IMAGE_URL" "$STORE_LANG_PACK" "$SECURE_SHOPSITE_IMAGE_URL" "$DEBUG_LOG_DIRECTORY" "$DEBUG_LOG_FILE" "$LOG_DEBUG")
then
    print_debug "Aborting installation:"
    print_debug "FAILED: ./install_store_sc.ksh"
    ERROR=`./error_handler.ksh $FATAL_ERROR`
    printf "ERROR: $ERROR\n"
    exit 1
fi


##  Set the username and password for this store.
if test $UPDATE_HTPASSWD = "YES"
   then
     if ! (./set_password.ksh $SHOPSITE_DIRECTORY $SHOPSITE_STORE_ID $SHOPSITE_SELLER_PASSWORD $DEBUG_LOG_DIRECTORY $DEBUG_LOG_FILE $LOG_DEBUG)
     then
       print_debug "Aborting installation:"
       print_debug "FAILED: ./set_password.ksh"
       ERROR=`./error_handler.ksh $FATAL_ERROR`
       printf "ERROR: $ERROR\n"
       exit 1
     fi
fi

#uncomment this to automatically copy the config_store.aa into the data dir
#cp ${STORE_CONFIGURATION_FILE} ${DATA_DIRECTORY}/config_store.aa

print_debug ""
print_debug ""
print_debug "This portion of the store installation process has finished."
print_debug "Check all previous screen output for possible warnings or errors."
print_debug ""

##  Successfully completed.
exit 0







