#!/usr/bin/env bash

ROOT="$(dirname "$(readlink -f "${0}")")"

RPM=0
CONFIG="${CONFIG:-/etc/ood/config/ood_portal.yml}"
APACHE="${APACHE:-/opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf}"
BIN="${BIN:-${ROOT}/../bin/generate}"

set -e

usage () {
    echo "Usage: update_ood_portal [-r|--rpm]"
    echo "-r|--rpm      Execution performed during RPM install"
}

OPTS=`getopt -o rh --long rpm,help -n 'update_ood_portal' -- "$@"`
if [[ $? -ne 0 ]]; then
    echo "Failed parsing options"
    usage
    exit 1
fi

eval set -- "$OPTS"

while true; do
    case "$1" in
        -h|--help) usage ; exit 0 ; shift ;;
        -r|--rpm) RPM=1 ; shift ;;
        *) break ;;
    esac
done

echo "Generating Apache config using YAML config: '${CONFIG}'"

NEW_APACHE="$("${BIN}" -c "${CONFIG}")"
if ! cmp -s <(echo "${NEW_APACHE}") "${APACHE}"; then
  if [[ -f "${APACHE}" ]]; then
    BAK_APACHE="${APACHE}.$(date '+%Y%m%dT%H%M%S')"
    echo "Backing up previous Apache config to: '${BAK_APACHE}'"
    mv "${APACHE}" "${BAK_APACHE}"
  fi
  echo "Generating new Apache config at: '${APACHE}'"
  echo "${NEW_APACHE}" > "${APACHE}"
  ret=0
else
  echo "No change in Apache config."
  ret=1
fi

if [ $RPM -eq 1 ]; then
    exit $ret
fi

echo "Completed successfully!"
echo ""
echo "Restart the httpd24-httpd service now."
echo ""

if [[ -L "/sbin/init" ]]; then
  echo "Suggested command:"
  echo "    sudo systemctl try-restart httpd24-httpd.service httpd24-htcacheclean.service"
  echo ""
else
  echo "Suggested commands:"
  echo "    sudo service httpd24-httpd condrestart"
  echo "    sudo service httpd24-htcacheclean condrestart"
  echo ""
fi

exit $ret
