#!/bin/bash

payara_path="/opt/docker/payara"
postgres_path="/var/lib/postgres"
postgres_path="/opt/docker"
postgres_data_path="$postgres_path/data"
postgres_log_path=$postgres_data_path/log

payara_config="$payara_path/config/domain.xml"
domain_log="$payara_path/logs/server.log"
script_path="/opt/docker/timing.sh"
pgbadger_out="/opt/docker/pgreport"
report_postfix=""
report_postno=""
docker_name=dcpgbatch

COMPOSE_FILE=/opt/docker/docker-compose.yaml

gflog() {
  echo "follow the log: $domain_log"
  tail -f $domain_log
}
gfconf() {
  nvim "$payara_config"
}
gfscript() {
  outPath=$pgbadger_out$report_postfix/bash.out
  touch "$outPath"
  echo "============================================================" >>"$outPath"
  bash $script_path | tee -a "$outPath"
}

pginit() {
  sudo chmod g+x $postgres_path
  sudo chmod g+x $postgres_data_path
  sudo chmod g+rx $postgres_log_path
}
pglogls() {
  echo "show postgresql logfiles"
  ls $postgres_log_path/ -lhtc
}
pglogrm() {
  cnt=${1:-10}
  cntTail=$(($cnt + 1))
  echo "remove old postgresql logfiles from $(ls $postgres_log_path/ -tc | wc -l) until $cnt (~${cnt}00 MB) $cntTail"
  ls $postgres_log_path/ -tc | tail -n +$cntTail | xargs -r -I{} sudo rm "$postgres_log_path/{}"
}
pglog() {
  pg_log=$(ls $postgres_log_path/ -tc --ignore '*log' | head -n 1)
  echo "follow the log: $postgres_log_path/$pg_log"
  tail -n 3 -f $postgres_log_path/$pg_log
}
pgconf() {
  sudo nvim "${postgres_path}/postgresql.conf"
}
pgrp() {
  mkdir -p $pgbadger_out$report_postfix
  mkdir -p $pgbadger_out$report_postfix$report_postno
  outPath=$pgbadger_out$report_postfix/bash.out
  touch "$outPath"
  echo "" >>"$outPath"
  pgbadger -X -I -f jsonlog -j 10 -O $pgbadger_out$report_postfix$report_postno $postgres_log_path/postgresql-*.json 2>&1 | tee -a "$outPath"
}
pgrpres() {
  if [[ "$report_postfix" == "" ]]; then
    rm -R $pgbadger_out
  else
    rm -R $pgbadger_out$report_postfix*
  fi
}
dccreate() {
  sudo docker compose -f $COMPOSE_FILE create --force-recreate
}
dcstart() {
  sudo docker compose -f $COMPOSE_FILE start
  sleep 2
  pginit
}
dcstop() {
  sudo docker compose -f $COMPOSE_FILE stop
}
dcres() {
  sudo docker compose -f $COMPOSE_FILE down
}
dcstats() {
  sudo docker stats
}
for name in "$@"; do
  case $name in
  -rppf=*) report_postfix="${name#*=}" ;;
  -rppn=*) report_postno="${name#*=}" ;;
  gflog) gflog ;;
  gfconf) gfconf ;;
  gfscript) gfscript ;;
  gfrestart) pgrpinit ;;
  pginit) pginit ;;
  pglogls) pglogls ;;
  pglogrm) pglogrm ;;
  pglog) pglog ;;
  pgconf) pgconf ;;
  pgrestart) pgrestart ;;
  pgrp) pgrp ;;
  pgrpres) pgrpres ;;
  dcinit) dccreate ;;
  dcstart) dcstart ;;
  dcstop) dcstop ;;
  dcres) dcres ;;
  dcstats) dcstats ;;
  measinit)
    pginit
    pgrpres
    pglogrm 0
    dccreate
    dcstart
    ;;
  measres)
    dcstop
    pgrpres
    pglogrm 0
    dcstart
    pgrp
    pglogrm
    ;;
  meascall)
    gfscript
    pgrp
    pglogrm
    ;;
  help)
    echo "CALLING: $0 <function> [ <function>]"
    echo "The overview of the functions of this script."
    echo "It is allowed to enter multiple functions in one execute,"
    echo "that would be called serialized."
    echo "ATTENTION: parameter must be defined in front of the commands!"
    echo ""
    echo "*** parameter ***"
    echo "  -rppf=<val>   Postfix name for the report-folder (used by gfscript, pgrp, pgrpres, measres, meascall)"
    echo "  -rppn=<val>   Postfix number for the report-folder (used by pgrp, measres, meascall)"
    echo ""
    echo "*** glassfish ***"
    echo "  gflog     Show and follow the log of the glassfish server with $domain_name"
    echo "  gfconf    Open the configuration file from glassfish server"
    echo "  gfscript  Calls the testscript for the website"
    echo ""
    echo "*** postgresql ***"
    echo "  pginit    Initialize the folder for postgresql log-folder"
    echo "  pglogls   Show the current content of the log-folder from postgresql"
    echo "  pglogrm   Clean the log-folder from postgresql, the newest 20 files are sill available"
    echo "  pglog     Show and follow the last log from postgresql"
    echo "  pgconf    Open the configuration file from postgresql"
    echo "  pgrestart Restart the postgresql"
    echo "  pgrp      Generate the pgbadger report from postgresql log files"
    echo "  pgrpres   Resetet the output of pgbadger"
    echo ""
    echo "*** docker ***"
    echo "  dcinit    Docker erstellen"
    echo "  dcstart   Docker Container starten"
    echo "  dcstop    Docker Container stoppen"
    echo "  dcres     Docker Container stoppen und loeschen"
    echo "  dcstats   Docker live Statistik anzeigen"
    echo ""
    echo "*** combine cmds ***"
    echo "  measinit  Initialize everthing after start"
    echo "  measres   reset data for new measuring"
    echo "  meascall  execute one measure"
    ;;
  *)
    echo >&2 "Invalid option $name"
    exit 1
    ;;
  esac
done
