#!/bin/bash

set +e
LOG_FILE=/tmp/entrypoint.log

{
date
whoami
env

#########################################################################
echo "run-time config (compose.yaml):"
echo "SECURITY            : "$SECURITY
echo "AUTH_DB_*           : "$AUTH_DB_URI" ["$AUTH_DB_SCHEMA"] "$AUTH_DB_USERNAME" / "$AUTH_DB_PASSWORD

echo "ACCESS_CONTEXT_ROOT : "$ACCESS_CONTEXT_ROOT
echo "RESPONSE_FORMAT     : "$RESPONSE_FORMAT
echo "URL_CUTOUTS         : "$URL_CUTOUTS
echo "RESOLVER_DB_*       : "$RESOLVER_DB_URI" ["$RESOLVER_DB_SCHEMA"] "$RESOLVER_DB_USERNAME" / "$RESOLVER_DB_PASSWORD
echo "VLKBOBSCORE_PG_URI  : "$VLKBOBSCORE_PG_URI

echo "build-time config'd (Dockefile):"
echo "INST_DIR   : "$INST_DIR
echo "WEBAPP_DIR : "$WEBAPP_DIR

echo "JAVA_HOME       : "$JAVA_HOME
echo "CATALINA_BASE   : "$CATALINA_BASE
echo "CATALINA_HOME   : "$CATALINA_HOME
echo "CATALINA_TMPDIR : "$CATALINA_TMPDIR
#########################################################################

## configure vlkb-tools
if test -n "$VLKBOBSCORE_PG_URI"
then
   echo "pg_uri=$VLKBOBSCORE_PG_URI" >> $INST_DIR/etc/vlkb-obscore/datasets.conf
   echo "pg_schema=datasets"         >> $INST_DIR/etc/vlkb-obscore/datasets.conf
fi


## configure VLKB access

cp $WEBAPP_DIR/META-INF/context.xml $CATALINA_BASE/conf/Catalina/localhost/$ACCESS_CONTEXT_ROOT.xml

echo "fits_path_surveys=/srv/surveys" > $WEBAPP_DIR/WEB-INF/classes/cutout.properties

{ # write cutout.properties

   case $RESPONSE_FORMAT in
      application/fits)
         ;;
      "application/fits;createfile=yes")
         echo "default_response_format=$RESPONSE_FORMAT"
         echo "fits_path_cutouts=/srv/cutouts"
         QUEUE_NAME=dockervlkb$ACCESS_CONTEXT_ROOT
         ;;
      application/x-vlkb*)
         echo "default_response_format=$RESPONSE_FORMAT"
         echo "fits_path_cutouts=/srv/cutouts"
         echo "fits_url_cutouts=$URL_CUTOUTS"
         echo "surveys_metadata_abs_pathname=/srv/surveys/survey_populate.csv"
         QUEUE_NAME=dockervlkb$ACCESS_CONTEXT_ROOT
         echo "default_sky_system=GALACTIC"
         echo "default_spec_system=VELO_LSRK"
         echo "show_duration=yes"
         ;;
   esac

   if test -n "$QUEUE_NAME"
   then
      echo "amqp_host_name=localhost"
      echo "amqp_port=5672"
      echo "amqp_routing_key=$QUEUE_NAME"
   fi

   if test -n "$RESOLVER_DB_URI"
   then
      echo "db_uri=$RESOLVER_DB_URI"
      echo "db_schema=$RESOLVER_DB_SCHEMA"
      echo "db_user_name=$RESOLVER_DB_USERNAME"
      echo "db_password=$RESOLVER_DB_PASSWORD"
   fi

} >> $WEBAPP_DIR/WEB-INF/classes/cutout.properties


if test -n "$QUEUE_NAME"
then
   service rabbitmq-server start
   $INST_DIR/bin/vlkbd_exec.sh localhost $QUEUE_NAME $INST_DIR/etc/vlkbd/datasets.conf
fi



if test -n "$SECURITY"
then
   cd $WEBAPP_DIR/WEB-INF/ && rm -f web.xml && cp web-cutout-$SECURITY.xml web.xml && cd -
   echo "db_uri=$AUTH_DB_URI"            >  $WEBAPP_DIR/WEB-INF/classes/authpolicy.properties
   echo "db_schema=$AUTH_DB_SCHEMA"      >> $WEBAPP_DIR/WEB-INF/classes/authpolicy.properties
   echo "db_user_name=$AUTH_DB_USERNAME" >> $WEBAPP_DIR/WEB-INF/classes/authpolicy.properties
   echo "db_password=$AUTH_DB_PASSWORD"  >> $WEBAPP_DIR/WEB-INF/classes/authpolicy.properties
fi

# configure access-token validation
if test -f /srv/surveys/iamtoken.properties
then
   cp /srv/surveys/iamtoken.properties $WEBAPP_DIR/WEB-INF/classes/
fi

# configure port/SSL connector: (path is relative to the dir where compose.yaml is
# - web.xml to run filters set above
# * ssl: set tomcat connector with certificates (ia2 needs SECTIGO, iam needs self-signed keystore.jks)
# * keep right jjwt*.jar libs (ia2 authlib needs v0.11, iam needs v0.12)
# assume all files in ssl sub-dir relative to where compose.yaml is
# set volume mapping in compose.yaml: ssl/ -> /etc/pki/tls/
case $SECURITY in
   ia2token)
      #cp ssl/server-connector-8443.xml-SECTIGO-vlkb_ia2_inaf_it  /etc/tomcat9/server-connector-8443.xml
      cp /root/ssl/server-connector-8443.xml /etc/tomcat9/server-connector-8443.xml
      # map volume instead of this: cp -r ssl/SECTIGO /etc/pki/tls/
      rm /webapps/vlkb-cutout/WEB-INF/lib/jjwt-*0.12*.jar
      ;;
   iamtoken)
      #cp ssl/server-connector-8443.xml-keystore-self-signed  /etc/tomcat9/server-connector-8443.xml
      cp /root/ssl/server-connector-8443.xml  /etc/tomcat9/server-connector-8443.xml
      # map volume somedir:/etc/pki/tls with somedir/{keystore.jks,SECTIGO/*} XXX cp ssl/keystore.jks /etc/pki/tls/
      rm /webapps/vlkb-cutout/WEB-INF/lib/jjwt-*0.11*.jar
      ;;
   *)
      echo "Security not configured, runs open."
      ;;
esac
#
#if test -f /srv/surveys/keystore.jks
#then
#   cp /srv/surveys/keystore.jks /root/
#fi
#if test -f /srv/surveys/server-connector-8443.xml /etc/tomcat9/
#then
#   cp /srv/surveys/server-connector-8443.xml /etc/tomcat9/
#fi
#
if test -n "$SECURITY"
then
   cd /etc/tomcat9/ && ln -s server-connector-8443.xml server-connector.xml && cd -
else
   cd /etc/tomcat9/ && ln -s server-connector-8080.xml server-connector.xml && cd -
fi


#########################################################################

date

} 1> $LOG_FILE 2>&1

/usr/libexec/tomcat9/tomcat-start.sh &

wait -n

