#!/bin/bash
#  Copyright (c) 2018, 2020, Oracle and/or its affiliates.
#
# rpm_post_install_init_config.sh 
#
# Script to perform initial configuration steps after an install
# preparing for all the other install scripts to run.
#
# This script will create the bootstrap.properties files and the sgdstorepass
# file.
#


# declare arguments that will be set via the command line 
install_dir=
asad_port=
peer_dns=
tta_group=
tta_user=
http_port=
https_port=
err_log=/dev/null
secure_port=
peer_port=

########## process command line arguments ##########
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    --install-dir)
    install_dir="$2"
    shift; shift # past argument & value
    ;;
    --asad-port)
    asad_port="$2"
    shift; shift # past argument & value
    ;;
    --peer-dns)
    peer_dns="$2"
    shift; shift # past argument & value
    ;;
    --tta-group)
    tta_group="$2"
    shift; shift # past argument & value
    ;;
    --tta-user)
    tta_user="$2"
    shift; shift # past argument & value
    ;;
    --http-port)
    http_port="$2"
    shift; shift # past argument & value
    ;;
    --peer-port)
    peer_port="$2"
    shift; shift # past argument & value
    ;;
    --err-log)
    err_log="$2"
    shift; shift # past argument & value
    ;;
    --secure-port)
    secure_port="$2"
    shift; shift # past argument & value
    ;;

    *)    # unknown option
    shift # past argument
    ;;
esac
done

source "${install_dir}/bin/scripts/install_scripts/install_utils"

########## create bootstrap values ##########
servconfig_global_dir="$install_dir/var/serverconfig/global"
servconfig_local_dir="$install_dir/var/serverconfig/local"
servconfig_globalserv_dir="$install_dir/var/serverconfig/global/services"

# prepare & configure dirs
MkDirForUser -p "$tta_user" "$tta_group" "$servconfig_global_dir" "$servconfig_local_dir" \
                "$servconfig_globalserv_dir"

chmod 700 "$servconfig_global_dir" "$servconfig_local_dir" \
          "$servconfig_globalserv_dir"

# create and config bootstrap.properties file
bootstrap_global_props="$install_dir/var/serverconfig/global/bootstrap.properties"
cat > "$bootstrap_global_props" <<EOF
# This file was generated by linux_initialStartConfig.sh 
tarantella.config.name=arraybootstrap
tarantella.config.type=server
tarantella.config.array.externalport=$asad_port
tarantella.config.array.port=$peer_port
tarantella.config.array.secureport=$secure_port
EOF

chmod 600 "$bootstrap_global_props"
chgrp "$tta_group" "$bootstrap_global_props"
chown "$tta_user" "$bootstrap_global_props"

bootstrap_local_props="$install_dir/var/serverconfig/local/bootstrap.properties"
cat > "$bootstrap_local_props" <<EOF
# This file was generated by linux_initialStartConfig.sh
tarantella.config.name=serverbootstrap
tarantella.config.type=server
tarantella.config.server.dns.external=$peer_dns
tarantella.config.server.dns.peer=$peer_dns
tarantella.config.server.webserver.port=$http_port
EOF

chmod 600 "$bootstrap_local_props"
chgrp "$tta_group" "$bootstrap_local_props"
chown "$tta_user" "$bootstrap_local_props"

# create the SGS trust store password file with a default password
sgd_keystore_passfile="$install_dir/var/info/sgdstorepass"
echo "changeit" > "${sgd_keystore_passfile}"
chmod 440 "${sgd_keystore_passfile}"
chgrp "$tta_group" "${sgd_keystore_passfile}"
chown "$tta_user" "${sgd_keystore_passfile}"
