#!/bin/bash

KEY_FILE_FLAG=false
CONFIG_FILE_FLAG=false
AUTH_CONFIG_FILE_FLAG=false
AUTH_N_CONFIG_FILE_FLAG=false
ECKT_CONFIG_FILE_FLAG=false

CONFIG_DIR="$WORK_ROOT/token-generator-config"

HOST_CONFIG_DIR="/home/root/token-generator-config"
HOST_DATA_DIR="/home/root/token-generator-data"


usage () { echo "How to use: generate_token [-h] [-k KEY_FILE] [-C CONFIG_FILE] [-a AUTH_CONFIG_FILE] [-n AUTH_N_CONFIG_FILE] [-e ECKT_CONFIG_FILE] -- script to generate token

where:
      -h show this help message
      -k KEY_FILE key file name for which a token will be generated
      -c CONFIG_FILE optional config file name
      -a AUTH_CONFIG_FILE optional auth config file name
      -n AUTH_N_CONFIG_FILE optional authN config file name
      -e ECKT_CONFIG_FILE optional eckt config file name

"; }

# Existing options are [-k] [-c] [-a] [-n] [-e] [-h]
options='k:c:a:n:e:h'

# Retrieve command line arguments
while getopts $options option
do
 case ${option} in
    k   ) KEY_FILE=${OPTARG}; KEY_FILE_FLAG=true;;
    c   ) CONFIG_FILE=${OPTARG}; CONFIG_FILE_FLAG=true;;
    a   ) AUTH_CONFIG_FILE=${OPTARG}; AUTH_CONFIG_FILE_FLAG=true;;
    n   ) AUTH_N_CONFIG_FILE=${OPTARG}; AUTH_N_CONFIG_FILE_FLAG=true;;
    e   ) ECKT_CONFIG_FILE=${OPTARG}; ECKT_CONFIG_FILE_FLAG=true;;
    h   ) usage; exit;;
    \?  ) usage;exit 1;;
    :   ) echo "Missing option argument for -$OPTARG" >&2; usage; exit 1;;
    *   ) echo "Unimplemented option: -$OPTARG" >&2; usage; exit 1;;
 esac
done

# Required parameter KEY_FILE
if ! $KEY_FILE_FLAG; then
  echo "-k <KEY_FILE> must be included" >&2; usage; exit 1
else
  # Exit if file with name KEY_FILE does not exist
  if [ ! -f $CONFIG_DIR/$KEY_FILE ]; then
    echo "File $HOST_CONFIG_DIR/$KEY_FILE does not exist, please provide a valid key file"
    exit 1;
  fi
fi
# Copy KEY_FILE from shared volume to src directory of Token Generator library
cp $CONFIG_DIR/$KEY_FILE $APP_ROOT/key.json


# Copy CONFIG_FILE from shared volume to config directory of Token Generator library if specified as input
if $CONFIG_FILE_FLAG; then
  # Exit if file with name KEY_FILE does not exist
  if [ ! -f $CONFIG_DIR/$CONFIG_FILE ]; then
    echo "File $HOST_CONFIG_DIR/$CONFIG_FILE does not exist, please provide a valid config file or use default one by not specifying '-c' option"
    exit 1;
  else
    cp $CONFIG_DIR/$CONFIG_FILE $APP_ROOT/config/config.json
  fi
fi

# Copy AUTH_CONFIG_FILE from shared volume to config directory of Token Generator library if specified as input
if $AUTH_CONFIG_FILE_FLAG; then
  # Exit if file with name KEY_FILE does not exist
  if [ ! -f $CONFIG_DIR/$AUTH_CONFIG_FILE ]; then
    echo "File $HOST_CONFIG_DIR/$AUTH_CONFIG_FILE does not exist, please provide a valid AUTH config file or use default one by not specifying '-a' option"
    exit 1;
  else
    cp $CONFIG_DIR/$AUTH_CONFIG_FILE $APP_ROOT/config/authConfig.json
  fi
fi

# Copy AUTH_CONFIG_FILE from shared volume to config directory of Token Generator library if specified as input
if $AUTH_N_CONFIG_FILE_FLAG; then
  # Exit if file with name KEY_FILE does not exist
  if [ ! -f $CONFIG_DIR/$AUTH_N_CONFIG_FILE ]; then
    echo "File $HOST_CONFIG_DIR/$AUTH_N_CONFIG_FILE does not exist, please provide a valid AUTHN config file or use default one by not specifying '-n' option"
    exit 1;
  else
    cp $CONFIG_DIR/$AUTH_N_CONFIG_FILE $APP_ROOT/config/authnConfig.json
  fi
fi

# Copy ECKT_CONFIG_FILE from shared volume to config directory of Token Generator library if specified as input
if $ECKT_CONFIG_FILE_FLAG; then
  # Exit if file with name KEY_FILE does not exist
  if [ ! -f $CONFIG_DIR/$ECKT_CONFIG_FILE ]; then
    echo "File $HOST_CONFIG_DIR/$ECKT_CONFIG_FILE does not exist, please provide a valid EKCT config file or use default one by not specifying '-e' option"
    exit 1;
  else
    cp $CONFIG_DIR/$ECKT_CONFIG_FILE $APP_ROOT/config/authnConfig.json
  fi
fi

# Run token generator via run script
/bin/bash $APP_ROOT/run.sh