#!/bin/sh


supervisor_conf_d_dir="/etc/supervisor/conf.d"
fliwi_config_base_services='/etc/fliwi/services'
restart_supervisor=0
changes_detected=0


##########################
fliwi_script_help()
{
  echo ""
  echo "Update Supervisor config in Fliwi"
  echo ""
  echo "Usage: $(basename $0) [OPTIONS]"
  echo "====="
  echo ""
  echo "OPTIONS:"
  echo "========"
  echo ""
  echo "--restart-supervisor-on-changes"
  echo "  Restart Supervisor if changes to the config are detected."
  echo ""
}


TEMP=$(getopt -o h --long help,restart-supervisor-on-changes \
              -n "$(basename $0)" -q -- "$@")
if [ $? != 0 ]; then
  fliwi_script_help
  exit 1
fi

eval set -- "$TEMP"

while true
do
  case "$1" in
    -h|--help)
      fliwi_script_help
      exit 1
    ;;

    --restart-supervisor-on-changes)
      restart_supervisor=1
      shift 1
    ;;

    --)
      shift
      break
    ;;

    *)
      fliwi_script_help
      exit 1
    ;;
  esac
done


### Removed invalid config-links
for config_link in $(find $supervisor_conf_d_dir -mindepth 1 -maxdepth 1 -type l)
do
  if [ "$config_link" = "$(readlink -m $config_link)" ]; then
    echo "WARNING: Removing wrong linked configuration at '$config_link', that is pointing to itself" 1>&2
    rm -f $config_link
  fi
done

### Removed gone config-links
for config_link in $(find $supervisor_conf_d_dir -mindepth 1 -maxdepth 1 -type l -lname '/etc/fliwi/services/*/supervisor/conf.d/*')
do
  service_name_based_on_parent_dir="$(readlink $config_link | cut -d '/' -f 5)"
  config_file_name_without_file_ending=$(basename $(readlink -m $config_link) | sed -r 's/\.conf$//')
  if [ ! -f "$(readlink -m $config_link)" ]; then
    echo "INFO: Removing obsolete linked configuration at '$config_link'" 1>&2
    changes_detected=1
    rm -f $config_link
  elif [ "$(basename $config_link)" != "$config_file_name_without_file_ending.$service_name_based_on_parent_dir.conf" ]; then
    changes_detected=1
    echo "WARNING: Removing wrong linked configuration at '$config_link', that is pointing to '$(readlink $config_link)'" 1>&2
    rm -f $config_link
  fi
done

### Add missing config-links
for my_numbered_service in $(fliwi-get-my-services)
do
  my_service=$(echo $my_numbered_service | sed -r 's/-[0-9]+$//')
  if [ -z "$my_service" ]; then
    continue
  fi
  for config_file in $(find -L $fliwi_config_base_services -mindepth 4 -maxdepth 4 -type f -path '/etc/fliwi/services/'$my_service'/supervisor/conf.d/*.conf')
  do
    config_file_name_without_file_ending=$(basename $config_file | sed -r 's/\.conf$//')
    config_link="$supervisor_conf_d_dir/$config_file_name_without_file_ending.$my_service.conf"
    if [ ! -L "$config_link" ]; then
      ln -s $config_file $config_link
      changes_detected=1
      echo "INFO: Added new linked configuration at '$config_link'" 1>&2
    fi
  done
done

if [ $changes_detected -eq 1 ]; then
  echo "INFO: Changes in configuration detected..." 1>&2
  if [ $restart_supervisor -eq 1 ]; then
    ### Restart Supervisor (if running)
    ### Note: A simple restart seems not to be enough for supervisor
    supervisorPID=$(pidof -s -x supervisord) || supervisorPID=0
    if [ -n "$supervisorPID" ] && \
       [ $supervisorPID -gt 0 ]; then
      echo "INFO: Restarting Supervisor..." 1>&2
      if which invoke-rc.d >/dev/null 2>&1; then
        invoke-rc.d supervisor stop || true
        invoke-rc.d supervisor start || true
      else
        /etc/init.d/supervisor stop || true
        /etc/init.d/supervisor start || true
      fi
    else
      echo "INFO: Not restarting Supervisor, as it is not running..." 1>&2
    fi
  fi
else
  echo "INFO: No changes in configuration detected..." 1>&2
fi

exit 0


