#!/bin/bash
#############################################################################
# Copyright (c) 2026 Broadcom Inc. and its subsidiaries. All Rights Reserved.
#
# Uses liquibase to perform db actions
#############################################################################
#

# check java home
if [ -z "${SSG_JAVA_HOME}" ] ; then
    echo "Java is not configured for gateway."
    exit 13
fi
if [ ! -x "${SSG_JAVA_HOME}/bin/java" ] ; then
    echo "Java not found: ${SSG_JAVA_HOME}"
    exit 13
fi

# Check the arguments
args=$(getopt -l "help" -l "type:" -l "url:" -l "changeLogFile:" -l "username:" -l "password:" -l "command:" -o "t:u:p::c:h" -- "$@")
eval set -- "$args"

while [ $# -ge 1 ]; do
    case "$1" in
        --)
            # No more options left.
            shift
            break
           ;;
        -h|--help)
            echo "Usage: $0 --type=[mysql|derby] --url='jdbc:mysql://localhost:3306/ssg?useSSL=false' --changeLogFile='filepath' -u 'username' [--password 'userpass']"
            exit 0
            ;;
        -t|--type)
            type="$2"
            shift
            ;;
        --url)
            url="$2"
            shift
            ;;
        --changeLogFile)
            changeLogFile="$2"
            shift
            ;;
        -u|--username)
            username="$2"
            shift
            ;;
        -p|--password)
            password="$2"
            shift
            ;;
        -c|--command)
            command="$2"
            shift
            ;;
        *)
            command="$2"
            shift

    esac
    shift
done

if [[ -z "$type" ]] 
  then 
    type="mysql"
fi
if [[ -z "$url" ]]
  then 
    url="jdbc:mysql://localhost:3306/ssg?useSSL=false"
fi
if [[ -z "$command" ]]
  then 
    command="update"
fi
if [[ -z "$changeLogFile" ]]
  then 
    echo "Missing changeLogFile. Supply a changeLogFile using the --changeLogFile option"
    exit 0
fi
if [[ -z "$username" ]]
  then 
    echo "Missing username. Supply a user name using the -u option"
    exit 0
fi    
if [[ -z "$password" ]]
  then 
    read -s -p "Enter Password: " password
    echo ""
fi    
    
# Detect install directory
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
INSTALL_DIR="${CURRENT_DIR}/../.."

LIQUIBASE_TEMP_DIR="/tmp/liquibase"

export LIQUIBASE_HOME="${LIQUIBASE_TEMP_DIR}"

LIQUIBASE_JAR="${LIQUIBASE_TEMP_DIR}/internal/lib/liquibase-core-4.25.1.jar"
# Create temporary directory then copy the liquibase jar and its dependencies to the directory
mkdir -p "${LIQUIBASE_TEMP_DIR}/lib"
mkdir -p "${LIQUIBASE_TEMP_DIR}/internal/lib"
cp "${INSTALL_DIR}/runtime/lib/liquibase-core-4.25.1.jar" "${LIQUIBASE_TEMP_DIR}/internal/lib"
cp "${INSTALL_DIR}/runtime/lib/slf4j-api-1.7.28.jar" "${LIQUIBASE_TEMP_DIR}/lib"
cp "${INSTALL_DIR}/config/etc/db/lib/logback-classic-1.2.13.jar" "${LIQUIBASE_TEMP_DIR}"
cp "${INSTALL_DIR}/config/etc/db/lib/logback-core-1.2.13.jar" "${LIQUIBASE_TEMP_DIR}/lib"
cp "${INSTALL_DIR}/runtime/lib/picocli-4.7.5.jar" "${LIQUIBASE_TEMP_DIR}/lib"
cp "${INSTALL_DIR}/runtime/lib/snakeyaml-2.3.jar" "${LIQUIBASE_TEMP_DIR}/lib"
cp "${INSTALL_DIR}/runtime/lib/opencsv-5.8.jar" "${LIQUIBASE_TEMP_DIR}/lib"

MYSQL_JAR="${INSTALL_DIR}/runtime/lib/mysql-connector-j-8.4.0.jar"
DERBY_JAR="${INSTALL_DIR}/runtime/lib/derby-10.17.1.0.jar"

case "$type" in 
    "mysql")
        DB_JAR=${MYSQL_JAR}
        DB_DRIVER="com.mysql.cj.jdbc.Driver"
        ;;
    "derby")
        DB_JAR=${DERBY_JAR}
        DB_DRIVER="org.apache.derby.jdbc.EmbeddedDriver"       
        ;;
    *)
        echo "Unknown database type. Given '${type}', expected either 'mysql' or 'derby'"
        exit 0
esac

changeLogFileName=$(basename ${changeLogFile})
changeLogFileDir=$(dirname ${changeLogFile})

pushd $changeLogFileDir > /dev/null
liquibaseOut=$( "${SSG_JAVA_HOME}/bin/java" -jar ${LIQUIBASE_JAR} --classpath=${DB_JAR} --driver=${DB_DRIVER} --changeLogFile=${changeLogFileName} --url=${url} --username=${username} --password=${password} ${command} 2>&1 )
popd > /dev/null

echo "${liquibaseOut}"

# Remove temporary directory
rm -rf "${LIQUIBASE_TEMP_DIR}"

# check if the response was successful
echo "${liquibaseOut}" | grep "Successful" -iq
# This will respond with 0 if "Successful" is contained in the liquibase response. It will respond with 1 otherwise.
exit $?
