# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/package/mnemoc/runit/djb-config
# ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
# 
# 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; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---
# e.g. $1: admin mail net ...
#      $2: $sbindir $bindir
#
pkg_djb_hierarchy=$1
pkg_djb_altdir=${2:-$bindir}

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

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

if [ "$createdocs" != "0" -a "$ROCKCFG_CREATE_DOCS" == "1" ]; then
	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 $pkg_djb_altdir
        hook_eval postmake
}

# why to use this safe stuff?
# because we can be updating... sure?
# to honor daemontools behavior :-)
#
pkg_djb_safecopy() {
	echo "'${1#root}/$3' -> '${2#root}/$3'"
	rm -f $2/$3'{new}'
	cp -p $1/$3 $2/$3'{new}'
	mv -f $2/$3'{new}' $2/$3
}

pkg_djb_safelink() {
	echo "create symbolic link '${1#root}/$3' to '${2#root}/$3'"
	rm -f $2/$3'{new}'
	ln -s $1/$3 $2/$3'{new}'
	mv -f $2/$3'{new}' $2/$3
}

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

	local x y
	local package=$1
	local altdir=$2

	if [ "$pkg_djb_packagedir" ]; then
		echo 'Copying commands to package directory...'
		mkdir -p $pkg_djb_packagedir/$package/$pkg-$ver/command
		for x in `cat ./package/commands`; do
			pkg_djb_safecopy ./command \
				$pkg_djb_packagedir/$package/$pkg-$ver/command $x
		done

		echo "Creating symlink $pkg -> $pkg-$ver..."
		rm -f $pkg_djb_packagedir/$package/$pkg
		ln -sv $pkg-$ver $pkg_djb_packagedir/$package/$pkg
	fi

	if [ "$pkg_djb_commanddir" ]; then
		echo "Making command links in $pkg_djb_commanddir..."
		y=pkg_djb_safelink
	else
		echo 'Copying commands to $altdir...'
		pkg_djb_commanddir=$altdir
		y=pkg_djb_safecopy
	fi
	mkdir -p $pkg_djb_commanddir
	for x in `cat package/commands`; do
		$y $pkg_djb_packagedir/$package/$pkg-$ver/command \
			$pkg_djb_commanddir $x
	done
	
	# usr/local is evil :)
	#if [ "$pkg_djb_compatdir" ]; then	
	#	echo "Making compatibility links in $pkg_djb_compatdir..."
	#	mkdir -p $pkg_djb_compatdir
	#	for i in `cat package/commands`; do
	#		pkg_djb_safelink $pkg_djb_packagedir/$package/$pkg-$ver/command \
	#			$pkg_djb_compatdir $i
	#	done
	#fi
}

# 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
		docpkg=$pkg_djb_packagedir/$package/$pkg-$ver/package
		docdir=$pkg_djb_packagedir/$package/$pkg-$ver/doc
	else
		docpkg=$docdir
	fi
	mkdir -p $docpkg $docdir

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

	# ./doc
	if [ -d doc ]; then
		cp -rLv ./doc/* $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 $mandir/man$x/
		done
	fi
}