#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          fliwi-initialise
# Required-Start:    $network
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Initializes Fliwi on the local system
# Description:       This cares about a proper configuration of resolv.conf,
#                    the locales and the hostname.
### END INIT INFO

NAME=fliwi-initialise

. /lib/lsb/init-functions

do_start()
{
  echo ""

  ### Try to avoid swapping
  echo 1 > /proc/sys/vm/swappiness || true

  ### Enable disk caches on non block device disks
  . /usr/lib/lib-fliwi/fliwi-drives.sh
  for disk in $(fliwi_get_non_block_device_disks)
  do
    sdparm -s WCE=1 $disk 2>/dev/null 1>&2 || true
  done

  /usr/sbin/fliwi-update-resolvconf
  if [ $? -ne 0 ]; then
    echo "W: Updating resolv.conf failed, trying again in 15s..." 1>&2
    sleep 15
    /usr/sbin/fliwi-update-resolvconf
    if [ $? -ne 0 ]; then
      echo "E: Updating resolv.conf failed." 1>&2
      return 1
    fi
  fi
  /usr/sbin/fliwi-update-locales-config || return 1
  /usr/sbin/fliwi-update-hostname || return 1

  return 0
}


case "$1" in
  start|restart)
    log_begin_msg "Initializing Fliwi..."
    do_start
    log_end_msg $?
  ;;

  reload|force-reload|force-stop|status)
    echo "Error: argument '$1' not supported" >&2
    exit 3
  ;;

  stop)
  ;;

  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart}"
    exit 3
  ;;
esac

exit 0
