#! /bin/bash
#*******************************************************************************
# E.S.O. - ACS project
#
# "@(#) $Id: acsMakeTclScript,v 1.63 2004/09/22 11:44:26 sqa-ops Exp $" 
#
# who       when        what
# --------  ----------  ----------------------------------------------
# psivera   2000-10-06  created
#

#*******************************************************************************
# ALMA - Atacama Large Millimeter Array
# Copyright (c) ESO - European Southern Observatory, 2002
# (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
#   acsMakeTclScript - create an executable Tcl/Tk procedure
# 
#   SYNOPSIS
#
#   acsMakeTclScript <tclChecker> <defTclShell> <tclShell>  
#                             <exeName> <objectList> <tclLibList>
# 
#   DESCRIPTION
#   Utility used by acsMakefile to create an executable Tcl/Tk procedure.
#   It is not intended to be used as a standalone command.
#
#   An Tcl/Tk procedure is obtained starting from one or more tcl/tk 
#   script files in the src/ directory.
#
#   (1) see also Tcl/Tk manual, 13.7 Autoloading, pag.138.
#
#
#   <tclChecker>  the program to be used as syntax checek for tcl files
#
#   <defTclShell> The full name of the default tcl shell
#
#   <tclShell>    The full name of the tcl shell for this one
#
#   <exeName>     The name of the  executable. The output is named 
#                 ../bin/<exeName>
#
#   <objectList>  The list of the script files in the src/ directory.
#                 (Without neither directory nor .tcl suffix)
#
#   <tclLibList>  the list of tcl libraries needed to link.
#                 the library directory name is created as
#                 lib<listMember>.tcl
#
#   FILES
#   $ACSROOT/include/acsMakefile   
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   SEE ALSO 
#   acsMakefile, Makefile, (GNU) make
#
#   BUGS    
#
#----------------------------------------------------------------------

