#!/bin/ksh

#
# set up internal parameters
#

SHOPSITE_DIRECTORY=$1
SHOPSITE_STORE_ID=$2
DATA_DIRECTORY=$3
HTML_DIRECTORY=$4
SELLER_USER_ID=$5
SELLER_GROUP_ID=$6
UNIX_WEB_USER_ID=$7
UNIX_WEB_GROUP_ID=$8
DEBUG_LOG_DIRECTORY=$9
DEBUG_LOG_FILE=${10}
LOG_DEBUG=${11}

#
# Functions
#



#
# 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_debug "SHOPSITE_DIRECTORY: $SHOPSITE_DIRECTORY"
print_debug "SHOPSITE_STORE_ID: $SHOPSITE_STORE_ID"
print_debug "DATA_DIRECTORY: $DATA_DIRECTORY"
print_debug "HTML_DIRECTORY: $HTML_DIRECTORY"
print_debug "SELLER_USER_ID: $SELLER_USER_ID"
print_debug "SELLER_GROUP_ID: $SELLER_GROUP_ID"
print_debug "UNIX_WEB_USER_ID: $UNIX_WEB_USER_ID"
print_debug "UNIX_WEB_GROUP_ID: $UNIX_WEB_GROUP_ID"
print_debug "DEBUG_LOG_DIRECTORY: $DEBUG_LOG_DIRECTORY"
print_debug "DEBUG_LOG_FILE: $DEBUG_LOG_FILE"
print_debug "LOG_DEBUG: $LOG_DEBUG"


##############################################
#########                            #########
#########  Actual code starts here.  #########
#########                            #########
##############################################
#
#
#




#  Set unix user id and group id on all files.
if test -e $SHOPSITE_DIRECTORY/start.cgi
then
    if test -e $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa
    then
        if ! chown -h $UNIX_WEB_USER_ID:$UNIX_WEB_GROUP_ID $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa
        then
            print_debug "Warning: Could not change ownership to $UNIX_WEB_USER_ID:$UNIX_WEB_GROUP_ID"
            print_debug "         on the following file:"
            print_debug "         $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.auth"
            print_debug ""
        fi
    fi

    #  Set correct file permissions.
    if ! chmod 664 ${SHOPSITE_DIRECTORY}/${SHOPSITE_STORE_ID}.aa
    then
        print_debug "Warning: Could not change permissions to 664"
        print_debug "         on the following file:"
        print_debug "         $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa"
        print_debug ""
    fi
else
    print_debug "Warning: The following is not a valid ShopSite directory:"
    print_debug "         $SHOPSITE_DIRECTORY"
    print_debug ""
fi


if test -e ${HTML_DIRECTORY}
then
    if ! chown -Rh $SELLER_USER_ID:$SELLER_GROUP_ID $HTML_DIRECTORY
    then
        print_debug "Warning: Could not change ownership to $SELLER_USER_ID:$SELLER_GROUP_ID"
        print_debug "         on or in the following directory:"
        print_debug "         $HTML_DIRECTORY"
        print_debug ""
    fi
    if ! chmod -R 771 ${HTML_DIRECTORY}
    then
        print_debug "Warning: Could not change permissions to 771"
        print_debug "         on or in the following directory:"
        print_debug "         $HTML_DIRECTORY"
        print_debug ""
    fi
    if ! chmod -R 774 ${HTML_DIRECTORY}/media
    then
        print_debug "Warning: Could not change permissions to 660"
        print_debug "         on the following files:"
        print_debug "         $HTML_DIRECTORY/media"
        print_debug ""
    fi
    if ! chmod 664 ${HTML_DIRECTORY}/index.html
    then
        print_debug "Warning: Could not change permissions to 664"
        print_debug "         on the following file:"
        print_debug "         $HTML_DIRECTORY/index.html"
        print_debug ""
    fi
else
    print_debug "Warning: The following does not appear to be an HTML directory:"
    print_debug "         $HTML_DIRECTORY"
    print_debug ""
fi


if test -e ${DATA_DIRECTORY}/ip
then
    if test -e $DATA_DIRECTORY/$SHOPSITE_STORE_ID.auth
    then
        if ! chown $UNIX_WEB_USER_ID:$UNIX_WEB_GROUP_ID $DATA_DIRECTORY/$SHOPSITE_STORE_ID.auth
        then
            print_debug "Warning: Could not change ownership to $UNIX_WEB_USER_ID:$UNIX_WEB_GROUP_ID"
            print_debug "         on the following file:"
            print_debug "         $SHOPSITE_DIRECTORY/$SHOPSITE_STORE_ID.aa"
            print_debug ""
        fi
    fi
    if ! chown -Rh $UNIX_WEB_USER_ID:$UNIX_WEB_GROUP_ID $DATA_DIRECTORY
    then
        print_debug "Warning: Could not change ownership to $UNIX_WEB_USER_ID:$UNIX_WEB_GROUP_ID"
        print_debug "         on or in the following directory:"
        print_debug "         $DATA_DIRECTORY"
        print_debug ""
    fi
    if ! chmod 775 ${DATA_DIRECTORY}
    then
        print_debug "Warning: Could not change permissions to 775"
        print_debug "         on the following directory:"
        print_debug "         $DATA_DIRECTORY"
        print_debug ""
    fi
    if ! chmod 664 ${DATA_DIRECTORY}/*
    then
        print_debug "Warning: Could not change permissions to 664"
        print_debug "         on or in the following directory:"
        print_debug "         $DATA_DIRECTORY"
        print_debug ""
    fi
    if ! chmod 775 ${DATA_DIRECTORY}/log
    then
        print_debug "Warning: Could not change permissions to 775"
        print_debug "         on the following directory:"
        print_debug "         $DATA_DIRECTORY/log"
        print_debug ""
    fi
    if ! chmod 775 ${DATA_DIRECTORY}/ip
    then
        print_debug "Warning: Could not change permissions to 775"
        print_debug "         on the following directory:"
        print_debug "         $DATA_DIRECTORY/ip"
        print_debug ""
    fi
    if ! chmod 775 ${DATA_DIRECTORY}/moreip
    then
        print_debug "Warning: Could not change permissions to 775"
        print_debug "         on the following directory:"
        print_debug "         $DATA_DIRECTORY/moreip"
        print_debug ""
    fi
    if ! chmod 775 ${DATA_DIRECTORY}/stats
    then
        print_debug "Warning: Could not change permissions to 775"
        print_debug "         on the following directory:"
        print_debug "         $DATA_DIRECTORY/stats"
        print_debug ""
    fi
    if ! chmod 664 ${DATA_DIRECTORY}/stats/*
    then
        print_debug "Warning: Could not change permissions to 775"
        print_debug "         on the files in the following directory:"
        print_debug "         $DATA_DIRECTORY/stats"
        print_debug ""
    fi
    if ! chmod 775 ${DATA_DIRECTORY}/stats/summaries
    then
        print_debug "Warning: Could not change permissions to 775"
        print_debug "         on the following directory:"
        print_debug "         $DATA_DIRECTORY/stats/summaries"
        print_debug ""
    fi
else
    print_debug "Warning: The following does not appear to be a DATA directory:"
    print_debug "         $DATA_DIRECTORY"
    print_debug ""
fi
