summaryrefslogtreecommitdiff
path: root/lang/perl5/files/perllink.in
blob: 44dfacca2d5f0cce45d14254dad890ad76fe1f8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!@SH@
#	$NetBSD: perllink.in,v 1.1 2005/08/06 06:18:45 jlam 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 and <prefix>/man/man1 that
# match the files in the packlists.
#	

perlprog="@PERL5@"

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"

# 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/man/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/man/man1"

echo __dummy__ | perl -pe '0' $opt_packlists |
{ while read file junk; do
	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 -