if [ $# -ne 6 ]
then
    echo "" >&2
    echo "acsMakeTclScript <tclChecker> <defTclShell> <tclShell>  <exeName> <objectList> <tclLibList>" >&2
    echo "" >&2
    exit 1
fi

#
# set up more readable variables:
tclChecker=$1
defTclShell=$2
tclShell=$3
exeName=$4
objectList=$5
libList=$6

OUTPUT=../bin/${exeName}

#
#  The first line of a script can be used to specify the shell to be used 
#  to execute the script. Such a line shall have the following format:
#          #! <shellFilename> [<parameter> ... ]
#  The <shellFilename> MUST be completed with the path and the total 
#  lenght cannot exceed 32 chars.
#
#  This impose a serious limitation to the flexibility of finding a shell
#  executable according to the precedence established by the current PATH 
#  
#!##  Since the introduction of tcl/Tk and Sequencer, many commands are build 
#!##  as scripts. To allow the usual flexibility given by the mod/int/acs-root
#!##  architecture, there is the need to use the following trick:
#!##  
#!##      - all scripts are executed by /bin/bash (first line)
#!##      - the second line ends with "\"
#!##      - the third line is an "exec" of the actual shell we want to use
#!##        (such a command is searched according to the current PATH)
#!##
#!##  Thanks to a different way in handling the "\" at the end of a comment line
#!##  (such line is considered a comment line for Tcl, i.e. when you "source"
#!##  the file, but not for /bin/bash ) this allows to be able both to source 
#!##  the script and to execute. In both cases the same shell (the one given
#!##  by "which shell") is used.
#!##  
#!##  Therefore the header of the output file shall be:
#!##  
#!##     #!/bin/bash
#!##     #\
#!##     exec <tclShell> "$0" ${1+"$@"}
#!##
# Tcl/Tk scripts should work as stand-alone scripts, but just as well when
# they are sourced-in by a Tcl-interpreter. On top of that, when executed
# as a script, we want to be sure that the corresponding Tcl-shell is found
# in the proper place, respecting a.o. the current PATH settings. To obtain
# this behaviour, we put a "#! /usr/bin/env <tclShell>" on the first line
# of the script.

if [ "${tclShell}" = "" ]
then
    tclShell=$defTclShell
fi

#!#echo "#!/bin/bash" >$OUTPUT
#!#echo "#\\" >>$OUTPUT
#!#echo "exec ${tclShell} \"\$0\" \${1+\"\$@\"}" >>$OUTPUT
echo "#! /usr/bin/env ${tclShell}" >$OUTPUT

#
# create header
#
echo "#*******************************************************************************" >>$OUTPUT
echo "# E.S.O. - ACS project" >>$OUTPUT
echo "# " >>$OUTPUT
echo "# ${exeName}" >>$OUTPUT
echo "# ">>$OUTPUT
echo "# Created by acsMakeTclScript: `whoami` `date '+%d.%m.%y %T'` " >>$OUTPUT
echo "# ">>$OUTPUT
echo "# ">>$OUTPUT
echo "#        !!!!!!!!!!!!!!  DO NOT MANUALLY EDIT THIS FILE  !!!!!!!!!!!!!!" >>$OUTPUT
echo "# ">>$OUTPUT
echo "#-------------------------------------------------------------------------------">>$OUTPUT
echo "" >>$OUTPUT

#
# define standard variables: ACSROOT, INTROOT, HOME
echo "#" >>$OUTPUT
#!#echo "#  Get directory root of VLT and ACS, integration, current module." >>$OUTPUT
echo "#  Get directory root of ACS, integration, current module." >>$OUTPUT
echo "if {[catch {set ACSROOT \$env(ACSROOT)}]} {set ACSROOT undefined}" >>$OUTPUT
#!#echo "if {[catch {set VLTROOT \$env(VLTROOT)}]} {set VLTROOT undefined}" >>$OUTPUT
echo "if {[catch {set INTLIST \$env(INTLIST)}]} {set INTLIST undefined}" >>$OUTPUT
echo "if {[catch {set INTROOT \$env(INTROOT)}]} {set INTROOT undefined}" >>$OUTPUT
echo "if {[catch {set HOME    \$env(HOME)}]}    {set HOME    undefined}" >>$OUTPUT
echo "" >>$OUTPUT

#
# if the list of libraries is not empty, insert each lib into the autopath

if [ "${libList}" != "" ]
then
    echo "#" >>$OUTPUT
    echo "#  Set Auto-Path for each library" >>$OUTPUT
    echo "#" >>$OUTPUT 

    for member in ${libList}
    do
        echo "" >>$OUTPUT
        echo "# " >>$OUTPUT
        echo "# >>>${member}<<<" >>$OUTPUT

        echo "set found \"NO\" "  >>$OUTPUT
        echo "if {\$INTLIST != \"undefined\"} { "  >>$OUTPUT
        echo "   set myIntlist \"\$env(INTLIST)\" "  >>$OUTPUT
        echo "   set intlist_list [split \$myIntlist :] "  >>$OUTPUT
        echo "   set first_dir [ lindex \$intlist_list 0 ] "  >>$OUTPUT
        echo "   foreach tmpdir \$intlist_list {" >>$OUTPUT
        echo "      if {[file exist \$tmpdir/lib/lib${member}.tcl] == 1} {" >>$OUTPUT
        echo "         if {[lsearch -exact \$auto_path \$tmpdir/lib/lib${member}.tcl] == -1} {" >>$OUTPUT
        echo "            set found \"YES\" " >>$OUTPUT
        echo "            set mydir \$tmpdir" >>$OUTPUT
        echo "            break " >>$OUTPUT
        echo "         } " >>$OUTPUT
        echo "      } " >>$OUTPUT
        echo "   } " >>$OUTPUT
        echo "} " >>$OUTPUT

        echo "if {[file exist ../lib/lib${member}.tcl] == 1} {" >>$OUTPUT
        echo "    # the current relative path can be affected by any change of working" >>$OUTPUT
        echo "    # directory within the application. " >>$OUTPUT
        echo "    # To prevent possible mess, the RELATIVE path is change into absolute"  >>$OUTPUT
        echo "    set keepWd [pwd]"           >>$OUTPUT
        echo "    cd ../lib/lib${member}.tcl" >>$OUTPUT
        echo "    if {[lsearch -exact \$auto_path [pwd]] == -1} {"  >>$OUTPUT
        echo "        set auto_path [linsert \$auto_path 0 [pwd]]"  >>$OUTPUT
	echo "    }" >>$OUTPUT
        echo "    cd \$keepWd"  >>$OUTPUT
        echo "    unset keepWd" >>$OUTPUT
        echo "} elseif {[file exist \$INTROOT/lib/lib${member}.tcl] == 1} {" >>$OUTPUT
        echo "    if {[lsearch -exact \$auto_path \$INTROOT/lib/lib${member}.tcl] == -1} {" >>$OUTPUT
        echo "        set auto_path [linsert \$auto_path 0 \$INTROOT/lib/lib${member}.tcl]" >>$OUTPUT
	echo "    }" >>$OUTPUT
        echo "} elseif  {\$found == \"YES\"} {" >>$OUTPUT
        echo "     set auto_path [linsert \$auto_path 0 \$mydir/lib/lib${member}.tcl]" >>$OUTPUT
#!#        echo "} elseif {[file exist \$VLTROOT/lib/lib${member}.tcl] == 1} {" >>$OUTPUT
#!#        echo "    if {[lsearch -exact \$auto_path \$VLTROOT/lib/lib${member}.tcl] == -1} {" >>$OUTPUT
#!#        echo "        set auto_path [linsert \$auto_path 0 \$VLTROOT/lib/lib${member}.tcl]" >>$OUTPUT
#!#        echo "    }" >>$OUTPUT
        echo "} elseif {[file exist \$ACSROOT/lib/lib${member}.tcl] == 1} {" >>$OUTPUT
	echo "    if {[lsearch -exact \$auto_path \$ACSROOT/lib/lib${member}.tcl] == -1} {" >>$OUTPUT
        echo "        set auto_path [linsert \$auto_path 0 \$ACSROOT/lib/lib${member}.tcl]" >>$OUTPUT
        echo "    }" >>$OUTPUT
        echo "} else {" >>$OUTPUT
        echo "    puts stderr  \"${exeName} - ERROR:\"; " >>$OUTPUT
        echo "    puts stderr  \"   Tcl library lib${member}.tcl not found. Unable to continue.\"; " >>$OUTPUT
        echo "    puts stderr  \"\"; " >>$OUTPUT
        echo "    exit 1;" >>$OUTPUT
        echo "}" >>$OUTPUT
    done
else
    echo "#" >>$OUTPUT
    echo "# No libraries defined for this script." >>$OUTPUT
    echo "# " >>$OUTPUT
fi

echo "###########################################################" >>$OUTPUT
echo "#                                                         #" >>$OUTPUT
echo "# if the current shell is including TK, i.e. option       #" >>$OUTPUT
echo "# command is known, then set-up X resources               #" >>$OUTPUT
echo "#                                                         #" >>$OUTPUT
echo "###########################################################" >>$OUTPUT

echo "" >>$OUTPUT
echo "if {[info command option] != \"\"} {" >>$OUTPUT

echo "    " >>$OUTPUT
echo "    #" >>$OUTPUT
echo "    # Load resources:" >>$OUTPUT
echo "    # merge possible Xresource files in the following order:" >>$OUTPUT
echo "    #           - $ACSROOT/app-defaults  ">>$OUTPUT
#!#echo "    #           - $VLTROOT/app-defaults  ">>$OUTPUT
echo "    #           - $INTROOT/app-defaults  ">>$OUTPUT
echo "    #           - $HOME/                 ">>$OUTPUT
echo "    #           - ../app-defaults        ">>$OUTPUT
echo "    #" >>$OUTPUT
echo "    if {[file exists \$ACSROOT/app-defaults/X${exeName}] == 1} {" >>$OUTPUT
echo "        option readfile \$ACSROOT/app-defaults/X${exeName} " >>$OUTPUT
echo "    }" >>$OUTPUT

echo "if {\$INTLIST != \"undefined\"} { "  >>$OUTPUT
echo "    foreach tmpdir \$intlist_list {" >>$OUTPUT
echo "       if {[file exists \$tmpdir/app-defaults/X${exeName}] == 1} {" >>$OUTPUT
echo "          option readfile \$tmpdir/app-defaults/X${exeName} " >>$OUTPUT
echo "       }" >>$OUTPUT
echo "    }" >>$OUTPUT
echo "}" >>$OUTPUT

echo "    if {[file exists \$INTROOT/app-defaults/X${exeName}] == 1} {" >>$OUTPUT
echo "    option readfile \$INTROOT/app-defaults/X${exeName} " >>$OUTPUT
echo "    }" >>$OUTPUT

echo "    if {[file exists \$HOME/X${exeName}] == 1} {" >>$OUTPUT
echo "        option readfile \$HOME/X${exeName} " >>$OUTPUT
echo "    }" >>$OUTPUT

echo "    if {[file exists ../app-defaults/X${exeName}] == 1} {" >>$OUTPUT
echo "        option readfile ../app-defaults/X${exeName} " >>$OUTPUT
echo "    }" >>$OUTPUT

echo "}" >>$OUTPUT
   
echo "#-------------------------------------------------------------------" >>$OUTPUT
echo "#                    END OF STANDARD PROLOGUE " >>$OUTPUT
echo "#-------------------------------------------------------------------" >>$OUTPUT

#
# append each source file to the output file
if [ "${objectList}" = "" ]
then
    echo "WARNING: acsMakeTclScript: ${exeName}_OBJECTS is not defined." >&2
    exit 1
fi

for member in ${objectList}
do
    #
    # run the tcl checker on each tcl-file
    $tclChecker ${member}.tcl 1>&2
    
    echo "" >>$OUTPUT
    echo "#vvvvvvvvvvvvvvvvv  src/${member}.tcl  vvvvvvvvvvvvvvvvvvvvvv" >>$OUTPUT
    cat ${member}.tcl >>$OUTPUT
    echo "#^^^^^^^^^^^^^^^^^  src/${member}.tcl  ^^^^^^^^^^^^^^^^^^^^^^" >>$OUTPUT
done

#
# terminate output file
echo "" >>$OUTPUT
echo "#--------------------------------------------------------------" >>$OUTPUT
echo "#                       End of ${exeName}" >>$OUTPUT
echo "#--------------------------------------------------------------" >>$OUTPUT
echo "# ___oOo___ " >>$OUTPUT

#
# make output file executable
chmod +x $OUTPUT

#
# notify user that all has been done
echo "                      $OUTPUT  created"
#
# ___oOo___
