#! /bin/sh
### BEGIN INIT INFO
# Provides:          opac
# Required-Start:    $caspernfs
# Required-Stop:
# Should-Start:      $local_fs
# Should-Stop:       halt reboot
# X-Stop-After:      umountroot
# Default-Start:
# Default-Stop:      0 6
# Short-Description: Casper init script
# Description:       Resyncs snapshots, evantually caches files in order
#                    to let remove the media.
### END INIT INFO

# Author: Tollef Fog Heen <tfheen@canonical.com>
#         Marco Amadori <marco.amadori@gmail.com>
#
PATH=/usr/sbin:/usr/bin:/sbin:/bin
NAME=opac
SCRIPTNAME=/etc/init.d/${NAME}
DO_SNAPSHOT=/sbin/${NAME}-snapshot

# Exit if system was not booted by caspernfs
grep -qs boot=nfs /proc/cmdline || exit 0

# Read configuration variable file if it is present
[ -r /etc/$NAME.conf ] && . /etc/$NAME.conf

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions


case "$1" in
    restart|reload|force-reload|status)
        [ "$VERBOSE" != no ] && log_end_msg 0
        ;;
    start)
        log_begin_msg "${NAME} is fixing mounts and autostart ..." 
        if [ -z "$( mount | grep proc)" ]; then
          mount /proc
        fi
        # handle special autologin options
	for x in $(cat /proc/cmdline) ; do
	 case $x in
	   caspernfs-autologin=*)
	    AUTOLOGIN="${x#caspernfs-autologin=}"
	  ;;
	   opac-proxy=*)
	    OPACPROXY="${x#opac-proxy=}"
	  ;;
	   opac-home=*)
	    OPACHOME="${x#opac-home=}"
	  ;;
	 esac
	done
        if [ "$AUTOLOGIN" = "opac" ]; then
          MDMCustomFile=/etc/mdm/mdm.conf
          if ! `grep -qs '\[daemon\]' $MDMCustomFile` ; then
            echo '[daemon]' >> $MDMCustomFile
          fi
          sed -i "s/\[daemon\]/\[daemon\]\nDefaultSession=opac.desktop/" $MDMCustomFile        
          /etc/init.d/mdm stop
          sleep 1
          /etc/init.d/mdm start
        fi
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    *)
        log_success_msg "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac
