1#!/bin/sh
2
3#*************************************************************************
4#
5# mklinks - makes (or removes) soft-links from distribution directory to
6#           more appropriate places (typically under /usr/local).
7#
8# Maurice LeBrun (mjl@dino.ph.utexas.edu)
9# IFS, University of Texas at Austin
10# May 17, 1994
11#
12# Arguments:
13#	-a	Add soft links
14#	-n	Print commands without executing
15#	-r	Remove soft links
16#
17# Anything else results in a short help message being printed.
18#
19# I wrote this script to simplify the business of installing software
20# distributions.  With a lot of packages continuing to evolve at a pretty
21# good clip, it helps to be able to update without too much hassle.  Also,
22# it is best to provide a fairly consistent organization of the files on
23# disk, one that fits into just about anyone's system management scheme.
24# The method I use works whether you are system manager or just some poor
25# slob user :-).
26#
27# The idea is to type "make install" in the build directory, with the
28# install target set to some empty directory.  A good choice is
29# /usr/local/<pkg><ver>, where <pkg> is the software package name and
30# <ver> is the version.  This way you can have multiple versions of the
31# package installed, with only one of them easily accessible.  Or you can
32# leave out the version number.
33#
34# By softlinking the package distribution files into the usual /usr/local
35# areas you (a) avoid requiring a change in PATH to get at the binaries,
36# (b) avoid requiring changes to makefiles to get include and lib
37# settings, (c) avoid requiring changes to MANPATH settings to allow
38# access to the man pages, and (d) have an easy way to do upgrades or
39# degrades.
40#
41# The main difficulty as I see it with using softlinks from /usr/local
42# areas into /usr/local/bin, /usr/local/lib, /usr/local/include, and
43# /usr/local/man, is that once created, the softlinks are hard to get rid
44# of.  If you decide to delete the package, you must manually delete the
45# softlinks as well.  Switching between versions is an onerous task,
46# especially if you want to back down to a previous revision.  Therefore,
47# a fundamental capability of this script is to allow easy removal of all
48# created softlinks (-r option).
49#
50# Note: the default is to only link librarys under lib into the target
51# dir, since some packages store lots of data files under lib/.
52#
53#*************************************************************************
54
55# Miscellaneous settings
56
57NAME=$0			# Name of script
58CWD=`pwd`		# Current directory
59LINK_MAN=1		# Install man pages (set to null to disable)
60LINK_LIB_ALL=0		# Link lib/* rather than just lib/lib*
61
62# Infer package name from directory name
63# Not all systems have "basename" so use sed for this.
64
65PKG=`echo $CWD | sed 's%/.*/%%'`
66
67# Get base target directory -- the links will go into $INSTALL_DIR/bin,
68# $INSTALL_DIR/lib, etc.  Since the package is typically in
69# /usr/local/<pkg>, INSTALL_DIR is just one directory up.  Use an absolute
70# path name rather than relative (..) since it's less confusing.
71
72INSTALL_DIR=`echo $CWD | sed 's%/[^/][^/]*$%%'`
73
74# Per-package defaults:
75#    PKG_NAME		Name of package, for help/error messages
76#    REFERENCE_FILE	Name of an installed file to assist internal logic
77
78case "$PKG" in
79
80    perl* )
81	PKG_NAME="perl"
82	REFERENCE_FILE="$INSTALL_DIR/bin/perl"
83    ;;
84
85    plplot* )
86	PKG_NAME="PLplot"
87	REFERENCE_FILE="$INSTALL_DIR/bin/plrender"
88    ;;
89
90    python* )
91	PKG_NAME="Python"
92	REFERENCE_FILE="$INSTALL_DIR/bin/python"
93	LINK_LIB_ALL=1
94    ;;
95
96    tcl*|tk* )
97	PKG_NAME="Tcl/TK/etc"
98	REFERENCE_FILE="$INSTALL_DIR/bin/tclsh"
99	LINK_LIB_ALL=1
100    ;;
101
102    * )
103	echo "Unrecognized package; aborting"
104	exit
105    ;;
106esac
107
108# Account for differences in /bin/sh semantics wrt soft links.
109
110IF_SOFT="-h"
111if test `uname` = "AIX"; then
112    IF_SOFT="-L"
113fi
114
115# Define a symbol for echo to save a little bit of space.
116
117e=echo
118
119#*************************************************************************
120
121# Spits out help message, then exits
122
123help () {
124
125$e "Usage: $NAME [-n] [-a | -r]"
126$e "       Creates (-a) or removes (-r) soft links to $PKG_NAME files."
127$e "       Currently configured to put soft links under $INSTALL_DIR."
128$e ""
129$e "       If -n is specified, commands are printed with no action taken."
130$e "       The -n flag must come first, if specified."
131    exit 1
132}
133
134#*************************************************************************
135
136# Adds one or many soft-links.  Creates directory if necessary.
137# $1 - subdir name
138# $2 - file spec
139
140add_link () {
141
142    if test -d "$CWD/$1"; then
143
144	if test ! -d "$INSTALL_DIR/$1"; then
145	    if test "$DO_NOTHING"; then
146		echo "mkdir -p $INSTALL_DIR/$1"
147	    else
148		mkdir -p $INSTALL_DIR/$1
149	    fi
150	fi
151
152	# Do filename globbing here so we can catch cases where no
153	# filenames match.
154
155	for file in $CWD/$1/$2; do
156	    if test -r $file; then
157		if test "$DO_NOTHING"; then
158		    echo "ln -s $file  $INSTALL_DIR/$1"
159		else
160		    ln -s $file $INSTALL_DIR/$1
161		fi
162	    fi
163	done
164    fi
165}
166
167#*************************************************************************
168
169# Removes a single soft-link
170# $1 - link name (relative to $INSTALL_DIR)
171
172rm_link () {
173
174    if test $IF_SOFT "$INSTALL_DIR/$1"; then
175	if test "$DO_NOTHING"; then
176	    echo "rm $INSTALL_DIR/$1"
177	else
178	    rm $INSTALL_DIR/$1
179	fi
180    fi
181}
182
183#*************************************************************************
184
185# Removes multiple soft-links
186# $1 through $# - link specs (relative to $INSTALL_DIR)
187
188rm_links () {
189    for file in $*; do
190	rm_link $file
191    done
192}
193
194#*************************************************************************
195
196# Add links
197
198Add () {
199
200# Bomb out if we're not starting clean
201
202    if test $IF_SOFT "$REFERENCE_FILE"; then
203	echo "Must remove old links first -- use \"$NAME -r\"."
204	exit 1
205    fi
206
207# Set up links
208
209    echo "Adding links from $CWD to $INSTALL_DIR"
210
211    add_link "bin"	"*"
212    add_link "include"	"*.h"
213
214    if test "$LINK_LIB_ALL" = 1; then
215	add_link "lib"	"*"
216    else
217	add_link "lib"	"lib*"
218    fi
219
220    if test "$LINK_MAN"; then
221	add_link "man/man1" "*.1"
222	add_link "man/man3" "*.3"
223	add_link "man/mann" "*.n"
224    fi
225}
226
227#*************************************************************************
228
229# Remove links
230
231Remove () {
232
233# Bomb out if links already removed.
234
235    if test ! $IF_SOFT "$REFERENCE_FILE"; then
236	echo 'Soft links already removed.'
237	exit 1
238    fi
239
240# Delete links
241# Here we reglob to determine what links need deleting.  Note that in each
242# case, we check to make sure it really is a soft link.
243
244    echo "Removing links from $CWD to $INSTALL_DIR"
245
246    rm_links bin/* lib/* include/*.h
247
248    if test "$LINK_MAN"; then
249	rm_links man/man1/*.1 man/man3/*.3 man/mann/*.n
250    fi
251}
252
253#*************************************************************************
254
255# Call the necessary function to do the job.
256
257if test "$1" = "-n"; then
258    DO_NOTHING=1
259    shift
260fi
261
262if test "$1" = '-a'; then
263    Add
264
265elif test "$1" = '-r'; then
266    Remove
267
268else
269    help
270fi
271exit 0
272