493dd5e90091a13f55d4b7c01a6216da1de9a51d
[gnulib.git] / build-aux / relocatable.sh.in
1 # The functions in this file provide support for relocatability of
2 # shell scripts.  They should be included near the beginning of each
3 # shell script in a relocatable program, by adding @relocatable_sh@
4 # and causing the script to be expanded with AC_CONFIG_FILES.  A
5 # small amount of additional code must be added and adapted to the
6 # package by hand; see doc/relocatable-maint.texi (in Gnulib) for
7 # details.
8 #
9 # Copyright (C) 2003, 2005-2007 Free Software Foundation, Inc.
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
14 # any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #
25
26 # func_tmpdir
27 # creates a temporary directory.
28 # Sets variable
29 # - tmp             pathname of freshly created temporary directory
30 func_tmpdir ()
31 {
32   # Use the environment variable TMPDIR, falling back to /tmp. This allows
33   # users to specify a different temporary directory, for example, if their
34   # /tmp is filled up or too small.
35   : ${TMPDIR=/tmp}
36   {
37     # Use the mktemp program if available. If not available, hide the error
38     # message.
39     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
40     test -n "$tmp" && test -d "$tmp"
41   } ||
42   {
43     # Use a simple mkdir command. It is guaranteed to fail if the directory
44     # already exists.  $RANDOM is bash specific and expands to empty in shells
45     # other than bash, ksh and zsh.  Its use does not increase security;
46     # rather, it minimizes the probability of failure in a very cluttered /tmp
47     # directory.
48     tmp=$TMPDIR/gl$$-$RANDOM
49     (umask 077 && mkdir "$tmp")
50   } ||
51   {
52     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
53     { (exit 1); exit 1; }
54   }
55 }
56
57 # Support for relocatability.
58 func_find_curr_installdir ()
59 {
60   # Determine curr_installdir, even taking into account symlinks.
61   curr_executable="$0"
62   case "$curr_executable" in
63     */* | *\\*) ;;
64     *) # Need to look in the PATH.
65       if test "${PATH_SEPARATOR+set}" != set; then
66         func_tmpdir
67         { echo "#! /bin/sh"; echo "exit 0"; } > "$tmp"/conf.sh
68         chmod +x "$tmp"/conf.sh
69         if (PATH="/nonexistent;$tmp"; conf.sh) >/dev/null 2>&1; then
70           PATH_SEPARATOR=';'
71         else
72           PATH_SEPARATOR=:
73         fi
74         rm -rf "$tmp"
75       fi
76       save_IFS="$IFS"; IFS="$PATH_SEPARATOR"
77       for dir in $PATH; do
78         IFS="$save_IFS"
79         test -z "$dir" && dir=.
80         for exec_ext in ''; do
81           if test -f "$dir/$curr_executable$exec_ext"; then
82             curr_executable="$dir/$curr_executable$exec_ext"
83             break 2
84           fi
85         done
86       done
87       IFS="$save_IFS"
88       ;;
89   esac
90   # Make absolute.
91   case "$curr_executable" in
92     /* | ?:/* | ?:\\*) ;;
93     *) curr_executable=`pwd`/"$curr_executable" ;;
94   esac
95   # Resolve symlinks.
96   sed_dirname='s,/[^/]*$,,'
97   sed_linkdest='s,^.* -> \(.*\),\1,p'
98   while : ; do
99     lsline=`LC_ALL=C ls -l "$curr_executable"`
100     case "$lsline" in
101       *" -> "*)
102         linkdest=`echo "$lsline" | sed -n -e "$sed_linkdest"`
103         case "$linkdest" in
104           /* | ?:/* | ?:\\*) curr_executable="$linkdest" ;;
105           *) curr_executable=`echo "$curr_executable" | sed -e "$sed_dirname"`/"$linkdest" ;;
106         esac ;;
107       *) break ;;
108     esac
109   done
110   curr_installdir=`echo "$curr_executable" | sed -e 's,/[^/]*$,,'`
111   # Canonicalize.
112   curr_installdir=`cd "$curr_installdir" && pwd`
113 }
114 func_find_prefixes ()
115 {
116   # Compute the original/current installation prefixes by stripping the
117   # trailing directories off the original/current installation directories.
118   orig_installprefix="$orig_installdir"
119   curr_installprefix="$curr_installdir"
120   while true; do
121     orig_last=`echo "$orig_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
122     curr_last=`echo "$curr_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
123     if test -z "$orig_last" || test -z "$curr_last"; then
124       break
125     fi
126     if test "$orig_last" != "$curr_last"; then
127       break
128     fi
129     orig_installprefix=`echo "$orig_installprefix" | sed -e 's,/[^/]*$,,'`
130     curr_installprefix=`echo "$curr_installprefix" | sed -e 's,/[^/]*$,,'`
131   done
132 }