1#! /bin/sh
2
3if test $# -lt 4; then
4    echo >&2 "Usage: $0 <dist-tree-name> <top-source-dir> <top-build-dir> <type> <make-args>"
5    exit 2
6fi
7
8case "$1" in
9    /*) disttree=$1 ;;
10    *) disttree=`pwd`/$1 ;;
11esac
12
13case "$2" in
14    /*) sdir_top=$2 ;;
15    *) sdir_top=`pwd`/$2 ;;
16esac
17
18case "$3" in
19    /*) dir_top=$3 ;;
20    *) dir_top=`pwd`/$3 ;;
21esac
22
23type=$4
24shift 4
25
26rm -rf $disttree
27
28sed_separate='
29    :1
30    $!{
31	N
32	b1
33    }
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
43'
44
45filelist=filelist$$
46trap 'rm -f $filelist; rm -rf $disttree; exit 1' 1 2 15
47(
48    cd $sdir_top
49    find . -name .git -prune -o -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
62	fi
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
74	    fi
75	done
76    fi
77done ) < $filelist
78
79status=$?
80rm -f $filelist
81trap '' 1 2 15
82if test $status -ne 0; then
83    rm -rf $disttree
84    exit $status
85fi
86
87exec chmod -R a+rX,u+w,g-s,go-w $disttree
88