#!/bin/tcsh -f

if ( $#argv < 1 ) then
   echo usage : get_packages package_names ...
   exit 1
endif

#
# E391_HOME , E391_LEVEL, E391_TOP_DIR
#
if ( ! $?E391_HOME || ! $?E391_LEVEL || ! $?E391_TOP_DIR ) then
    echo Please make sure E391_HOME , E391_LEVEL , E391_TOP_DIR
    exit 1
endif

#
# Directory Tree
#
set dtop = `pwd | grep '/e391/src$'`
if ( $dtop == '' ) then
    echo Please create the directory tree \'e391/src\'.
    echo Move to \'e391/src\' and invoke get_packages there.
    exit
endif

#
# MY_TOP_DIR
#
if ( ! $?MY_TOP_DIR  ) then
    echo MY_TOP_DIR is not set.
    echo Please invoke :
    echo setenv MY_TOP_DIR `cd .. && pwd`
    echo source ${E391_HOME}/local/etc/cshrc_e391
    exit 1
endif

#
# Makefile
#
cp ${E391_TOP_DIR}/src/Makefile .
perl -i -p -e 's|^SUBDIRS.*|SUBDIRS = \$\(shell cat PACKAGES\)|g; s|^default:|default: libs install-libs bins install-bins\n#default:|g; s|^include \$\(E391_CONFIG_DIR\)|include \$\(MY_CONFIG_DIR\)|g; s|^#SUBDIRS.*||g' ./Makefile

#
# Makefile.common
#
mkdir -p ./config
cp ${E391_CONFIG_DIR}/Makefile.common config/.
perl -i -p -e 's|\$\(E391_TOP_DIR\)\/include|\$\(MY_TOP_DIR\)\/include \$\(E391_TOP_DIR\)\/include|g; s|\$\(E391_TOP_DIR\)\/lib/lib|\$\(MY_TOP_DIR\)\/lib/lib|g' ./config/Makefile.common

#
# Makefiles.subdirs
#
cp ${E391_CONFIG_DIR}/Makefile.subdirs ./config/.
perl -i -p -e 's|\$\(E391_TOP_DIR\)|\$\(MY_TOP_DIR\)|g; s|\$\(E391_CONFIG_DIR\)\/gentableheader\.pl|\$\(MY_CONFIG_DIR\)\/gentableheader\.pl|g' ./config/Makefile.subdirs
    
cp ${E391_CONFIG_DIR}/gentableheader.pl ./config/.

#
# packages
#
set curdir = `pwd`
while ( $1 != "" )
    if ( $1 == 'skeleton' ) then
        set dn = `find ${E391_TOP_DIR}/examples -type d -name skeleton | head -1`
    else
        set dn = `find ${E391_TOP_DIR}/src -type d -name $1 | head -1`
        if ( $dn == "" ) then
            set dn = `find ${E391_TOP_DIR}/examples -type d -name $1 | head -1`
        endif
    endif
    if ( $dn != "" ) then
        set dname = `dirname ${dn}`
        set pname = `basename ${dn}`
        cd ${dname}
        find ${pname} -type f > ${curdir}/.f.tmp
        find ${pname} -type l >> ${curdir}/.f.tmp
        cat ${curdir}/.f.tmp | \
        grep -v '/bin/'      | \
        grep -v '/lib/'      | \
        grep -v '~$'         | \
        grep -v '\.[dsoa]$'  | \
        grep -v '\.so$'      | \
        grep -v '\.[it]mp$'  | \
        xargs tar cfz ${curdir}/.${pname}.tgz 
        cd ${curdir}
        tar xvfz .${pname}.tgz
        touch PACKAGES
        if ( $pname != 'skeleton' ) then
            echo ${pname} >> PACKAGES
        endif
        rm -f .f.tmp .*.tgz
    else
        echo No such package : $1
    endif
    #
    shift
end
cat PACKAGES | sort | uniq > .PACKAGES
mv -f .PACKAGES PACKAGES

#
# Makefiles in the packages
#
foreach d (`cat PACKAGES` skeleton)
    perl -i -p -e 's|^include \$\(E391_CONFIG_DIR\)|include \$\(MY_CONFIG_DIR\)|g' $d/Makefile
    if ( -f $d/src/Makefile ) then
        perl -i -p -e 's|^include \$\(E391_CONFIG_DIR\)|include \$\(MY_CONFIG_DIR\)|g' $d/src/Makefile
        perl -i -p -e 's|LINK_ARGS_SO.*-L\$\(E391_LIB_DIR\)\/so|LINK_ARGS_SO \= -L\$\(MY_LIB_DIR\)\/so -L\$\(MY_LIB_DIR\) -L\$\(E391_LIB_DIR\)\/so|g' $d/src/Makefile
        perl -i -p -e 's|LINK_ARGS_BIN.*-L\$\(E391_LIB_DIR\)\/so|LINK_ARGS_BIN \= -L\$\(MY_LIB_DIR\)\/so -L\$\(MY_LIB_DIR\) -L\$\(E391_LIB_DIR\)\/so|g' $d/src/Makefile
    endif
end

#
# First part of make : setdirs install-headers
#
gmake setdirs table-headers install-headers

#
# Comments
# 
echo ''
echo 'Command "get_package" completed.'
echo 'Type "gmake" here to build all the packages.'
echo ''

