26763: fix problem on failed cd -s to relative path
[zsh.git] / Util / mkdisttree.sh
blob92ded2f1844b9de8a4cff28ea5b38b9f01474899
1 #! /bin/sh
3 if test $# -lt 4; then
4 echo >&2 "Usage: $0 <dist-tree-name> <top-source-dir> <top-build-dir> <type> <make-args>"
5 exit 2
6 fi
8 case "$1" in
9 /*) disttree=$1 ;;
10 *) disttree=`pwd`/$1 ;;
11 esac
13 case "$2" in
14 /*) sdir_top=$2 ;;
15 *) sdir_top=`pwd`/$2 ;;
16 esac
18 case "$3" in
19 /*) dir_top=$3 ;;
20 *) dir_top=`pwd`/$3 ;;
21 esac
23 type=$4
24 shift 4
26 rm -rf $disttree
28 sed_separate='
30 $!{
34 s/\n/ /g
35 s/^/deplist=;globlist=! /
36 s/$/ !/
37 s/ */ /g
38 s/ \([^?*[!][^?*[!]*\) / !deplist="$deplist \1"! /g
39 s/! !/;/g
40 s/! \([^!]*\) !/;globlist="$globlist \1";/g
41 s/!/;/g
42 s/;;*/;/g
45 filelist=filelist$$
46 trap 'rm -f $filelist; rm -rf $disttree; exit 1' 1 2 15
48 cd $sdir_top
49 find . -name '?*.*' -prune -o -name .distfiles -print
50 ) > $filelist
51 ( while read dfn; do
52 subdir=`echo $dfn | sed 's,/\.distfiles$,,'`
53 echo >&2 "Processing directory $subdir..."
54 eval "DISTFILES_$type="
55 . $sdir_top/$dfn
56 eval "distfiles=\$DISTFILES_$type"
57 if test -n "$distfiles"; then
58 cmds=`echo "$distfiles" | sed -e "$sed_separate"`
59 eval "$cmds"
60 if test -n "$deplist" && test -f $dir_top/$subdir/Makefile; then
61 ( trap '' 1 2 15; cd $dir_top/$subdir && "$@" $deplist ) || exit 1
63 $sdir_top/mkinstalldirs $disttree/$subdir || exit 1
64 for f in $deplist `test -z "$globlist" || ( cd $dir_top/$subdir && eval "echo $globlist")`; do
65 if test -f $dir_top/$subdir/$f; then
66 # ln $dir_top/$subdir/$f $disttree/$subdir/$f || \
67 cp -p $dir_top/$subdir/$f $disttree/$subdir/$f || exit 1
68 elif test -f $sdir_top/$subdir/$f; then
69 # ln $sdir_top/$subdir/$f $disttree/$subdir/$f || \
70 cp -p $sdir_top/$subdir/$f $disttree/$subdir/$f || exit 1
71 else
72 echo >&2 "$0: can't find file $subdir/$f"
73 exit 1
75 done
77 done ) < $filelist
79 status=$?
80 rm -f $filelist
81 trap '' 1 2 15
82 if test $status -ne 0; then
83 rm -rf $disttree
84 exit $status
87 exec chmod -R a+rX,u+w,g-s,go-w $disttree