#!/bin/bash
#
# Bash Script for checking config version and execute python only on version mismatch
# Description: This script reads the config file from environment variable ECP_WORK_FILES_DIR and the base config
# file in /opt/ECP/config with jq and compares their version string. If they match no work is done. However, if they
# do not match the python script migrateConfig is called
#
# Requirements: - Config file and base config file are in their respective path
#               - jsonpatch and jsonpatchext is installed for the python script
#

################################################################################
# Variables                                                                    #
################################################################################
UPLOAD_PATH="/mnt/persistent-data/uploads"
UPLOAD_CONFIG_FILE_NAME="uploaded_config.json"
CONFIG_ROLES="DCMS METER POWERSINK STORAGE VEHICLE"
DO_MIGRATION=0
JSON_KEY_VERSION='._version'
BASE_JSON_KEY_VERSION='._version.Value'
CONFIG_PATH="$ECP_WORK_FILES_DIR/config/config.json"
DEPRECATED_CONFIG_PATH="$ECP_WORK_FILES_DIR/config/deprecated_config.json"
BASE_CONFIG_PATH="$PYTHONPATH/config/data/BASE_CONFIG.json"

################################################################################
# Functions                                                                    #
################################################################################
HandleUploadedConfig()
{
  # Install Chargeboxcontroller config from uploads to the definitive directory
  if [[ -f "${UPLOAD_PATH}/${UPLOAD_CONFIG_FILE_NAME}" ]]; then
        mv "$UPLOAD_PATH/${UPLOAD_CONFIG_FILE_NAME}" "$ECP_WORK_FILES_DIR/config/config.json"
        echo "Installed config: config.json in: ${ECP_WORK_FILES_DIR}/config/"
  fi

  # Install any other role config from uploads to the definitive directory
  for role in $CONFIG_ROLES; do
    if [[ -f "${UPLOAD_PATH}/${role}_${UPLOAD_CONFIG_FILE_NAME}" ]]; then
        mv "$UPLOAD_PATH/${role}_${UPLOAD_CONFIG_FILE_NAME}" "$ECP_WORK_FILES_DIR/config/${role}_config.json"
        echo "Installed config: ${role}_config.json in: ${ECP_WORK_FILES_DIR}/config/"
    fi
  done
  sync
}

CreateDirectories()
{
  # create folder when it does not exist, for example after starting new image
  if [[ ! -d $ECP_WORK_FILES_DIR/config ]]; then
    echo "Create $ECP_WORK_FILES_DIR/config directory and change permission for DMS"
    mkdir $ECP_WORK_FILES_DIR/config -p
    chown -R xfc:xfc $ECP_WORK_FILES_DIR/config/  # make the folder visible for DMS server
  fi
  if [[ ! -d $ECP_WORK_FILES_DIR/log ]]; then
    echo "Create $ECP_WORK_FILES_DIR/log directory and change permission for DMS"
    mkdir $ECP_WORK_FILES_DIR/log -p
    chown -R xfc:xfc $ECP_WORK_FILES_DIR/log/  # make the folder visible for DMS server
  fi
}

CheckConfigVersion()
{
  # Check version differences for roles
  for role in $CONFIG_ROLES; do
    if [[ -f "$ECP_WORK_FILES_DIR/config/${role}_config.json" ]]; then
      echo "Check config version for $role"
      configPath="$ECP_WORK_FILES_DIR/config/${role}_config.json"
      version=$(jq $JSON_KEY_VERSION $configPath | tr -d \")
      echo "Config $role version $version"

      baseConfigExtension="_BASE_CONFIG.json"
      baseConfigPath="$PYTHONPATH/config/data/$role$baseConfigExtension"
      baseversion=$(jq $BASE_JSON_KEY_VERSION $baseConfigPath | tr -d \")
      echo "Base config $role version $baseversion"

      if [ "$version" != "$baseversion" ]; then
        echo "Versions $role are not equal. Trigger migration."
        DO_MIGRATION=1
      fi
    fi
  done

  if [[ -f $BASE_CONFIG_PATH ]]; then
    echo "Base config exist check config version"

    if [[ -f $CONFIG_PATH ]]; then
      # Check config version of Chargebox
      version=$(jq $JSON_KEY_VERSION $CONFIG_PATH | tr -d \")
      echo "Config version $version"
    else
      echo "Config does not exist."
    fi

    baseversion=$(jq $BASE_JSON_KEY_VERSION $BASE_CONFIG_PATH | tr -d \")
    echo "Base config version $baseversion"

    if [ "$version" != "$baseversion" ]; then
      echo "Versions are not equal. Trigger migration."
      DO_MIGRATION=1
    fi
  fi
}

ModifyFilePermissions()
{
  for role in $CONFIG_ROLES; do
    if [[ -f "$ECP_WORK_FILES_DIR/config/${role}_config.json" ]]; then
      echo "${role}_config.json modify permissions"
      chown xfc:xfc "$ECP_WORK_FILES_DIR/config/${role}_config.json" # make the file visible to the dms server
      chmod 664 "$ECP_WORK_FILES_DIR/config/${role}_config.json" # grant permission to the groups for write
    fi

    if [[ -f "$ECP_WORK_FILES_DIR/config/${role}_deprecated_config.json" ]]; then
      echo "${role}_deprecated_config.json modify permissions"
      chown xfc:xfc "$ECP_WORK_FILES_DIR/config/${role}_deprecated_config.json" # make the file visible to the dms server
      chmod 664 "$ECP_WORK_FILES_DIR/config/${role}_deprecated_config.json" # grant permission to the groups for write
    fi
  done

  echo "config.json modify permissions"
  if [[ -f $CONFIG_PATH ]]; then
    chown xfc:xfc $CONFIG_PATH # make the file visible to the dms server
    chmod 664 $CONFIG_PATH # grant permission to the groups for write
  fi
  if [[ -f $DEPRECATED_CONFIG_PATH ]]; then
    chown xfc:xfc $DEPRECATED_CONFIG_PATH # make the file visible to the dms server
    chmod 664 $DEPRECATED_CONFIG_PATH # grant permission to the groups for write
  fi
  sync
}

HandleUploadedConfig
CreateDirectories
CheckConfigVersion

if [ $DO_MIGRATION -gt 0 ]; then
    echo "Start migration"
    /usr/bin/python3 $PYTHONPATH/config/migration/migrateConfig*
fi

ModifyFilePermissions
