#!/bin/bash
# VERSION=2

#################################
### /!\ /!\ PLEASE DO NOT USE THIS SCRIPT - BACKUP
###
### USE /etc/init.d/S44redirect INSTEAD
###

## redirect already executed in rootfs
## no need to execute again since it's script
## is a backup and should be useless
[ -f /tmp/redirect.log ] && exit 0

BEROCONF="/usr/fallback/beroconf"

function log () {
	echo "[init_cloud] ${1}"
}

function start () {
	# if cloud disabled, leave
	if [ "$(${BEROCONF} get root redirect_cfg | grep -v failed)" == "off" ]; then
		log "Disabled."
		exit 0
	fi
	# get the redirect_server
	# redirect_server=$(${BEROCONF} get root redirect_server | grep -v failed)
	
	/usr/bin/env -i bash -c "/usr/local/php/redirect.php"

	log "Started."
}

function stop () {
	log "Stopped."
}

case "${1}" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		sleep 1
		start
		;;
	*)
		echo "Usage: ${0} [start|stop|restart]" >&2
		exit 1
		;;
esac

exit 0
