#!/bin/sh
set -e

PKGNAME="$1"
MODE="$2" # PRE-INSTALL, POST-INSTALL, DEINSTALL, POST-DEINSTALL

LOGFILE=/tmp/bareos-setup.log

initialize_bareos()
{
  COMPONENT="$1"
  echo "$PKGNAME: initializing bareos configuration, logs to ${LOGFILE}"
  /usr/local/lib/bareos/scripts/bareos-config deploy_config "${COMPONENT}" >>${LOGFILE} 2>&1 || echo "bareos-config deploy_config ${COMPONENT}: failed"
}

case "$MODE" in
  POST-INSTALL)
    case "$PKGNAME" in
      bareos.com-director*)
        initialize_bareos "bareos-dir"
        #echo "enable the service with 'sysrc bareosdir_enable=YES'"
        ;;

      bareos.com-filedaemon*)
        initialize_bareos "bareos-fd"
        #echo "enable the service with 'sysrc bareosfd_enable=YES'"

        ;;

      bareos.com-storage*)
        initialize_bareos "bareos-sd"
        #echo "enable the service with 'sysrc bareossd_enable=YES'"
        ;;

      bareos.com-bconsole*)
        initialize_bareos "bconsole"
        #echo "enable the service with 'sysrc bareossd_enable=YES'"
        ;;

    esac
    ;;
  DEINSTALL) ;;
  PRE-INSTALL)
    true
    ;;
  POST-DEINSTALL)
    echo "deinstalling $PKGNAME"
    case "$PKGNAME" in
      bareos.com-director*)
        echo "shutting down bareos-dir..."
        service bareos-dir status && service bareos-dir stop || echo " already stopped"
        ;;

      bareos.com-filedaemon*)
        echo "shutting down bareos-fd..."
        service bareos-fd status && service bareos-fd stop || echo " already stopped"
        ;;

      bareos.com-storage*)
        echo "shutting down bareos-fd..."
        service bareos-sd status && service bareos-sd stop || echo " already stopped"
        ;;
    esac
    ;;
esac
