#! /bin/bash
#*******************************************************************************
# E.S.O. - ALMA project
#
# "@(#) $Id: acsMakeInstallFiles,v 1.3 2010/07/09 15:15:24 rtobar Exp $" 
#
# who       when        what
# --------  ----------  ----------------------------------------------
# eallaert  2014-10-07  copied over from Kit/vlt/src/vltMakeInstallFiles

#*******************************************************************************
# ALMA - Atacama Large Millimeter Array
# Copyright (c) ESO - European Southern Observatory, 2014
# (in the framework of the ALMA collaboration).
# All rights reserved.
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#*******************************************************************************

#************************************************************************
#   NAME
#   acsMakeInstallFiles - copy the files into target area.
# 
#   SYNOPSIS
#
#   acsMakeInstallFiles <fileList> <PRJTOP> <ACSTARGET> <protectionMask>
#
# 
#   DESCRIPTION
#   Utility used by acsMakefile to generate the acsMakefile.install section
#   in charge to copy the files into target area.
#   For every file in <fileList>:
#
#      - get "name" and "dir", i.e.: the filename and the parent directory
#
#      - the rule to copy the file into PRJTOP/<dir> or in ACSTARGET/<dir>
#        is generated. As for any installed file, the rule is executed only if
#        the file is newer. The protection mask is applied to leave the file 
#        overwritable by the next installation
#
#
#   It is not intended to be used as a standalone command.
#
#   <fileList>        file to be copied
#   <PRJTOP>          starting point for bin, include, etc (ACS|INT-ROOT[/vw])
#   <ACSTARGET>       starting point for other directories (ACS|INT-ROOT)
#   <protectionMask>  how to set the protection of created file
#
#
#   FILES
#   $ACSROOT/include/acsMakefile   
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   SEE ALSO 
#   acsMakefile
#
#   BUGS    
#
#----------------------------------------------------------------------


# if Linux: disable the bash builtin command 'echo'.
if [ "`uname`" = "Linux" ]
then
    enable -n echo
elif [ "`uname`" = "$CYGWIN_VER" ]
then
    alias echo="echo -e"
fi

if [ $# != 4 ]
then
    echo "" >&2
    echo " ERROR:  acsMakeInstallFiles: $*" >&2
    echo " Usage:  acsMakeInstallFiles <fileList> <PRJTOP> <ACSTARGET> <protectionMask>" >&2
    echo "" >&2
    exit 1
fi

fileList=${1}
PRJTOP=$2
ACSTARGET=$3
MASK=$4


if [ ! -d $PRJTOP ]
then 
    echo "" >&2
    echo " ERROR: acsMakeInstallFiles: " >&2
    echo "          >>$PRJTOP<< not a valid directory " >&2
    echo "" >&2
    exit 1
fi

if [ ! -d $ACSTARGET ]
then 
    echo "" >&2
    echo " ERROR: acsMakeInstallFiles: " >&2
    echo "          >>$ACSTARGET<< not a valid directory " >&2
    echo "" >&2
    exit 1
fi

#
# according to the file currently under ERRORS, if any, produce
# the needed targets:

if [ "$fileList" != "" ]
then     
    target="install_files: files_begin "

    echo "files_begin:"
    echo "\t-@echo \"\"; echo \"..other files:\""

    for FILE in $fileList
    do
        if [ ! -f $FILE ]
        then 
            echo "" >&2
            echo " ERROR: acsMakeInstallFiles: " >&2
            echo "          >>$FILE<< file not found " >&2
            echo "" >&2
            exit 1
        fi

        # Extract the filename and the directory relative to the
        # root of the installation point.
        name=`basename $FILE`
        fulldir=`dirname $FILE`

	case "$FILE" in
        ../*) dir=`expr $fulldir : '\.\./\(.*\)'`
              ;;
        *)    dir=`basename $fulldir`
              ;;
        esac

        if [ -d $PRJTOP/$dir ]
        then 
            TOFILE=$PRJTOP/$dir/$name
        else
            if [ -d $ACSTARGET/$dir ]
            then 
                TOFILE=$ACSTARGET/$dir/$name
            else
                echo "" >&2
                echo " ERROR: acsMakeInstallFiles: " >&2
                echo "          >>$dir<< not a standard directory" >&2
                echo "" >&2
                exit 1
            fi
        fi  

        echo "$TOFILE: $FILE"
	echo "\t-\$(AT)echo \"\t$name\"; \\"
        echo "\t      cp $FILE  $TOFILE; \\"  
        echo "\t      chmod $MASK $TOFILE"
        target="$target $TOFILE"
    done

    echo $target

else

    echo "files:"
    echo "\t-@echo \"\""

fi

exit 0
#
# ___oOo___

