#!/bin/sh
#
# Register scripts/files/options for the freetz web interface
#
PROGRAM=$0
usage() {
	cat >&2 << EOF
Usage: $PROGRAM cgi    <pkg> <title>
       $PROGRAM pkg    <pkg> <title>
       $PROGRAM conf   <pkg> <id> <title>
       $PROGRAM extra  <pkg> <title> <sec-level> <id>               # empty title to hide page in menu
       $PROGRAM file   <pkg> <id> <title> <sec-level> <desc-file>
       $PROGRAM status <pkg> <title> [<id>]
       $PROGRAM daemon [-d|--disable] [-h|--hide] [-r|--rc-script=<script>] [-n|--name=<description>] [-p|--package=<pkg>] <id>
EOF
	exit 1
}

replace() {
	local regexp=$1 file=$2 data=$3
	[ -e "$file" ] || touch "$file"

	{ echo "$data"; grep -v "$regexp" "$file"; } | sort > "$file.$$"
	# replace $file atomically
	mv "$file.$$" "$file"
}

file_exists_or_die() {
	local file=$1
	if [ ! -e "$file" ]; then
		echo "$PROGRAM: File not found: $file" >&2
		exit 1
	fi
}

#
# For 'extra' and 'status', <cgi-name> is relative to
# /mod/usr/lib/cgi-bin/$pkg/ and without the .cgi extension!
#

case $1 in
	cgi)
		pkg=$2 title=$3
		"$0" pkg "$pkg" "$title"
		"$0" conf "$pkg" _index "$(lang de:"Einstellungen" en:"Settings")"
		;;
	pkg)
		pkg=$2 title=$3
		replace "^$pkg|" /mod/etc/reg/pkg.reg "$pkg|$title"
		touch /mod/var/cache/menu.stale
		;;
	conf)
		pkg=$2 id=$3 title=$4
		if [ "$id" = _index ]; then
			cgi="$pkg.cgi"
		else
			cgi="$pkg/$id.cgi"
		fi
		file_exists_or_die "/mod/usr/lib/cgi-bin/$cgi"
		replace "^$pkg|$id|" /mod/etc/reg/conf.reg "$pkg|$id|$title"
		touch /mod/var/cache/menu.stale
		;;
	extra)
		pkg=$2 title=$3 sec_level=$4 id=$5
		file_exists_or_die "/mod/usr/lib/cgi-bin/$pkg/$id.cgi"
		replace "^$pkg|.*|$id\$" /mod/etc/reg/extra.reg "$pkg|$title|$sec_level|$id"
		rm -f /mod/var/cache/extras
		touch /mod/var/cache/menu.stale
		;;
	file)
		if [ $# -lt 6 ]; then
			echo "Error: $0 file $2: Registration without <pkg> argument is deprecated. Please switch to the new style. See /usr/bin/modreg for more details." >&2
			exit 1
		else
			pkg=$2 id=$3 title=$4 sec_level=$5 desc=$6
#
# With the new style of registering files, each file is associated with a
# package explicitly. Hence, $id needs only be unique within a package.
#
# This style has the following advantages:
#  1. All files of a package can be unregistered at once: modunreg file "$pkg"
#  2. Files may be grouped/associated with a package in the Web interface
#  3. We can derive the default locations of the description files:
#     /mod/etc/default.$pkg/ and /tmp/flash/$pkg/
#
# If $desc is an absolute path it is taken as it is (previous behaviour).
# A relative path chooses from the default description file or the
# overriding version at the standard locations. The resulting path is stored.
#
			oldstyle_id="${pkg}__$id"
			case $desc in
				/*) deffile=$desc ;;
				*)
					deffile="/tmp/flash/$pkg/$desc.def"
					if [ ! -r "$deffile" ]; then
						deffile="/mod/etc/default.$pkg/$desc.def"
					fi
					;;
			esac
			file_exists_or_die "$deffile"
			replace "^$pkg|$id|" /mod/etc/reg/file.reg "$pkg|$id|$title|$sec_level|$deffile"
		fi
		touch /mod/var/cache/menu.stale
		;;
	status)
		pkg=$2 title=$3 id=${4:-status}
		file_exists_or_die "/mod/usr/lib/cgi-bin/$pkg/$id.cgi"
		replace "^$2|.*|$id\$" /mod/etc/reg/status.reg "$2|$3|$id"
		touch /mod/var/cache/menu.stale
		;;
	daemon)
		shift
		TEMP=$(getopt -o dhn:r:p: --long disable,hide,name:,rc-script:,package: -n "$(basename "$PROGRAM")" -- "$@")

		disable=false
		hide=false
		description=
		package=
		[ $? == 0 ] || usage
		eval set -- "$TEMP"

		while true; do
			case $1 in
				-d|--disable)   disable=true; shift ;;
				-h|--hide)      hide=true; shift ;;
				-n|--name)      description=$2; shift 2 ;;
				-r|--rc-script) rcscript=$2; shift 2 ;;
				-p|--package)   package=$2; shift 2 ;;
				--)             shift ; break ;;
				*) echo "Internal error!" ; exit 1 ;;
			esac
		done

		[ -n "$1" ] || usage
		daemon=$1
		: ${description:=$daemon}
		: ${rcscript:=rc.$daemon}
		: ${package:=$daemon}

		file_exists_or_die "/mod/etc/init.d/$rcscript"

		replace "^$daemon|.*|$package\$" /mod/etc/reg/daemon.reg "$daemon|$description|$rcscript|$disable|$hide|$package"
		;;
	*)
		usage
		;;
esac

exit 0