#!/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=""
report_postmax="5"
docker_name=dcpgbatch

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

gflog() {
  echo "follow the log: $domain_log"
  tail -f $domain_log
}
gflogrm() {
  echo "remove the log: $domain_log"
  rm "$domain_log"
}
gfconf() {
  nvim "$payara_config"
}
gfscript() {
  gflogcount=$(cat "$domain_log" | wc -l)
  outPath=$pgbadger_out$report_postfix/bash.out
  touch "$outPath"
  echo "" >>"$outPath"
  echo "============================================================" >>"$outPath"
  echo "calling number $report_postno / $report_postmax" >>"$outPath"
  echo "" >>"$outPath"
  bash $script_path | tee -a "$outPath"
  gflastlog=$(cat "$domain_log" | wc -l)
  gfcnt=$(($gflastlog - $gflogcount))
  printf "\n%-20s %+5s %+9s %+9s %+9s %+9s\n" "function" "Runs" "Min (ms)" "Avg (ms)" "Max (ms)" "1st (ms)" >>"$outPath"

  tail -$gfcnt $domain_log | awk '/PerformanceCounter-create \/view/      { print $3 }' | awk 'BEGIN {min=500;max=0} {sum += $1; cnt += 1}; first == "" { first = $1 }; min > $1 { min = $1 } max < $1 { max = $1 } END { if (cnt < 1) {cnt = -1}; printf("%-20s %5i %9.3f %9.3f %9.3f %9.3f\n", "view-create-list", cnt, min, sum/cnt, max, first) }' >>"$outPath"
  tail -$gfcnt $domain_log | awk '/PerformanceCounter-build \/view/       { print $3 }' | awk 'BEGIN {min=500;max=0} {sum += $1; cnt += 1}; first == "" { first = $1 }; min > $1 { min = $1 } max < $1 { max = $1 } END { if (cnt < 1) {cnt = -1}; printf("%-20s %5i %9.3f %9.3f %9.3f %9.3f\n", "view-buildr-list", cnt, min, sum/cnt, max, first) }' >>"$outPath"
  tail -$gfcnt $domain_log | awk '/PerformanceCounter-loadData \/view/    { print $3 }' | awk 'BEGIN {min=500;max=0} {sum += $1; cnt += 1}; first == "" { first = $1 }; min > $1 { min = $1 } max < $1 { max = $1 } END { if (cnt < 1) {cnt = -1}; printf("%-20s %5i %9.3f %9.3f %9.3f %9.3f\n", "view-loaddt-list", cnt, min, sum/cnt, max, first) }' >>"$outPath"
  tail -$gfcnt $domain_log | awk '/PerformanceCounter-loadDataMap \/view/ { print $3 }' | awk 'BEGIN {min=500;max=0} {sum += $1; cnt += 1}; first == "" { first = $1 }; min > $1 { min = $1 } max < $1 { max = $1 } END { if (cnt < 1) {cnt = -1}; printf("%-20s %5i %9.3f %9.3f %9.3f %9.3f\n", "view-loaddm-list", cnt, min, sum/cnt, max, first) }' >>"$outPath"
  tail -$gfcnt $domain_log | awk '/PerformanceCounter-renderData \/view/  { print $3 }' | awk 'BEGIN {min=500;max=0} {sum += $1; cnt += 1}; first == "" { first = $1 }; min > $1 { min = $1 } max < $1 { max = $1 } END { if (cnt < 1) {cnt = -1}; printf("%-20s %5i %9.3f %9.3f %9.3f %9.3f\n", "view-rdrdat-list", cnt, min, sum/cnt, max, first) }' >>"$outPath"
  tail -$gfcnt $domain_log | awk '/PerformanceCounter-renderLoad \/view/  { print $3 }' | awk 'BEGIN {min=500;max=0} {sum += $1; cnt += 1}; first == "" { first = $1 }; min > $1 { min = $1 } max < $1 { max = $1 } END { if (cnt < 1) {cnt = -1}; printf("%-20s %5i %9.3f %9.3f %9.3f %9.3f\n", "view-rdrlst-list", cnt, min, sum/cnt, max, first) }' >>"$outPath"
  tail -$gfcnt $domain_log | awk '/PerformanceCounter-render \/view/      { print $3 }' | awk 'BEGIN {min=500;max=0} {sum += $1; cnt += 1}; first == "" { first = $1 }; min > $1 { min = $1 } max < $1 { max = $1 } END { if (cnt < 1) {cnt = -1}; printf("%-20s %5i %9.3f %9.3f %9.3f %9.3f\n", "view-render-list", cnt, min, sum/cnt, max, first) }' >>"$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
}
pgrpout() {
  echo "Show bash output"
  outPath=$pgbadger_out$report_postfix/bash.out
  cat "$outPath"
}
dccreate() {
  sudo docker compose -f $COMPOSE_FILE create --force-recreate
}
dcconf() {
  nvim $COMPOSE_FILE
}
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
}
meascall() {
  gfscript
  pgrp
  pglogrm
}
measrun() {
  for i in $(seq 1 $report_postmax); do
    report_postno=$i
    meascall
  done
}
for name in "$@"; do
  case $name in
  -rppf=*) report_postfix="${name#*=}" ;;
  -rppn=*) report_postno="${name#*=}" ;;
  -rppm=*) report_postmax="${name#*=}" ;;
  gflog) gflog ;;
  gflogrm) gflogrm ;;
  gfconf) gfconf ;;
  gfscript) gfscript ;;
  gfrestart) pgrpinit ;;
  pginit) pginit ;;
  pglogls) pglogls ;;
  pglogrm) pglogrm ;;
  pglog) pglog ;;
  pgconf) pgconf ;;
  pgrestart) pgrestart ;;
  pgrp) pgrp ;;
  pgrpres) pgrpres ;;
  pgrpout) pgrpout ;;
  dcinit) dccreate ;;
  dcconf) dcconf ;;
  dcstart) dcstart ;;
  dcstop) dcstop ;;
  dcres) dcres ;;
  dcstats) dcstats ;;
  measinit)
    pginit
    pgrpres
    pglogrm 0
    gflogrm
    dccreate
    dcstart
    ;;
  measres)
    dcstop
    pgrpres
    pglogrm 0
    gflogrm
    dcstart
    pgrp
    pglogrm
    ;;
  meascall) meascall ;;
  measrun) measrun ;;
  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 "  -rppm=<val>  Count of calling meascall (used by measrun)"
    echo ""
    echo "*** glassfish ***"
    echo "  gflog     Show and follow the log of the glassfish server with $domain_name"
    echo "  gflogrm   Remove the log file 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 "  pgrpout   Show the saved output of the bash scripts"
    echo "  pgrpres   Resetet the output of pgbadger"
    echo ""
    echo "*** docker ***"
    echo "  dcinit    Create the both docker container"
    echo "  dcconf    Open the configuration file for the docker container"
    echo "  dcstart   Start the both docker container"
    echo "  dcstop    Stop the both docker container stoppen"
    echo "  dcres     Stop and remote the both docker container"
    echo "  dcstats   Show the docker live statistik"
    echo ""
    echo "*** combine cmds ***"
    echo "  measinit  Initialize everthing after start"
    echo "  measres   reset data for new measuring"
    echo "  meascall  execute one measure"
    echo "  measrun   execute <rppm> calls of measure (default: 5 runs)"
    ;;
  *)
    echo >&2 "Invalid option $name"
    exit 1
    ;;
  esac
done
