#!/bin/sh
#	$NetBSD: perllink.in,v 1.4 2012/03/04 10:26:57 tron Exp $
#
# Copyright (c) 2005 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Johnny C. Lam.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by the NetBSD
#        Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

#
# This script parses the packlists listed on the command line and either
# adds or deletes the symlinks in
#
#	<prefix>/bin
#	<prefix>/man/man1
#
# that match the files in the packlists.
#	

perlprog="/usr/pkg/bin/perl"

usage()
{
	echo 1>&2 "usage: $0 [-fnv] [-p prefix] add|delete [packlist ...]"
	exit 1
}

opt_action=
opt_force=
opt_noaction=
opt_prefix=
opt_packlists=
opt_verbose=

while test $# -gt 0; do
	opt="$1"
	case "$opt" in
	-f|--force)	opt_force=yes; shift ;;
	-n|--noaction)	opt_noaction=yes; shift ;;
	-p)		opt_prefix="$2"; shift; shift ;;
	--prefix=*)	opt_prefix="${1#--prefix=}"; shift ;;
	-v|--verbose)	opt_verbose=yes; shift ;;
	--)		break; shift ;;
	-*)		usage ;;
	[!-]*)		break ;;
	esac
done

# We expect the action argument, as well as any packlists, to be left
# in the argument list.
#
test $# -gt 0 || usage
opt_action="$1"; shift

# Get the values of various Config.pm values from perl.
eval `$perlprog -V:prefix -V:scriptdir -V:man1dir -V:vendorprefix -V:vendorscript -V:vendorman1dir -V:siteprefix -V:sitescript -V:siteman1dir -V:archlib`

# The remaining arguments, if any, are packlists.
if test $# -gt 0; then
	opt_packlists="$@"
elif test -f "$archlib/.packlist"; then
	opt_packlists="$archlib/.packlist"
fi

test -n "$opt_prefix" || opt_prefix="$prefix"

pkgmandir="man"

# Re-point variables so that if they are under $prefix, then they are
# recomputed to point under $opt_prefix.
#
case "$scriptdir" in
$prefix/*)	scriptdir="$opt_prefix/${scriptdir#$prefix/}" ;;
"")		scriptdir="$opt_prefix/bin" ;;
esac
case "$man1dir" in
$prefix/*)	man1dir="$opt_prefix/${man1dir#$prefix/}" ;;
"")		man1dir="$opt_prefix/$pkgmandir/man1" ;;
esac
case "$siteprefix" in
$prefix/*)	siteprefix="$opt_prefix/${siteprefix#$prefix/}" ;;
"")		siteprefix="$opt_prefix" ;;
esac
case "$sitescript" in
$prefix/*)	sitescript="$opt_prefix/${sitescript#$prefix/}" ;;
"")		sitescript="$scriptdir" ;;
esac
case "$siteman1dir" in
$prefix/*)	siteman1dir="$opt_prefix/${siteman1dir#$prefix/}" ;;
"")		siteman1dir="$man1dir" ;;
esac
case "$vendorprefix" in
$prefix/*)	vendorprefix="$opt_prefix/${vendorprefix#$prefix/}" ;;
"")		vendorprefix="$opt_prefix" ;;
esac
case "$vendorscript" in
$prefix/*)	vendorscript="$opt_prefix/${vendorscript#$prefix/}" ;;
"")		vendorscript="$scriptdir" ;;
esac
case "$vendorman1dir" in
$prefix/*)	vendorman1dir="$opt_prefix/${vendorman1dir#$prefix/}" ;;
"")		vendorman1dir="$man1dir" ;;
esac

destbindir="$opt_prefix/bin"
destman1dir="$opt_prefix/$pkgmandir/man1"

echo __dummy__ | $perlprog -pe '0' $opt_packlists |
{ while read file junk; do
	if [ ! -f "$file" ] && [ -f "$file.gz" ]; then
		file="$file.gz"
	fi

	case "$file" in
	$scriptdir/*|$sitescript/*|$vendorscript/*)  destdir="$destbindir" ;;
	$man1dir/*|$siteman1dir/*|$vendorman1dir/*)  destdir="$destman1dir" ;;
	esac

	case "$file" in
	$scriptdir/*)		dest="$destdir/${file#$scriptdir/}" ;;
	$sitescript/*)		dest="$destdir/${file#$sitescript/}" ;;
	$vendorscript/*)	dest="$destdir/${file#$vendorscript/}" ;;
	$man1dir/*)		dest="$destdir/${file#$man1dir/}" ;;
	$siteman1dir/*)		dest="$destdir/${file#$siteman1dir/}" ;;
	$vendorman1dir/*)	dest="$destdir/${file#$vendorman1dir/}" ;;
	*)			continue ;;
	esac

	test "$file" != "$dest" || continue

	# Set $src to be a relative symlink from $file into $destdir.
	file="${file#/}"
	destdir="${destdir#/}"
	while test "${file%%/*}" = "${destdir%%/*}"; do
		case "$file" in
		*/*)	file="${file#*/}" ;;
		*)	file= ;;
		esac
		case "$destdir" in
		*/*)	destdir="${destdir#*/}" ;;
		*)	destdir= ;;
		esac
	done
	src=""
	while test -n "$destdir"; do
		src="../$src"
		case "$destdir" in
		*/*)	destdir="${destdir#*/}" ;;
		*)	destdir= ;;
		esac
	done
	src="$src$file"

	case "$opt_action" in
	add|delete)
		if test -n "$opt_force"; then
			test -z "$opt_verbose" || \
				echo "print STDERR \"rm $dest\\n\";"
			test -n "$opt_noaction" || \
				echo "unlink(\"$dest\");"
		fi
		;;
	*)
		usage
		;;
	esac
	case "$opt_action" in
	add)
		test -z "$opt_verbose" || \
			echo "print STDERR \"ln -s $src $dest\\n\";"
		test -n "$opt_noaction" || \
			echo "symlink(\"$src\", \"$dest\");"
		;;
	esac
done;
echo "exit(0);"; } | $perlprog -w -
