#!/bin/sh -e
#
# gandi-postboot     Start/Stop the Gandi setup script from customer.
#
# chkconfig: 2345 52 25
# description: initscript which run customer configuration script \
#              in Gandi IaaS hosting virtual machine
# processname: gandi-postboot
# config: /etc/sysconfig/gandi
# pidfile: /var/run/gandi-postboot.pid
#
### BEGIN INIT INFO
# Provides: gandi-postboot
# Required-Start: $all $remote_fs
# Required-Stop: 
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Start-Before:
# Short-Description: gandi-postboot run customer configuration script.
# Description: initscript which run customer configuration script \
#              in Gandi IaaS hosting virtual machine
### END INIT INFO

# author : Nicolas Chipaux <aegiap@gandi.net> for Gandi.net
# all rights reserved.

# Customer of the IaaS hosting can provide a script which will be executed as
# root on the virtual machine and find some configuration in a JSON form in
# /gandi/config

case "$1" in
    start|restart|reload)
        if [ -d '/etc/sysconfig' ]; then
            CONFIG_FILE='/etc/sysconfig/gandi'
        else
            CONFIG_FILE='/etc/default/gandi'
        fi
        export $(grep ^CONFIG_ALLOW_MOUNT "${CONFIG_FILE}")
        if [ 0 -eq "${CONFIG_ALLOW_MOUNT}" ] && [ -f /etc/default/autofs ]; then
            service autofs stop
        fi
        if [ -x /gandi/init ]; then
            pbin=/usr/bin/python3
            [ -x $pbin ] || pbin=/usr/bin/python2
            [ -x $pbin ] || pbin=/usr/bin/python
            $pbin /gandi/init --postboot
            rm -f /gandi/init
        fi
    ;;
    stop|force-reload|status)
        # nothing
    ;;
    *)
        echo "Script only started at boot for system configuration."
    ;;
esac

exit 0
