#! /bin/sh

# shadow_tree - create shadow link tree
#
# Used to create a copy of the a directory tree that has links for all non-
# directories (except those named RCS).  If you are building the distribution
# on more than one machine, you should use this script.
#
# If your master sources are located in /usr/local/src and you would like
# your link tree to be in /usr/local/new_tree, do the following:
#
# 	%  mkdir /usr/local/new_tree
#	%  cd /usr/local/new_tree
# 	%  shadow_tree ../src ...
#

USAGE="Usage: $0 <fromdir> <top-of-source-tree> <pcncomp-script-path> <makefile_header> <spaces> <include_path>"

if [ $# -ne 6 ]; then
    echo $#
    echo $@
    echo "$USAGE"
    exit 1
fi

dir_from=$1
DIRTO=.

dir_top=$2

pcncomp_path=$3

makefile_hdr=$4

spaces="$5"
newspaces="  ${spaces}"

include_path="$6"

if [  ! -d ${dir_from}  ]; then
    echo "${0}: ${dir_from} is not a directory"
    echo "$USAGE"
    exit 2
fi

pwd=`pwd`
here=$pwd

if [  `(cd ${dir_from}; pwd)` = $pwd  ]; then
    echo "$pwd: FROM and TO are identical!"
    exit 1
fi

#echo "DIRTO=${DIRTO}"
#echo "dir_from=${dir_from}"

cd ${dir_from}
file_list=`echo * .??*`
cd ${here}

#foreach file (`/bin/ls ${dir_from}`)
for file in ${file_list}
do
    full_file="${dir_from}/${file}"
    tmppwd=`pwd`
    if [ ! -d ${full_file} ]; then
	case ${file} in
            Makefile.base)
                echo "${spaces}Creating file: Makefile from ${full_file}"
                cat ${makefile_hdr} > Makefile
                echo "SRC_PATH=${dir_from}" >> Makefile
		echo "TOPLEVEL_PATH=${dir_top}" >> Makefile
		echo "PCNCOMP_PATH=${dir_top}/${pcncomp_path}" >> Makefile
                echo "INCLUDE_PATH=${include_path}" >> Makefile
# add the include for Makefile.base
		echo "include Makefile.base" >> Makefile
		ln -s ${full_file} Makefile.base
# Commented out -- "include Makefile.base" takes care of it
#                cat ${full_file} >> Makefile
# Copy Makefile.base to the shadow dir
                ;;
            .gdbinit.base)
                echo "${spaces}Creating file: .gdbinit from ${full_file}"
                echo "dir ${dir_from}:${include_path}" > .gdbinit
                cat ${full_file} >> .gdbinit
                ;;
            *.h)
                if [ -r ${dir_from}/.link_headers -o -r ${dir_from}/.link_files ]; then
                    echo "${spaces}Symbolic linking: ${file} -> ${full_file}"
                    ln -s ${full_file} ${file}
                fi
		;;
            *)
                if [ -r ${dir_from}/.link_files ]; then
                    echo "${spaces}Symbolic linking: ${file} -> ${full_file}"
                    ln -s ${full_file} ${file}
                fi
                ;;
        esac
    else
        if [ $file != RCS ]; then
            if [ -r ${full_file}/Makefile.base ]; then
                echo "${spaces}Creating directory: ${file}"
                mkdir ${file}
                cd ${file}
                pwd=`pwd`
		case ${dir_from} in
                    /*)
                        new_dir_from="${dir_from}"
                        ;;
                    *)
                        new_dir_from="../${dir_from}"
                        ;;
                esac
		case ${dir_top} in
                    /*)
                        new_dir_top="${dir_top}"
                        ;;
                    *)
                        new_dir_top="../${dir_top}"
                        ;;
                esac
                if [ `(cd ${new_dir_from}/${file}; pwd)` = ${pwd} ]; then
                    echo "${pwd}: FROM and TO are identical\!"
                    exit 1
                fi
                ln -s ${new_dir_from}/${file} .src
                $0 ${new_dir_from}/$file ${new_dir_top} ${pcncomp_path} ${makefile_hdr} "${newspaces}" ../${include_path}
                cd $here
            else
                echo "${spaces}Symlinking directory: ${full_file}"
                ln -s $full_file .
            fi
        fi
    fi
done

exit 0

