938d9dc5782d06aeedad9acc38432cda7ca6ccca
[platform/upstream/bash.git] / support / fixlinks
1 #! /bin/sh
2 #
3 # fixlinks - make symlinks in the bash source tree so that there is
4 #            exactly one version of any given source file.
5 #
6 #
7
8 SRCDIR=.
9 while [ $# -gt 0 ]; do
10         case "$1" in
11         -s)     shift; SRCDIR=$1 ;;
12         -u)     unfix=yes ;;
13         -h)     hardlinks=yes ;;
14         -*)     echo "$0: $1: bad option" 1>&2
15                 echo "$0: usage: $0 [-hu] [-s srcdir] [linkmap]" 1>&2
16                 exit 1;;
17         *)      break ;;
18         esac
19         shift
20 done
21
22 if [ ! -d $SRCDIR/builtins ]; then
23         echo "$0: must be run with valid -s argument or from source directory" 1>&2
24         exit 1
25 fi
26
27 if [ $# -eq 0 ]; then
28         linkfile=$SRCDIR/support/SYMLINKS
29 else
30         linkfile=$1     
31 fi
32
33 if [ ! -f "$linkfile" ]; then
34         echo "$0: symlink map file \`$linkfile' does not exist"
35         exit 1
36 fi
37
38 rm_ltmp=false
39 LINKTEMP=`mktemp -t linktmp.XXXXXXXX 2>/dev/null`
40 if [ -z "$LINKTEMP" ]; then
41         : ${TMPDIR:=/tmp}
42         LINKTEMP=${TMPDIR}/linktmp.$$
43         rm_ltmp=true
44 fi
45
46 $rm_ltmp && rm -f ${LINKTEMP}
47 # if the user specified hard links, then do that.  otherwise, try to use
48 # symlinks if they're present
49 if [ -n "$hardlinks" ]; then
50         LN=ln
51 elif (ln -s /dev/null ${LINKTEMP}) >/dev/null 2>&1; then
52         LN="ln -s"
53 else
54         LN=ln
55 fi
56 rm -f ${LINKTEMP}
57
58 while read name target
59 do
60         case "$name" in
61         \#*)    continue;;
62         esac
63
64         rm -f $name
65         case "$unfix" in
66         yes)    dirname=`expr "$name" ':' '^\(.*\)/[^/]*'`
67                 [ -z "$dirname" ] && dirname=.
68                 cp $dirname/$target $name
69                 echo $target copied to $name ;;
70         *)      $LN $target $name ; echo "$name -> $target" ;;
71         esac
72
73 done < $linkfile
74
75 exit 0