1#!/usr/local/bin/bash
2#*********************************************************************
3#** prefix
4#*********************************************************************
5#** @(#)$Id: prefix 7988 2006-10-27 18:39:02Z spadkins $
6#*********************************************************************
7
8export OLD_PREFIX=$PREFIX
9export PREFIX=${PREFIX:-/usr/local}
10
11if [[ -f "$HOME/.prefixes" ]]      # check if multiple environments exist
12then
13   export PREFIXES_FILE=$HOME/.prefixes
14else
15   export PREFIXES_FILE=/etc/prefixes
16fi
17
18print_usage ()
19{
20   echo
21   echo "The prefix script allows you to manage multiple installations of"
22   echo "a set of software installed on the same machine.  It helps in this"
23   echo "by taking care of the reconfiguration of environment variables"
24   echo "appropriate for each installation.  It is assumed that the"
25   echo "software for each installation is all under a single directory"
26   echo "whose name is assigned to an environment variable called PREFIX. "
27   echo
28   echo "This arrangement of enabling multiple installations of software"
29   echo "on a single machine is useful at many times.  On a single server,"
30   echo "it can provide for development, test, and production installations"
31   echo "of software.  Alternatively, on development servers,"
32   echo "it allows for multiple development \"sandboxes\", one for each"
33   echo "developer.  On production servers, it allows for multiple versions"
34   echo "of the production software to be installed.  One might be the currently"
35   echo "running software, one the previous software kept online as a fall-back,"
36   echo "and one a new release of software which is scheduled to be brought"
37   echo "online soon."
38   echo
39   echo "There are three usages of the prefix script:"
40   echo
41   echo "  (1) The interactive usage should be placed as the last line"
42   echo "      of a user's ".profile".  The user must be running the"
43   echo "      Korn shell (ksh) or the Bourne Again shell (bash)."
44   echo "      The user is prompted to enter one of the known PREFIX locations,"
45   echo "      specified in the \$HOME/.prefixes file or the /etc/prefixes file."
46   echo "      During configuration, the \$PREFIX/.prefixrc file is sourced"
47   echo "      in order to accomplish environment-specific configurations."
48   echo "  (2) The non-interactive user configuration does not consult"
49   echo "      \$HOME/.prefixes or /etc/prefixes or prompt the user, but merely"
50   echo "      configures the environment in accordance with the cmd line argument."
51   echo "  (3) The batch command usage is mainly for running commands from"
52   echo "      cron or running commands in another environment without changing"
53   echo "      to that environment."
54   echo
55   echo "Usage (1): . prefix                     (sets up environment)"
56   echo "      (2): . prefix <prefix>            (non-interactive setup)"
57   echo "      (3): prefix <prefix> <cmd> <args> (runs cmd configured for PREFIX)"
58   echo
59}
60
61if [[ "$1" = "-?" ]]
62then
63   print_usage
64else
65   export PREFIX_CMD=""
66   if [[ "$1" != "" ]]
67   then
68      export NEW_PREFIX="$1"
69      export PREFIX_ASK="no"
70      shift
71      if [[ $# -gt 0 ]]
72      then
73         export PREFIX_CMD="$*"
74         set --                             # clear cmd line args
75      fi
76   fi
77
78   if [[ "$PREFIX_ASK" != "no" ]]
79   then
80      if [[ -f "$PREFIXES_FILE" ]]     # check if multiple environments exist
81      then
82         export NUM_PREFIXES=$(wc -l < "$PREFIXES_FILE")
83         if [[ "$NEW_PREFIX" = "" ]]
84         then
85            export NEW_PREFIX=$(head -1 "$PREFIXES_FILE")
86         fi
87
88         if [[ "$PREFIX_ASK" != "no" && "$NUM_PREFIXES" -gt 1 ]]
89         then
90            echo "========================================================"
91            echo "SET UP SOFTWARE PREFIX (ROOT DIRECTORY FOR THE SOFTWARE)"
92            echo "========================================================"
93            export PS3="Select a directory: "
94            select NEW_PREFIX in $(sed 's/#.*//' "$PREFIXES_FILE")
95            do
96               break
97            done
98            if [[ "$NEW_PREFIX" = "" ]]
99            then
100               export NEW_PREFIX="$REPLY"
101            fi
102         fi
103      fi
104   fi
105
106   if [[ "$NEW_PREFIX" = "" ]]
107   then
108      echo
109      echo "ERROR: Please specify a PREFIX or create a ~/.prefixes file"
110      echo "       to enable interactive usage."
111      echo
112      print_usage
113   elif [[ ! -d $NEW_PREFIX/. ]]
114   then
115      echo
116      echo "ERROR: Directory [$NEW_PREFIX] does not exist."
117      echo
118      print_usage
119   else
120      export PREFIX=$NEW_PREFIX
121      if [[ -t 1 && "$PREFIX_CMD" = "" ]]
122      then
123         echo "Configuring Software Prefix [$NEW_PREFIX]."
124      fi
125
126      ######################################################
127      # Remove all PATH references before adding them back
128      ######################################################
129      export SED_CLEAN_PATH=""
130      export SED_CLEAN_LIBPATH=""
131      export SED_CLEAN_MANPATH=""
132
133      for AUX_DIR in $(echo "$OLD_PREFIX:$PRE_PREFIX:$POST_PREFIX" | sed -e 's/^/:/' -e 's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
134      do
135         SED_CLEAN_PATH="$SED_CLEAN_PATH -e s!:$AUX_DIR/bin!!g"
136         SED_CLEAN_LIBPATH="$SED_CLEAN_LIBPATH -e s!:$AUX_DIR/lib!!g"
137         SED_CLEAN_MANPATH="$SED_CLEAN_MANPATH -e s!:$AUX_DIR/man!!g -e s!:$AUX_DIR/share/man!!g"
138      done
139
140      # Remove old references from the PATH
141      export PATH=`echo $PATH | \
142             sed -e "s/^:*/:/" \
143                 -e "s/:*$/:/" \
144                 -e "s/:::*/:/g" \
145                 $SED_CLEAN_PATH \
146                 -e "s/::*$//" \
147                 -e "s/^::*//"`
148
149      # Remove old references from the LD_LIBRARY_PATH
150      OLD_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
151      if [ "$OLD_LD_LIBRARY_PATH" = "" ]
152      then
153          OLD_LD_LIBRARY_PATH="/usr/local/lib:/usr/lib:/lib"
154      fi
155      export LD_LIBRARY_PATH=""
156      export LD_LIBRARY_PATH=`echo $OLD_LD_LIBRARY_PATH | \
157             sed -e "s/^:*/:/" \
158                 -e "s/:*$/:/" \
159                 -e "s/:::*/:/g" \
160                 $SED_CLEAN_LIBPATH \
161                 -e "s/::*$//" \
162                 -e "s/^::*//"`
163      unset OLD_LD_LIBRARY_PATH
164
165      # Remove old references from the LIBPATH
166      OLD_LIBPATH="$LIBPATH"
167      if [ "$OLD_LIBPATH" = "" ]
168      then
169          OLD_LIBPATH="/usr/local/lib:/usr/lib:/lib"
170      fi
171      export LIBPATH=""
172      export LIBPATH=`echo $OLD_LIBPATH | \
173             sed -e "s/^:*/:/" \
174                 -e "s/:*$/:/" \
175                 -e "s/:::*/:/g" \
176                 $SED_CLEAN_LIBPATH \
177                 -e "s/::*$//" \
178                 -e "s/^::*//"`
179      unset OLD_LIBPATH
180
181      # Remove old references from the MANPATH
182      if [ "$MANPATH" = "" ]
183      then
184          MANPATH="/usr/local/man:/usr/share/man:/usr/man:/man"
185      fi
186      export MANPATH=`echo $MANPATH | \
187             sed -e "s/^:*/:/" \
188                 -e "s/:*$/:/" \
189                 -e "s/:::*/:/g" \
190                 $SED_CLEAN_MANPATH \
191                 -e "s/::*$//" \
192                 -e "s/^::*//"`
193
194      ######################################################
195      # source the environment's .rmsrc file if it exists
196      ######################################################
197      for AUX_DIR in $(echo $POST_PREFIX | sed -e 's/^/:/' -e 's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
198      do
199         if [[ -f $AUX_DIR/.prefixrc ]]
200         then
201            . $AUX_DIR/.prefixrc
202         fi
203      done
204
205      if [[ -f $PREFIX/.prefixrc ]]
206      then
207         . $PREFIX/.prefixrc
208      fi
209
210      for AUX_DIR in $(echo $PRE_PREFIX | sed -e 's/^/:/' -e 's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
211      do
212         if [[ -f $AUX_DIR/.prefixrc ]]
213         then
214            . $AUX_DIR/.prefixrc
215         fi
216      done
217
218      ######################################################
219      # source the user's .prefixrc file if it exists
220      ######################################################
221      if [[ -f ~/.prefixrc ]]
222      then
223         . ~/.prefixrc
224      fi
225
226      ##########################################################
227      # Set appropriate defaults for common variables
228      ##########################################################
229
230      export AUX_PATH=""
231      export AUX_LIBPATH=""
232      export AUX_MANPATH=""
233      export PREFIX_INCLUDES=""
234      for AUX_DIR in $(echo $PRE_PREFIX | sed -e 's/^/:/' -e 's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
235      do
236         AUX_PATH="$AUX_PATH$AUX_DIR/bin:"
237         AUX_LIBPATH="$AUX_LIBPATH$AUX_DIR/lib:"
238         AUX_MANPATH="$AUX_MANPATH$AUX_DIR/man:"
239         PREFIX_INCLUDES="$PREFIX_INCLUDES -I$AUX_DIR/include"
240      done
241
242      PREFIX_INCLUDES="$PREFIX_INCLUDES -I$PREFIX/include"
243
244      export AUX_PATHPOST=""
245      export AUX_LIBPATHPOST=""
246      export AUX_MANPATHPOST=""
247      for AUX_DIR in $(echo $POST_PREFIX | sed -e 's/^/:/' -e 's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
248      do
249         AUX_PATHPOST="$AUX_PATHPOST:$AUX_DIR/bin"
250         AUX_LIBPATHPOST="$AUX_LIBPATHPOST:$AUX_DIR/lib"
251         AUX_MANPATHPOST="$AUX_MANPATHPOST:$AUX_DIR/man"
252         PREFIX_INCLUDES="$PREFIX_INCLUDES -I$AUX_DIR/include"
253      done
254
255      # Add new references into the PATH
256      export PATH=$AUX_PATH$PREFIX/bin$AUX_PATHPOST:$PATH
257
258      # Add new references into the LD_LIBRARY_PATH
259      if [[ "$LD_LIBRARY_PATH" = "" ]]
260      then
261         export LD_LIBRARY_PATH=$AUX_LIBPATH$PREFIX/lib$AUX_LIBPATHPOST
262      else
263         export LD_LIBRARY_PATH=$AUX_LIBPATH$PREFIX/lib$AUX_LIBPATHPOST:$LD_LIBRARY_PATH
264      fi
265
266      # Add new references into the LIBPATH
267      if [[ "$LIBPATH" = "" ]]
268      then
269         export LIBPATH=$AUX_LIBPATH$PREFIX/lib$AUX_LIBPATHPOST
270      else
271         export LIBPATH=$AUX_LIBPATH$PREFIX/lib$AUX_LIBPATHPOST:$LIBPATH
272      fi
273
274      # Add new references into the MANPATH
275      if [[ "$MANPATH" = "" ]]
276      then
277         # guess at the base MANPATH
278         MANPATH=$(ls -d /man /*/man /*/*/man 2> /dev/null | tr '\n' ':' | sed 's/:$//')
279      fi
280      # Add new references into the MANPATH
281      if [[ "$MANPATH" = "" ]]
282      then
283         export MANPATH=$AUX_MANPATH$PREFIX/share/man:$PREFIX/man$AUX_MANPATHPOST
284      else
285         export MANPATH=$AUX_MANPATH$PREFIX/share/man:$PREFIX/man$AUX_MANPATHPOST:$MANPATH
286      fi
287
288   fi
289
290   if [[ "$PREFIX_CMD" != "" ]]
291   then
292      exec $PREFIX_CMD
293   fi
294fi
295
296set --   # clear cmd line args
297
298unset PREFIXES_FILE
299unset PREFIX_CMD
300unset PREFIX_ASK
301unset NUM_PREFIXES
302unset NEW_PREFIX
303unset OLD_PREFIX
304
305unset SED_CLEAN_PATH
306unset SED_CLEAN_LIBPATH
307unset SED_CLEAN_MANPATH
308unset AUX_PATH
309unset AUX_LIBPATH
310unset AUX_MANPATH
311unset AUX_DIR
312unset AUX_PATHPOST
313unset AUX_LIBPATHPOST
314unset AUX_MANPATHPOST
315
316