#!/bin/sh


apache_sites_available_dir="/etc/apache2/sites-available"
fliwi_config_base_services='/etc/fliwi/services'
reload_apache2=0
changes_detected=0


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


TEMP=$(getopt -o h --long help,restart-apache-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-apache-on-changes)
      reload_apache2=1
      shift 1
    ;;

    --)
      shift
      break
    ;;

    *)
      fliwi_script_help
      exit 1
    ;;
  esac
done


### Removed invalid config-links
for config_link in $(find $apache_sites_available_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
    a2dissite $(basename $config_link) 2>/dev/null || true
    rm -f $config_link
  fi
done

### Removed gone config-links
for config_link in $(find $apache_sites_available_dir -mindepth 1 -maxdepth 1 -type l -lname '/etc/fliwi/services/*/apache/sites-available/*')
do
  if [ ! -f "$(readlink -m $config_link)" ]; then
    echo "INFO: Removing obsolete linked configuration at '$config_link'" 1>&2
    changes_detected=1
    a2dissite $(basename $config_link) 2>/dev/null || true
    rm -f $config_link
  elif [ "$(basename $config_link)" != "$(basename $(readlink -m $config_link)).$(readlink $config_link | cut -d '/' -f 5)" ]; then
    changes_detected=1
    a2dissite $(basename $config_link) 2>/dev/null || true
    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'/apache/sites-available/*')
  do
    config_link="$apache_sites_available_dir/$(basename $config_file).$my_service"
    if [ ! -L "$config_link" ]; then
      ln -s $config_file $config_link
      changes_detected=1
      a2ensite $(basename $config_link) 2>/dev/null || true
      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 [ $reload_apache2 -eq 1 ]; then
    echo "INFO: Reloading apache2..." 1>&2
    ### Restart apache2 (if running)
    apache2PID=$(pidof -s apache2) || apache2PID=0
    if [ -n "$apache2PID" ] && \
       [ $apache2PID -gt 0 ]; then
      if which invoke-rc.d >/dev/null 2>&1; then
        invoke-rc.d apache2 reload || true
      else
        /etc/init.d/apache2 reload || true
      fi
    fi
  fi
else
  echo "INFO: No changes in configuration detected..." 1>&2
fi

exit 0


