#!/bin/sh

## live-config(7) - System Configuration Scripts
## Copyright (C) 2006-2012 Daniel Baumann <daniel@debian.org>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.

#########################################################################################

## script name: 9989-hooks 
## script location: /lib/live/config/

## This version is a (hopefully temporary) experimental fix for a regression in Sid

## David Hare and fsr 26-12-2012

## If "hooks" or "live-hooks" is called, this will run before (official) 9990-hooks.

## If hooks are found 9990-hooks gets "-x"ed then cannot be executed

## When the official version gets fixed this version should be removed and <chmod +x /lib/live/config/9990-hooks>

#########################################################################################

Hooks ()
{

# Checking if package is installed or already configured
	if  [ -e /var/lib/live/config/hooks ]
	then
		return
	fi

	for _PARAMETER in ${_CMDLINE}
	do
		case "${_PARAMETER}" in
			live-config.hooks=*|hooks=*)
				LIVE_HOOKS="${_PARAMETER#*hooks=}"
				;;
		esac
	done

	if [ -z "${LIVE_HOOKS}" ]
	then
		return
	else
	echo "Hooks are set.. Processing... "
	fi

	Process_hooks
}

Process_hooks ()
{
	if [ -z "${LIVE_HOOKS}" ]
	then
		return
	fi

	echo -n " hooks"

	for _HOOK in $(echo ${LIVE_HOOKS} | sed -e 's/|/ /g')
	do
		case "${_HOOK}" in
			filesystem)
				if ls /lib/live/config-hooks/* 2>&1
				then
					_HOOKS="${_HOOKS} $(for _FILE in /lib/live/config-hooks/*; do echo file://${_FILE}; done)"
				fi
			;;

			medium)

#######################################################

# live-media-path directory is /live by default... but user might have set a different one

# if custom live-media-path was used
if (cat /proc/cmdline|grep -q "live-media-path="); then

LIVEMEDIA_PATH="$(cat /proc/cmdline|grep -o "live-media-path=.*" |sed 's: .*::' |sed 's:live-media-path=/::'|sed 's:live-media-path=::')"

	# see if it is a sub-dir
	if (echo $LIVEMEDIA_PATH|grep -q "/"); then
	LIVEMEDIA_DIR=$(dirname $LIVEMEDIA_PATH)
	else
	LIVEMEDIA_DIR="$LIVEMEDIA_PATH"
	fi

# if findiso was used it's different. It could be a partition root, directory or subdirectory
elif (cat /proc/cmdline|grep -q "findiso="); then 

LIVEMEDIA_PATH="$(cat /proc/cmdline|grep -o "findiso=.*" |sed 's: .*::' |sed 's:findiso=/::'|sed 's:findiso=::')"

	# if it's a sub-dir the output will have a slash in it
	if (echo $LIVEMEDIA_PATH|grep -q "/"); then
	LIVEMEDIA_DIR=$(dirname $LIVEMEDIA_PATH)

	else
	# it is not in a sub-dir
	LIVEMEDIA_DIR="$LIVEMEDIA_PATH"

		# if iso is in partition root, look for hooks only in /live
		if (grep -q "\.iso" ${LIVEMEDIA_PATH}); then
		LIVEMEDIA_DIR="live"
		fi
	fi
fi

	if [ -z "${LIVEMEDIA_DIR}" ]; then
	# live-media-path or findiso were not used so look for hooks only in /live
	LIVEMEDIA_DIR="live"
	fi

#######################################################

				# look for hooks in all likely mountpoints/${LIVEMEDIA_DIR}/config-hooks/*

				# suppress stderr from ls if nothing found

				# if booting direct from squash
				if ls /lib/live/mount/medium/${LIVEMEDIA_DIR}/config-hooks/* 2> /dev/null
				then
					_HOOKS="${_HOOKS} $(for _FILE in $(ls /lib/live/mount/medium/${LIVEMEDIA_DIR}/config-hooks/*) ; do echo file://${_FILE}; done)"

# TODO "find" might be better than then "ls" in case config-hooks/ has any subdirs
#					_HOOKS="${_HOOKS} $(for _FILE in $(find /lib/live/mount/medium/${LIVEMEDIA_DIR}/config-hooks/* -type f) ; do echo file://${_FILE}; done)"

				fi

				# if booting findiso
				if ls /lib/live/mount/findiso/${LIVEMEDIA_DIR}/config-hooks/* 2> /dev/null
				then
					_HOOKS="${_HOOKS} $(for _FILE in $(ls /lib/live/mount/findiso/${LIVEMEDIA_DIR}/config-hooks/*); do echo file://${_FILE}; done)"
				fi

				# EXPERIMENTAL : If 9990-misc-helpers.sh was patched and persistence is on
				# The live-media volume might be mounted on /lib/live/mount/persistence/sd[a-z][0-9]
				if ls /lib/live/mount/persistence/sd[a-z][0-9]/${LIVEMEDIA_DIR}/config-hooks/* 2> /dev/null
				then
					_HOOKS="${_HOOKS} $(for _FILE in $(ls /lib/live/mount/persistence/sd[a-z][0-9]/${LIVEMEDIA_DIR}/config-hooks/*); do echo file://${_FILE}; done)"
				fi
			;;

			*)
				_HOOKS="${_HOOKS} ${_HOOK}"
				;;
		esac
	done

	for _HOOK in ${_HOOKS}
	do
		_TMPFILE="$(mktemp -t live-config.XXXXXXXX)"

		if echo "${_HOOK}" | grep -qs file://
		then
			# local file
			cp $(echo ${_HOOK} | sed 's|file://||') "${_TMPFILE}"
		else
			# remote file
			Start_network

			wget --quiet "${_HOOK}" -O "${_TMPFILE}"
		fi

		chmod 0755 "${_TMPFILE}"
		./"${_TMPFILE}"
		rm -f "${_TMPFILE}"
	done

# Creating state file
	if [ -n "${_HOOKS}" ]; then
		touch /var/lib/live/config/hooks

	# this will be done only after thist script has successfully run, in a live session
	# disable original hook script
	chmod -x /lib/live/config/9990-hooks

	fi
}

Hooks
