#!/bin/sh


memcached_config_prefix="memcached_"
memcached_config_prefix_fliwi="memcached_FLIWI_"
fliwi_config_base_services='/etc/fliwi/services'
restart_memcached=0
changes_detected=0

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


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

    --)
      shift
      break
    ;;

    *)
      fliwi_script_help
      exit 1
    ;;
  esac
done


### Removed invalid config-links
for config_link in $(find /etc/$memcached_config_prefix*.conf -mindepth 0 -maxdepth 0 -type l 2>/dev/null)
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 /etc/$memcached_config_prefix_fliwi*.conf -mindepth 0 -maxdepth 0 -lname '/etc/fliwi/services/*/memcached/memcached.conf' 2>/dev/null)
do
  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)" != "$memcached_config_prefix_fliwi$(readlink $config_link | cut -d '/' -f 5).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 3 -maxdepth 3 -type f -path '/etc/fliwi/services/'$my_service'/memcached/*.conf' 2>/dev/null)
  do
    config_link="/etc/$memcached_config_prefix_fliwi$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

### Always stop "ghost"-memcached-instances
# Check if we need to stop the the single memcached instance
if [ -f "/var/run/memcached.pid" ] && \
   [ $(ls /etc/memcached_*.conf 2>/dev/null | wc -l) -gt 0 ]; then
  echo "WARNING: Detected obsolete and possibly still running single memcache-instance - stopping it..." 1>&2
  start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile /var/run/memcached.pid --exec /usr/bin/memcached
  rm -f /var/run/memcached.pid
fi

# Check for other "ghost"-instances and stop them
for pid_file in $(find /var/run/memcached_*.pid 2>/dev/null)
do
  memchache_name=$(basename $pid_file | sed -r 's/\.pid$//')
  if [ ! -f "/etc/$memchache_name.conf" ]; then
    echo "WARNING: Detected obsolete and possibly still running memcache-instance '$memchache_name' - stopping it..." 1>&2
    start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $pid_file --exec /usr/bin/memcached
    rm -f $pid_file
  fi
done

if [ $changes_detected -eq 1 ]; then
  echo "INFO: Changes in configuration detected..." 1>&2
  if [ $restart_memcached -eq 1 ]; then
    echo "INFO: Restarting memcached..." 1>&2
    if which invoke-rc.d >/dev/null 2>&1; then
      invoke-rc.d memcached restart || true
    else
      /etc/init.d/memcached restart || true
    fi
  fi
else
  echo "INFO: No changes in configuration detected..." 1>&2
fi

exit 0


