#!/bin/bash

# Read the value of the variable from file
source /usr/local/omc/etc/omc.conf

C_ARG_LOWER="omc"
C_ARG_UPPER="OMC"
M_ARG_LOWER="*"

check_args() {
    while getopts "c:m:" option; do
        case $option in
            c)  
                C_ARG_LOWER=$(echo $OPTARG | tr '[:upper:]' '[:lower:]')
                C_ARG_UPPER=$(echo $OPTARG | tr '[:lower:]' '[:upper:]')
                # Modifying Script Variables
                if [ "${C_ARG_LOWER}" == "ba" ]; then
                        C_ARG_UPPER="BA"
                        sed -i 's/VENDORS=.*/VENDORS=BA/' /usr/local/omc/etc/omc.conf
                elif [ "${C_ARG_LOWER}" == "omc" ]; then
                        C_ARG_UPPER="OMC"
                        sed -i 's/VENDORS=.*/VENDORS=OMC/' /usr/local/omc/etc/omc.conf
                elif [ "${C_ARG_LOWER}" == "agt" ]; then
                        C_ARG_UPPER="AGT"
                        sed -i 's/VENDORS=.*/VENDORS=AGT/' /usr/local/omc/etc/omc.conf
                fi
                ;;  
            m)  
                M_ARG=$(echo $OPTARG | tr '[:upper:]' '[:lower:]')
                if [ "${VENDORS}" == "BA" ]; then
                    C_ARG_LOWER="ba"
                    C_ARG_UPPER="BA"
                fi
                ;;  
            \?) 
                echo "Invalid option: -$OPTARG" >&2
                ;;  
        esac
   done
}

USER="root"
PASSWORD="1000omc@kp!"
PORT="33066"
DBNAME="psap_db"
OMCRootDir=/usr/local/omc
OMCBinDir=${OMCRootDir}/bin
UpgradeSQLDir=${OMCRootDir}/etc/db/upgrade
Upgvue3SQLDir=${OMCRootDir}/etc/db/upgvue3
InstallSQLDir=${OMCRootDir}/etc/db/install
OMCStaticDir=${OMCRootDir}/static

check_args "$@"

case "${M_ARG}" in
    install)
        ${OMCBinDir}/importdb.sh ${M_ARG}

        if [ "${C_ARG_LOWER}" != "" ]; then
            CustomizedDir=${OMCStaticDir}/${C_ARG_LOWER}.d
            if [ ! -d "${CustomizedDir}" ]; then
                echo "Not found ${C_ARG_UPPER} customized directory, nothing to be done"
                exit 1
            fi  
            echo -n "Setting ${C_ARG_UPPER} customized PSAP ..."
            for SQL in ${CustomizedDir}/db/*.sql; do  
                mysql -u${USER} -p${PASSWORD} -P ${PORT} --protocol tcp -D ${DBNAME} < ${SQL}; 
            done
            cp -rf ${CustomizedDir}/logo/* ${OMCStaticDir}/logo
            cp -rf ${CustomizedDir}/doc/* ${OMCStaticDir}/helpDoc
            if [ "${C_ARG_LOWER}" == "ba" ]; then
                rm -rf ${OMCStaticDir}/logo/zh_*
                rm -rf ${OMCStaticDir}/helpDoc/zh_*
            fi
            #perl -0777 -i -pe 's/omcuser/bluearcus/g' ${OMCRootDir}/etc/default/restconf.yaml
            #perl -0777 -i -pe 's/omcuser/bluearcus/g' ${OMCBinDir}/nehosts
            if [ $? = 0 ]; then
               	echo "done" 
            fi 
        fi       
        ;;
    upgrade | upgvue3)
        ${OMCBinDir}/importdb.sh ${M_ARG}
        ;;
    skip)
        if [ "${C_ARG_LOWER}" != "" ]; then
            CustomizedDir=${OMCStaticDir}/${C_ARG_LOWER}.d
            if [ ! -d "${CustomizedDir}" ]; then
                echo "Not found ${C_ARG_UPPER} customized directory, nothing to be done"
                exit 1
            fi  
            echo -n "Setting ${C_ARG_UPPER} customized PSAP ..."
            for SQL in ${CustomizedDir}/db/*.sql; do  
                mysql -u${USER} -p${PASSWORD} -P ${PORT} --protocol tcp -D ${DBNAME} < ${SQL}; 
            done
            cp -rf ${CustomizedDir}/logo/* ${OMCStaticDir}/logo
            cp -rf ${CustomizedDir}/doc/* ${OMCStaticDir}/helpDoc
            if [ "${C_ARG_LOWER}" == "ba" ]; then
                rm -rf ${OMCStaticDir}/logo/zh_*
                rm -rf ${OMCStaticDir}/helpDoc/zh_*
            fi            
            if [ $? = 0 ]; then
               	echo "done" 
            fi 
        fi
        ;;    
    *)
        ${OMCBinDir}/importdb.sh
        ;;
esac

exit 0