#!/bin/ksh

STORE_CONFIGURATION_FILE=$1


#
#
#
#
print_abort_message()
{
  printf "\n"
  printf "\n"
  printf "Auth file upgrade aborted.\n"
  printf "\n"
}



case $# in
  1)   ## A store config file was passed in.
       ## Check that file actually exists.
       abort="FALSE"
       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
       ;;
  *)   ## Wrong number of parameters passed in. Improper use of script.
       printf "No config_store.aa file indicated.\n"
       printf "usage: upgrade_auth_file config_store.aa\n"
       ERROR=`./error_handler.ksh $FATAL_ERROR`
       printf "ERROR: $ERROR\n"
       exit 1
       ;;
esac

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

## 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"
    if test "X$field" = "XSTORE_FRONT_URL:"
    then
        STORE_FRONT_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"
    fi
#    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" = "XSHOPSITE_STORE_CURRENCY_SYMBOL:"
#    then
#        SHOPSITE_STORE_CURRENCY_SYMBOL=$value
#        VALUE_FOUND="TRUE"
#    fi
#    elif test "X$field" = "XTRANSACT_STORE_ID:"
#    then
#        TRANSACT_STORE_ID=$value
#        VALUE_FOUND="TRUE"
#    fi

    if test "$VALUE_FOUND" = "TRUE"
    then
        VALUE_FOUND="FALSE"
    fi
done < $STORE_CONFIGURATION_FILE


#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

# Default currency to dollar sign
#if ! test "$SHOPSITE_STORE_CURRENCY_SYMBOL"
#then
#    SHOPSITE_STORE_CURRENCY_SYMBOL="$"
#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"
    exit 1
fi


#  Obtain an auth file if one doesn't already exist.
    if  test -e "./get_auth
.ksh"
    then
        case $PRODUCT_TYPE in
          exp)   ## Express store.
                 PRODUCT_NUMBER="03"
                 ;;
          lte)   ## Lite store.
                 PRODUCT_NUMBER="04"
                 ;;
          mgr)   ## Manager store.
                 PRODUCT_NUMBER="01"
                 ;;
          pro)   ## Pro store.
                 PRODUCT_NUMBER="02"
                 ;;
            *)   ## Illegal store type.
                 printf "Invalid product type: $PRODUCT_TYPE"
                 printf "expected a value of exp|lte|mgr|pro"
                 printf "Modify the variable PRODUCT_TYPE in the"
                 printf "config_store.aa file."
                 exit 1
                 ;;
        esac

        if ! (./get_auth.ksh "$STORE_FRONT_URL" "$SHOPSITE_STORE_ID" "$SELLER_EMAIL" "$STORE_TYPE" "$PRODUCT_NUMBER" "$PRODUCT_LIMIT" "$PAGE_LIMIT" "$TRANSACT_STORE_ID")
        then
            printf "Upgrading the auth file was not successful!"
            exit 1
        fi
    fi

mv ${SHOPSITE_STORE_ID}.auth ${DATA_DIRECTORY}/${SHOPSITE_STORE_ID}.auth 

##  Successfully completed.
exit 0