#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../runit/djb-config
# Copyright (C) 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

# e.g. $1: admin mail net ...
#      $2: $sbindir $bindir
#
pkg_djb_hierarchy=$1

#
# prepare root links for DJB filesystem hierarchy inside chrooted env.
#
if [ "$SDECFG_PKG_DJB_LAYOUT" == "1" ]; then
	for x in package command service; do
		eval "pkg_djb_${x}dir='$root/$x'"
		var_append flistroot ' ' $x
	done
else
	pkg_djb_packagedir=
	pkg_djb_commanddir="$root${2:-$bindir}"
	pkg_djb_servicedir="$root/var/service"
fi

# create them if it doesn't exist
for x in package command service; do
	y=$( eval echo \$pkg_djb_${x}dir )
	if [ -n "$y" ]; then
		mkdir -p "$y"
		# TODO: research where 1755 comes from
		[ "$x" = command ] || chmod 1755 "$y"
	fi
done

# $logdir
if [ "${localstatedir%$pkg}" != "$localstatedir" ]; then
	logdir=$localstatedir/log
else
	logdir=$localstatedir/log/$pkg
fi

# $sysconfdir
if [ "${sysconfdir%$pkg}" == "$sysconfdir" ]; then
	sysconfdir=$sysconfdir/$pkg
fi

if [ -z "$pkg_djb_hierarchy" ]; then
	abort "djb-config: must specify hierarchy /package/\$hierarchy."
fi

srcdir="$pkg_djb_hierarchy/$pkg-$ver"

if [ "$createdocs" != "0" ]; then
	# let us handle the installation of the docs
	createdocs=0
	hook_add postdoc 2 'pkg_djb_installdoc $pkg_djb_hierarchy'
fi

# custmain
#
custmain="pkg_djb_main"
pkg_djb_main() {
        hook_eval premake
        ./package/compile
        hook_eval inmake
        pkg_djb_installpackage $pkg_djb_hierarchy
        hook_eval postmake
}

pkg_djb_substitute() {
	sed \
	    -e "s,D_commanddir,${pkg_djb_commanddir#$root},g" \
	    -e "s,D_servicedir,${pkg_djb_servicedir#$root},g" \
	    -e "s,D_logdir,$logdir,g" \
	    $1
}

# installs the package in DJB way if requested
#
pkg_djb_installpackage() {
	# requires ./package/commands
	# and files in ./command

	local x= y= cmddir=
	local package=$1

	if [ "$pkg_djb_packagedir" ]; then
		x="$pkg_djb_packagedir/$package"
		echo "Creating ${x#$root}/{$pkg,$pkg-$ver}..."
		mkdir -p "$x/$pkg-$ver/command"
		ln -svnf "$pkg-$ver" "$x/$pkg"

		cmddir="$x/$pkg-$ver/command"
	else
		cmddir="$pkg_djb_commanddir"
	fi

	echo "Installing commands at ${cmddir#$root}..."
	for x in `cat ./package/commands`; do
		cp -vfp "./command/$x" "$cmddir/"
		[ -z "$pkg_djb_packagedir" ] || ln -snvf "${cmddir#$root}/$x" "$pkg_djb_commanddir/$x"
	done

}

# installs documentation to package directory or $docdir as requested
#
pkg_djb_installdoc() {
	local x package=$1
	local docpkg

	echo 'Copying documentation....'
	if [ "$pkg_djb_packagedir" ]; then
		# $pkg_djb_* includes $root, $docdir doesn't.
		docpkg=${pkg_djb_packagedir#$root}/$package/$pkg-$ver/package
		docdir=${pkg_djb_packagedir#$root}/$package/$pkg-$ver/doc
		mkdir -p $root$docpkg $root$docdir
	else
		docpkg=$docdir
	fi

	# ./package
	for x in `ls ./package`; do
		case $x in
			compile|install*|upgrade)
				/bin/true ;;
			*)	cp -vf ./package/$x "$root$docpkg" ;;
		esac
	done

	# ./doc
	if [ -d doc ]; then
		cp -rLv ./doc/* "$root$docdir"
	fi

	# ./man
	if [ -d man ]; then
		echo "Installing man pages ... "
		for x in $( cd man; ls -1 *.[1-9] | sed -e 's,.*\.\([1-9]\),\1,g' | sort -u ); do
			gzip -9 ./man/*.$x
			cp -vf ./man/*.$x.gz $root/$mandir/man$x/
		done
	fi
}