#!/bin/sh
# ALTRAN_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# 61ksys170 src/ksys/usr/ui/uidev/vmrui-server/server/bin/dependencyUpdate.ksh, ksys.ui, 61vmrg170 1.7.0.0
#
# Licensed Materials - Property of Altran ACT S.A.S.
#
# Restricted Materials of Altran ACT S.A.S.
#
# Copyright (C) Altran ACT S.A.S. 2023
# All Rights Reserved
#
# ALTRAN_PROLOG_END_TAG

###############################################################################
#
# Name:        dependencyUpdate.ksh
#
# Description: A script to update the Node JS package dependency. This script performs two main operations:
#
#                  - Update the dependency package version in package.json files under different location as needed.
#                  - Extract, rename and then Upload the downloaded dependency zip file  under different location
#
#              When this script completes successfully, Node JS Dependency Package updated, and the VMRUI server
#              should be running and usable.
#
# NOTE: All this is so that customers, who will already be annoyed at having
#       to run this script at all, will at least be able to run it from just
#       about anywhere (to do downloads only), and still enjoy full translated
#       messages.
#
################################################################################

PATH="/bin:/usr/bin:/sbin:/usr/local/bin:/etc"
#export PATH

###############################################################################
#
# Name:        update_package_and_dependency
#
# Description: Checks to see if the requisite files have already been
#              downloaded.
#
# Inputs:      allPackageFileLocations    A global variable that is set in main and
#                         contains all the package.json file location.
#
# Returns:     No explicit return value.
#
###############################################################################
function update_package_and_dependency
{
    (( DEBUG )) && set -x

    typeset PACKAGE_NAME="moment-timezone" OLD_PACKAGE_VERSION="0.5.32" NEW_PACKAGE_VERSION="0.5.40" PACKAGE_ZIP_FILE_NAME="moment-timezone-develop"
    typeset -i COUNT=0

    for DATA in ${allPackageFileLocations[@]}; do
        : ================
        if cat "${DATA}package.json" | grep "\"$PACKAGE_NAME\": \"$OLD_PACKAGE_VERSION\""
            then
                # package version update in package.json file start 
                eval "sed 's/\"${PACKAGE_NAME}\": \"${OLD_PACKAGE_VERSION}\"/\"${PACKAGE_NAME}\": \"${NEW_PACKAGE_VERSION}\"/'" "${DATA}package.json" > "${DATA}modifiedPackage.json"
                mv ${DATA}modifiedPackage.json ${DATA}package.json
                printf "\npackage version updated in ${DATA}package.json . \n"
                # package version update in package.json file end 

                # package version update under node_module folder for corresponding dependency start
                unzip -d "${DATA}node_modules/" "${PACKAGE_ZIP_FILE_NAME}.zip"
                printf "\nUnzip of \"${PACKAGE_ZIP_FILE_NAME}.zip\" done successfully.\n"

                rm -rf ${DATA}node_modules/$PACKAGE_NAME
                printf "\nOld dependency folder with \"${PACKAGE_NAME}\" deleted successfully.\n"

                mv "${DATA}node_modules/$PACKAGE_ZIP_FILE_NAME" "${DATA}node_modules/${PACKAGE_NAME}"
                printf "\nRename of \"${PACKAGE_ZIP_FILE_NAME}\" to \"${PACKAGE_NAME}\" done successfully .\n"
                # package version update under node_module folder for corresponding dependency end
            else
                printf "\nPackage you want to update not present in ${DATA}package.json\n"    
        fi
    done
} # End of "update_package_and_dependency()"

######################################################################
######################################################################
##
##  MAIN Main main
##
######################################################################
######################################################################
trap trap_func INT

#
# Declare and initialize variables
#
typeset -i DEBUG=0 idx=0

# default is to update package dependency
typeset update_package_flag=true
typeset PLATFORM="AIX"

#
# These are the package.json files under different Location.
#
typeset allPackageFileLocations
typeset DATA=""
if [[ $PLATFORM == "AIX" ]]
then
    for DATA in \
        "/opt/IBM/ksys/ui/server/" \
        "/opt/IBM/ksys/ui/server/node_modules/vmrui-common/" \
        "/opt/IBM/ksys/ui/agent/" \
        "/opt/IBM/ksys/ui/agent/node_modules/vmrui-common/"
    do
        allPackageFileLocations[$idx]="$DATA"
        (( idx = $idx + 1 ))
    done
fi

if [[ $update_package_flag == true ]]; then
    update_package_and_dependency
fi


