1840b03fe520b172a001e0bf43e2fa8823e686f9
[platform/upstream/bash.git] / support / mkclone
1 #! /bin/bash
2 #
3 # mkclone - symlink every file appearing in $src/MANIFEST to a corresponding
4 #           file in the target directory ($1).  Directories specified in
5 #           MANIFEST are created in the target directory
6 #
7 # Copyright (C) 1996-2002 Free Software Foundation, Inc.
8 #
9 #   This program is free software: you can redistribute it and/or modify
10 #   it under the terms of the GNU General Public License as published by
11 #   the Free Software Foundation, either version 3 of the License, or
12 #   (at your option) any later version.
13 #
14 #   This program is distributed in the hope that it will be useful,
15 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #   GNU General Public License for more details.
18 #
19 #   You should have received a copy of the GNU General Public License
20 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 prog=`basename $0`
23
24 SRCDIR=src
25
26 USAGE="usage: $prog [-m manifest] [-s srcdir] [-v] [-d] [-h] target"
27 while getopts dhm:s:v opt
28 do
29         case "$opt" in
30         m)      MANIFEST=$OPTARG ;;
31         s)      SRCDIR=$OPTARG ;;
32         v)      verbose=y ;;
33         d)      ECHO=echo debug=y ;;
34         h)      hardlinks=y ;;
35         ?)      echo $USAGE >&2
36                 exit 2;;
37         esac
38 done
39
40 : ${MANIFEST:=${SRCDIR}/MANIFEST}
41
42 [ -n "$debug" ] && verbose=
43
44 shift $(( $OPTIND - 1 ))
45
46 if [ $# -lt 1 ]; then
47         echo $USAGE >&2
48         exit 2
49 fi
50
51 if [ ! -f $MANIFEST ]; then
52         echo "$prog: $MANIFEST: no such file or directory" >&2
53         echo "$prog: must be run with valid -s argument or from source directory" >&2
54         exit 1
55 fi
56
57 rm_ltmp=false
58 LINKTEMP=`mktemp -t linktmp.XXXXXXXX 2>/dev/null`
59 if [ -z "$LINKTEMP" ]; then
60         : ${TMPDIR:=/tmp}
61         LINKTEMP=${TMPDIR}/linktmp.$$
62         rm_ltmp=true
63 fi
64
65 $rm_ltmp && rm -f ${LINKTEMP}
66 # if the user specified hard links, then do that.  otherwise, try to use
67 # symlinks if they're present
68 if [ -n "$hardlinks" ]; then
69         LN=ln
70 elif (ln -s /dev/null ${LINKTEMP}) >/dev/null 2>&1; then
71         LN="ln -s"
72 else
73         LN=ln
74 fi
75 rm -f ${LINKTEMP}
76
77 TARGET=$1
78
79 if [ ! -d "$TARGET" ]; then
80         mkdir "$TARGET"
81 fi
82
83 echo "${prog}: creating clone of bash source tree (from $SRCDIR) in $TARGET"
84
85 cd "$TARGET" || { echo "${prog}: cannot cd to $TARGET" >&2 ; exit 1; }
86
87 while read fname type mode
88 do
89         [ -z "$fname" ] && continue
90
91         case "$fname" in
92         \#*)    continue ;;
93         esac
94
95         case "$type" in
96         d)      [ -n "$verbose" ] && echo mkdir $fname
97                 $ECHO mkdir $fname ;;           # already in $TARGET
98         f)      fn=${fname##*/}
99                 case "$fname" in
100                 */*)    dn=${fname%/*} ;;
101                 *)      dn=. ;;
102                 esac
103                 if [ -n "$verbose" ] || [ -n "$debug" ]; then
104                         echo "( cd $dn && $LN $SRCDIR/$fname $fn )"
105                 fi
106                 [ -z "$debug" ] && ( cd $dn && $LN $SRCDIR/$fname $fn )
107                 ;;
108         *)      echo "${prog}: ${fname}: unknown file type $type" 1>&2 ;;
109         esac
110 done < $MANIFEST
111
112 # special
113 SPECIAL="parser-built y.tab.c y.tab.h"
114
115 rm -f $SPECIAL
116 for sf in $SPECIAL
117 do
118         [ -n "$verbose" ] && echo cp -p $SRCDIR/$sf $TARGET
119         $ECHO cp -p $SRCDIR/$sf $TARGET
120 done
121
122 exit 0