#!/bin/ksh
# mbuild - script for compiling the PALM code and its utility programs
# $Id: mbuild 1021 2012-10-02 13:52:49Z sward $

     # Procedure to compile code on local and remote hosts using the
     # make-mechanism. The source code must be provided on the local host.

     # Last changes:
     # 06/05/02 - Siggi - start development
     # 12/06/02 - Siggi - first version finished
     # 12/09/02 - Siggi - ibmh (hanni.hlrn.de) included
     # 19/12/02 - Siggi - ibmb included
     # 05/02/03 - Siggi - hostname nobela included
     # 04/03/03 - Siggi - host nech included
     # 06/03/03 - Siggi - make_call_string is echoed without '' to
     #                    file LAST_MAKE_CALL (otherwise error on NEC, because
     #                    '' are part of compiler options
     # 16/03/03 - Siggi - Two underscores are placed in front of every define
     #                    string, in case that palm.f90 version contains
     #                    such strings
     # 16/04/03 - Siggi - First extensions for linux machines
     # 24/06/03 - Siggi - host orkan included
     # 17/07/03 - Siggi - IP adress set to new "cross" machine at DKRZ
     # 24/07/03 - Siggi - host maestro admitted
     # 06/08/03 - Siggi - host gregale admitted
     # 05/11/03 - Siggi - hosts irifi and quanero are now belonging to lcmuk
     # 19/11/03 - Heiko - on lcmuk, mbuild does not tar the *.i files
     # 08/01/04 - Siggi - additional preprocessor directive for ibm included
     #                    (-D$OMP=OMP) in order to avoid problems with
     #                    OMP_NUM_THREADS
     # 09/01/04 - Siggi - action above cancelled
     # 28/01/04 - Siggi - action above re-cancelled
     # 08/03/04 - Siggi - host scirocco admitted
     # 26/03/04 - Siggi - .o and .mod files are also deleted in depository, if
     #                    the respective .f90 file is not listed in the makefile
     #                    and deletion is demanded by the user
     # 12/04/04 - Siggi - scp2 instead of scp used for transfer from decalpha
     #                    due to error in ssh installation (otherwise a prompt
     #                    for the password appears)
     # 23/07/04 - Siggi - changes due to the new berni configuration
     #                    (federation switch)
     # 08/09/04 - Siggi - hanni IP address changed to 130.75.4.10
     # 23/09/04 - Joerg - correction of IP-Address for 'cross' (DKRZ)
     # 09/03/05 - Siggi - on nech, mbuild does not tar the *.i files
     # 31/03/05 - Siggi - mbuild does not tar *.i files any more
     # 24/04/05 - Siggi - netcdf support on lcmuk
     # 25/04/05 - Siggi - netcdf support on gfdl3 (decalpha)
     # 12/05/05 - Siggi - netcdf support on ibm
     #                    set OBJECT_MODE=64 for compiling on ibmb, ibmh
     # 18/05/05 - Siggi - netcdf support on nec
     # 19/05/05 - Siggi - IP addres 134.75.155.74 changed to 165.132.26.56
     # 23/05/05 - Siggi - netcdf support on ibms
     # 01/06/05 - Siggi - reset of cpp_options to "" on lcmuk
     # 30/06/05 - Siggi - netcdf support on bora
     # 20/10/05 - Siggi - update of netcdf-version on decalpha (gfdl3)
     # 04/11/05 - Siggi - netcdf 3.6.0-p1 on ibmh/ibmb
     # 30/12/05 - Siggi - change of IP adresses in subnet 130.75.105
     #                    host gfdl5 (ibmy) admitted
     # 10/01/06 - Siggi - cpp directive for NetCDF 64bit support
     # 20/01/06 - Siggi - cpp directive for ibmy
     # 09/02/06 - Marcus- compile only once on lcmuk (as on ibmh/ibmb)
     # 10/02/06 - Siggi - modifications for scp on decalpha
     # 13/04/06 - Siggi - ostria admitted
     # 19/04/06 - Siggi - preprocessor directive -D$OMP=OMP for ibm removed
     # 23/05/05 - Siggi - lctit (SUN Fire X4600) admitted
     # 29/05/05 - Siggi - atmos (lcide) admitted
     # 23/08/06 - Siggi - netcdf support for scirocco (notebook)
     # 24/11/06 - Siggi - breva and levanto admitted
     # 07/02/07 - Siggi - adapted for RIAM (neck)
     # 10/02/07 - Siggi - all hpmuk-related code removed
     # 02/03/07 - Siggi - compilation of utility programs and transfer of
     #                    scripts to remote hosts added (option -u)
     # 14/03/07 - Siggi - fimm admitted, revision number added to terminal
     #                    output
     # 16/03/07 - Siggi - adjustments (netcdf) for lctit
     #                    adjustments for running under pdksh, local ip-addres
     #                    is not determined any more
     # 30/03/07 - Siggi - cpp-directives/options + netcdf-options are read
     #                    from configuration file
     #                    host identifier (local_host) is read from config file
     # 10/10/07 - Siggi - bugfix: handling of comment lines
     # 18/07/08 - Siggi - adapted for lcsgih
     # 21/07/08 - Siggi - mainprog (executable) is added to the tar-file
     #                    ({mainprog}_current_version)
     # 02/10/08 - Siggi - adapted for lcxt4
     # 14/11/08 - Siggi - update mechanism completely revised: source tarfile
     #                    remains in source directory, one depository per block
     #                    (given in the config-file) is created, allways all
     #                    files from the source directory are copied to the
     #                    respective depository, no additional file checks are
     #                    done any more (version 2.1)
     # 13/01/09 - Marcus- re-enable compilation more than once on lcmuk (as on 
     #                    other machines)
     # 21/03/09 - Siggi - -u copies also copies process_dvr_output and
     #                    .dvrserver.config
     # 21/04/09 - Siggi - adjustments for new IBM at DKRZ, which is now ibmh
     # 25/08/09 - BjornM- adapted for lck
     # 01/12/09 - BjornM- re-adjustments for lcxt4
     # 01/02/10 - Siggi - adjustments for lcxt5m
     # 03/02/10 - Siggi - make options (mopts) to be set by configuration file
     #                    implemented, remoted mode option -r completely
     #                    removed, t3e related parts removed
     # 26/02/10 - BjornM- re-adjustments for lcxt4 (new modules)
     # 01/03/10 - Siggi - mpilib replaced by modules: explicit module settings
     #                    removed; modules should now be given in the
     #                    configuration file
     # 02/03/10 - Siggi - utility programs are created separately for each
     #                    block in the configuration file,
     #                    parallel make for utility programs
     # 12/03/10 - Siggi - bugfix: remode_md was given a wrong value
     # 16/08/10 - BjornM- bugfix: call of modules on local host, new workaround
     #                    for lcxt4
     # 07/09/10 - Siggi - bugfix for wrong netcdf/3.6.3 module on lcsgi
     # 08/12/10 - Siggi - initialization of the module command changed for
     #                    SGI-ICE/lcsgi
     #                    adjustments for Kyushu Univ. (lcrte, ibmku)
     # 14/12/10 - Siggi - adjustments for new Tsubame system at Tokyo
     #                    institute of technology (lctit)
     # 04/03/11 - BjornM- re-adjustments for lcxt4
     # 06/03/11 - Siggi - adjustments for ibmkisti
     # 03/04/11 - Micha - added lckordi
     # 17/08/11 - Siggi - HLRN access via hicegate0 (130.75.4.101)
     # 27/11/11 - Siggi - Bugfix for make options setting (-j) 
     # 12/12/11 - BjornM- added mrun GUI to ulitity programs
     # 25/01/12 - BjornM- compilation of check_namelist_files (namelist file 
     #                    check program) has been added to the utility branch),
     #                    modfified the compilation of mrungui
     # 31/01/12 - BjornM- bugfix for compilation of utility programs on 
     #                    multiple localhost branches
     # 08/03/12 - BjornM- bugfix: cpp directive for namelist check program
     #                    corrected
     # 02/05/12 - Bjoern- lcxt4 is now lcxe6, adjustments for lcxe6, 
     #                    bugfixes for the parameter file check, initial 
     #                    commands on ssh calls sourced out to .mrun.config
     # 05/06/12 - Bjoern- bugfixes for execution of login_init_cmd
     # 18/06/12 - Siggi - option -v (silent mode) added
     # 25/06/12 - Siggi - option -K (block conditions) added
     # 27/09/12 - Siggi - compiler options (F90FLAGS) added for namelist check
     # 02/10/12 - Siggi - added lcsb


    # VARIABLE DECLARATIONS + DEFAULT VALUES
 block_conditions=none
 block_conditions_found=false
 compile_utility_programs=false
 config_file=.mrun.config
 fimm=false
 host=all
 host_found=false
 locat=normal
 makefile=""
 module_calls=""
 util_compiled_localhost=false
 scirocco=false
 silent=false
 suf=f90
 update=false
 working_directory=`pwd`

 typeset -i  ih ihost=0

 typeset  -R30 calltime
 typeset  -L20 column1
 typeset  -L50 column2
 typeset  -L70 column3
 typeset  -L40 version="MBUILD  2.1  Rev$Rev: 1021 $"

    # ERROR HANDLING
    # IN CASE OF EXIT:
 trap 'rm -rf  $working_directory/tmp_mbuild
       if [[ $locat != normal ]]
       then
          printf "\n\n +++ mbuild killed \n\n"
       else
          printf "\n\n *** mbuild finished \n\n"
       fi' exit


    # IN CASE OF TERMINAL-BREAK:
 trap 'rm -rf  $working_directory/tmp_mbuild
       printf "\n\n +++ mbuild killed by \"^C\" \n\n"
       exit
      ' 2


 tmp_mbuild=${working_directory}/tmp_mbuild

    # READ SHELLSCRIPT-OPTIONS
 while  getopts  :c:h:K:m:s:uv  option
 do
   case  $option  in
       (c)   config_file=$OPTARG;;
       (h)   host=$OPTARG;;
       (K)   block_conditions=$OPTARG;;
       (m)   makefile=$OPTARG;;
       (s)   suf=$OPTARG;;
       (u)   compile_utility_programs=true;;
       (v)   silent=true;;
       (\?)  printf "\n  +++ unknown option $OPTARG \n";
             locat=parameter; exit;;
   esac
 done



    # CHECK, IF CONFIGURATION-FILE EXISTS
 if [[ ! -f $config_file ]]
 then
    printf "\n  +++ configuration file: "
    printf "\n           $config_file"
    printf "\n      does not exist"
    locat=configuration; exit  
 fi



    # DETERMINE THE LOCAL HOST
 local_host_real_name=$(hostname)
# local_addres=$(nslookup `hostname` 2>&1 | grep "Address:" | tail -1 | awk '{print $2}')



    # DETERMINE HOST-IDENTIFIER (local_host) FROM THE CONFIG-FILE
 line=""
 grep  "%host_identifier"  $config_file  >  $tmp_mbuild
 while read line
 do
    if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
    then
       HOSTNAME=`echo $line | cut -d" " -s -f2`
       host_identifier=`echo $line | cut -d" " -s -f3`
       if [[ $local_host_real_name = $HOSTNAME ]]
       then
          local_host=$host_identifier
          break
       fi
    fi
 done < $tmp_mbuild

 if [[ "$local_host" = "" ]]
 then
    printf "\n  +++ no host identifier found in configuration file \"$config_file\""
    printf "\n      for local host \"$local_host_real_name\"."
    printf "\n      Please add line"
    printf "\n      \"\%host_identifier $local_host_real_name <identifier>\""
    printf "\n      to the configuration file."
    locat=local_host; exit
 fi



 [[ $local_host_real_name = scirocco ]]  &&  scirocco=true
 [[ $local_host_real_name = fimm.bccs.uib.no ]]  &&  fimm=true



 if [[ $local_host != ibms ]]
 then
    config_file=$PWD/$config_file
 else
    config_file=`pwd`/$config_file
 fi


    # determine the block conditions
 if [[ $block_conditions != none ]]
 then
    block_condition1=`echo $block_conditions | cut -d" " -f1`
    block_condition2=`echo $block_conditions | cut -d" " -f2`
    if [[ "$block_condition2" = "$block_condition1" ]]
    then
       block_condition2=""
    fi
 fi

 
    # DETERMINE USER NAME ON LOCAL HOST FROM THE CONFIG-FILE
 line=""
 grep  " $local_host" $config_file | grep "%remote_username"  >  $tmp_mbuild
 while read line
 do
    if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
    then
       local_username=`echo $line | cut -d" " -s -f2`
    fi
 done < $tmp_mbuild


 if [[ "$local_username" = "" ]]
 then
    printf "\n  +++ no user name found in configuration file"
    printf "\n      for local host \"$local_host\" "
    locat=config_file; exit
 fi


    # DETERMINE LOCAL SOURCE-CODE PATH.
    # FIRST CHECK, IF A GLOBAL SOURCE-CODE PATH HAS BEEN DECLARED FOR ALL HOSTS.
    # THEREFORE, FIRST SET ALL GLOBAL VARIABLES DECLARED IN THE CONFIG-FILE,
    # BECAUSE THEY MAY BE USED AS PART OF THE PATH NAME.
 line=""
 grep "%" $config_file  >  $tmp_mbuild
 while read line
 do
    if [[ "$line" != ""  &&  "$(echo $line | cut -d" " -s -f3)" = ""  &&  $(echo $line | cut -c1) != "#" ]]
    then
       var=`echo $line | cut -d" " -s -f1 | cut -c2-`
       value=`echo $line | cut -d" " -s -f2`
       eval export $var=\$value
    fi
 done < $tmp_mbuild

    # NOW CHECK, IF A GLOBAL SOURCE-CODE-PATH HAS BEEN DECLARED
 line=""
 grep "%source_path" $config_file  >  $tmp_mbuild
 while read line
 do
    if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
    then
       if [[ "$(echo $line | cut -d" " -s -f3)" = "" ]]
       then
          global_source_path=`echo $line | cut -d" " -s -f2`
       fi
    fi
 done < $tmp_mbuild

 line=""
 grep  " $local_host" $config_file | grep "%source_path"  >  $tmp_mbuild
 while read line
 do
    if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
    then
       local_source_path=`echo $line | cut -d" " -s -f2`
    fi
 done < $tmp_mbuild

 if [[ "$local_source_path" = "" ]]
 then
    if [[ "$global_source_path" != "" ]]
    then
       local_source_path=$global_source_path
    else
       printf "\n  +++ no source path found in configuration file"
       printf "\n      for local host \"$local_host\" "
       printf "\n      please set \"\%source_path\" in configuration file"
       locat=config_file; exit
    fi
 fi
 eval local_source_path=$local_source_path
 eval local_source_path=$local_source_path



    # DETERMINE GLOBAL DEPOSITORY-PATH
 line=""
 grep "%depository_path" $config_file  >  $tmp_mbuild
 while read line
 do
    if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
    then
       if [[ "$(echo $line | cut -d" " -s -f3)" = "" ]]
       then
          global_depository_path=`echo $line | cut -d" " -s -f2`
       fi
    fi
 done < $tmp_mbuild

 if [[ $found = false ]]
 then
    printf "\n  +++ no \%depository_path found in" 
    printf "\n                            $config_file"
    locat=depository_path; exit
  fi

    # CHECK, IF A MAIN PROGRAM HAS BEEN DEFINED IN THE CONFIG-FILE
 if [[ $(grep -c "%mainprog" $config_file) != 1 ]]
 then
    printf "\n  +++ no main program or more than one main program defined"
    printf "\n      in configuration file"
    locat=configuration; exit
 else
    line=`grep "%mainprog" $config_file`
    if [[ "$line" = ""  ||  $(echo $line | cut -c1) = "#" ]]
    then
       printf "\n  +++ no main program defined in configuration file"
       locat=configuration; exit
    fi
    mainprog=`echo $line | cut -d" " -s -f2 | cut -d"." -f1`
 fi



    # CHECK IF MAKEFILE EXITS
 [[ "$makefile" = "" ]]  &&  makefile=$local_source_path/Makefile
 if [[ ! -f $makefile ]]
 then
    printf "\n  +++ makefile: "
    printf "\n           $makefile"
    printf "\n      does not exist"
    locat=makefile; exit  
 fi


    # HEADER-OUTPUT (PART1: MESSAGES CONCERNING THE LOCAL HOST)
 calltime=$(date)
 printf "\n"
 printf "#------------------------------------------------------------------------# \n"
 printf "| $version$calltime | \n"
 printf "|                                                                        | \n"
 column1="called on:"; column2=$local_host_real_name
 printf "| $column1$column2 | \n"
 column1="local username:"; column2=$local_username
 printf "| $column1$column2 | \n"
 column1="local IP-addres:"; column2=$local_addres
 printf "| $column1$column2 | \n"
 column1="config file:"; column2=$config_file
 printf "| $column1$column2 | \n"
 column1="makefile:"; column2=$makefile
 printf "| $column1$column2 | \n"
 column1="local source path:"; column2=$local_source_path
 printf "| $column1$column2 | \n"
 printf "#------------------------------------------------------------------------# \n"

# printf "|                                                                        | \n"


 if [[ $compile_utility_programs = false ]]
 then

       # IN ANY CASE, GIVE ALL FILES WRITE-PERMIT, IN ORDER TO AVOID PROBLEMS
       # WITH OVERWRITING FILES ON THE REMOTE HOST
    cd  $local_source_path
    printf "\n\n  *** tar of makefile and source files in $local_source_path"  
    tar -cf  ${mainprog}_sources.tar  Makefile  *.$suf
    printf "\n"

 else
    cd  $local_source_path
    printf "\n\n  *** tar of makefile and source files in $local_source_path"  
 
    cat Makefile_check|while read line
    do
       line=$(echo $line|grep RCS)
       if [[ $line == *"RCS"* ]]
       then
          line=$(echo $line|sed 's/RCS = //g')
          break
       fi
    done

    tar -cf  ${mainprog}_sources_check.tar  Makefile_check  $line
    printf "\n"
 fi



    # GET CONFIRMATION TO CONTINUE
 if [[ $host = all ]]
 then
    printf "\n  *** updates will be made for ALL hosts found in"
    printf "\n      the configuration file"
 else
    printf "\n  *** update will be made for host \"$host\" "
 fi

 if [[ $silent = false ]]
 then
    answer=dummy
    printf "\n\n"
    while [[ "$answer" != y  &&  "$answer" != Y  &&  "$answer" != n  &&  "$answer" != N ]]
    do
       printf " >>> continue (y/n) ?  "
       read  answer
    done
    if [[ $answer = n  ||  $answer = N ]]
    then
       locat=user_abort; exit
    fi
 fi
 
 

    
    # GENERIERUNG DER AKTUELLEN MODELLVERSION FUER ALLE RECHNER-/UEBERSETZUNGS-
    # VERSIONEN, DIE IN DER KONFIGURATIONSDATEI GEFUNDEN WERDEN
 printf "\n  *** scanning configuration file for host(s) ..."

 grep  %fopts  $config_file  >  $tmp_mbuild
 while read line
 do
       # KOMMENTARZEILEN UEBERSPRINGEN
    [[ $(echo $line | cut -c1) = "#" ]]  &&  continue
    (( ihost = ihost + 1 ))
    hostline[$ihost]="$line"
 done < $tmp_mbuild

 
 while (( ih < ihost ))
 do

    (( ih = ih + 1 ))

       # determine remote host and conditions for the respective block
       # continue, only if this host has been chosen via -h option and if
       # conditions have been chosen via -K option
    remote_host_string=`echo ${hostline[$ih]} | cut -d" " -s -f3-`
    remote_host=`echo $remote_host_string | cut -d" " -f1`
    if [[ $host != all ]]
    then
       [[ $remote_host != $host ]]  &&  continue
    fi
    host_found=true
    condition1=`echo $remote_host_string | cut -d" " -s -f2`
    if [[ $condition1 = $remote_host ]]
    then
       condition1=""
    else
       condition2=`echo $remote_host_string | cut -d" " -s -f3`
    fi

    if [[ $block_conditions != none ]]
    then
       if [[ "$condition1" != "$block_condition1"  || "$condition2" != "$block_condition2" ]]
       then
          continue
       fi
       block_conditions_found=true
    fi

    modules=""
    netcdf_inc=""
    netcdf_lib=""
    make_options=""

       # IP-ADRESSE DES REMOTE-RECHNERS BESTIMMEN
    case  $remote_host  in
        (lckordi)        remote_adress=210.219.61.8;;
        (lcmuk)          remote_addres=130.75.105.2;;
        (lcrte)          remote_addres=133.5.185.60;;
        (lcsb)           remote_adress=147.46.30.151;;
        (lcsgib)         remote_addres=130.73.232.102;;
        (lcsgih)         remote_addres=130.75.4.101;;
        (lck)            remote_addres=165.132.26.61;;
        (lctit)          remote_addres=10.1.6.170;;
        (lcxe6)          remote_addres=129.177.20.113;;
        (lcxt5m)         remote_addres=193.166.211.144;;
        (decalpha)       remote_addres=165.132.26.56;;
        (ibmh)           remote_addres=136.172.40.15;;
        (ibmkisti)       remote_addres=150.183.146.24;;
        (ibmku)          remote_addres=133.5.4.129;;
        (ibms)           remote_addres=150.183.5.101;;
        (ibmy)           remote_addres=165.132.26.58;;
        (nech)           remote_addres=136.172.44.192;;
        (neck)           remote_addres=133.5.178.11;;
        (ground.yonsei.ac.kr) remote_addres=134.75.155.33;;
        (*)              if [[ $local_host != $remote_host ]]
                         then
                            printf "\n  +++ remote host \"$remote_host\" unknown";
                            printf "\n      please inform PALM group support!"
                         fi;;
    esac


       # REMOTE-USERNAMEN ERMITTELN
    line=""
    found=false
    grep  "$remote_host_string" $config_file | grep "%remote_username"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
          remote_username=`echo $line | cut -d" " -s -f2`
          found=true
       fi

    done < $tmp_mbuild

    if [[ $found = false ]]
    then
       printf "\n  +++ no remote username found in configuration file"
       printf "\n      for \"$remote_host_string\" "
       locat=config_file; exit
    fi


       # REMOTE-QUELLTEXTPFAD ERMITTELN
    line=""
    remote_source_path=""
    grep  "$remote_host_string" $config_file | grep "%source_path"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
          remote_source_path=`echo $line | cut -d" " -s -f2`
       fi

    done < $tmp_mbuild

    if [[ "$remote_source_path" = "" ]]
    then
       if [[ "$global_source_path" != "" ]]
       then
          remote_source_path=$global_source_path
       else
          printf "\n  +++ no source path found in configuration file"
          printf "\n      for \"$remote_host_string\" "
          locat=config_file; exit
       fi
    fi

    remote_ud=${remote_source_path}/../UTIL
    remote_ud=$(eval echo $remote_ud)


       # REMOTE-PFAD FUER MAKE-DEPOSITORY ERMITTELN
    remote_md=""
    line=""
    grep  "$remote_host_string" $config_file | grep "%depository_path"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
          remote_md=`echo $line | cut -d" " -s -f2`
       fi

    done < $tmp_mbuild

    if [[ "$remote_md" = "" ]]
    then
       if [[ "$global_depository_path" != "" ]]
       then
          remote_md=$global_depository_path
       else
          printf "\n  +++ no depository path found in configuration file"
          printf "\n      for \"$remote_host_string\" "
          printf "\n      please set \"\%depository_path\" in configuration file"
          locat=config_file; exit
       fi
    fi

    remote_md=$(eval echo $remote_md)
    block=""
    [[ "$condition1" != "" ]]  &&  block=_$condition1
    [[ "$condition2" != "" ]]  &&  block=${block}_$condition2
    remote_md=${remote_md}$block


       # COMPILERNAMEN ERMITTELN
    line=""
    found=false
    grep  "$remote_host_string" $config_file | grep "%compiler_name "  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
          compiler_name=`echo $line | cut -d" " -s -f2`
          found=true
       fi

    done < $tmp_mbuild

    if [[ $found = false ]]
    then
       printf "\n  +++ no compiler name found in configuration file"
       printf "\n      for \"$remote_host_string\" "
       locat=config_file; exit
    fi


       # BEI BENUTZUNG EINES PARALLELEN COMPILERS MUSS AUCH EIN
       # SERIELLER COMPILERNAME ERMITTELT WERDEN
    if [[ $(echo $remote_host_string | grep -c parallel) = 1 ]]
    then
       line=""
       found=false
       grep  "$remote_host_string" $config_file | grep "%compiler_name_ser"  >  $tmp_mbuild
       while read line1
       do

          if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
          then
             line="$line1"
          fi

          if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
          then
             compiler_name_ser=`echo $line | cut -d" " -s -f2`
             found=true
          fi

       done < $tmp_mbuild

       if [[ $found = false ]]
       then
          printf "\n  +++ no serial compiler name found in configuration file"
          printf "\n      for \"$remote_host_string\" "
          locat=config_file; exit
       fi
    else
       compiler_name_ser=$compiler_name
    fi



       # PRAEPROZESSOR-OPTIONEN/DIREKTIVEN ERMITTELN
    line=""
    found=false
    grep  "$remote_host_string" $config_file | grep "%cpp_options"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
             # EVENTUELLE DOPPELPUNKTE AUS OPTIONSSTRING ENTFERNEN
          cpp_options=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g'`
          found=true
       fi

    done < $tmp_mbuild

    if [[ $found = false ]]
    then
       printf "\n  +++ no preprocessor options found in configuration file"
       printf "\n      for \"$remote_host_string\" "
       locat=config_file; exit
    fi


       # RECHNERSPEZIFISCHE CPP-DIREKTIVEN HINZUFUEGEN
    for  string  in  $remote_host_string
    do
       if [[ $(echo $remote_host | cut -c1-2) = lc  &&  $(echo $string | cut -c1-2) = lc ]]
       then
          cpp_options="$cpp_options -D__lc "
       elif [[ $(echo $remote_host | cut -c1-3) = ibm  &&  $(echo $string | cut -c1-3) = ibm ]]
       then
          cpp_options="${cpp_options},-D__ibm"
       elif [[ $(echo $remote_host | cut -c1-3) = nec  &&  $(echo $string | cut -c1-3) = nec ]]
       then
          cpp_options="${cpp_options} -D__nec"
       else
          if [[ $(echo $remote_host | cut -c1-3) = ibm ]]
          then
             cpp_options="${cpp_options},-D__$string"
          else
             cpp_options="$cpp_options -D__$string "
          fi
       fi
    done



       # NETCDF-OPTIONEN ERMITTELN
    line=""
    grep  "$remote_host_string" $config_file | grep "%netcdf_inc"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
             # EVENTUELLE DOPPELPUNKTE AUS OPTIONSSTRING ENTFERNEN
          netcdf_inc=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g'`
       fi

    done < $tmp_mbuild

    line=""
    grep  "$remote_host_string" $config_file | grep "%netcdf_lib"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
             # EVENTUELLE DOPPELPUNKTE AUS OPTIONSSTRING ENTFERNEN
          netcdf_lib=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g'`
       fi

    done < $tmp_mbuild



       # get make options
    line=""
    found=false
    grep  "$remote_host_string" $config_file | grep "%mopts"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
             # remove colons from directive string
          make_options=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g'`

       fi

    done < $tmp_mbuild



       # COMPILEROPTIONEN ERMITTELN
    line=""
    found=false
    grep  "$remote_host_string" $config_file | grep "%fopts"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
             # EVENTUELLE DOPPELPUNKTE AUS DIREKTIVENSTRING ENTFERNEN
          compiler_options=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g'`
          found=true

             # NETCDF-INCLUDEVERZEICHNIS HINZUFUEGEN
          compiler_options="$compiler_options $netcdf_inc"
       fi

    done < $tmp_mbuild

    if [[ $found = false ]]
    then
       printf "\n  +++ no compiler options found in configuration file"
       printf "\n      for \"$remote_host_string\" "
       locat=config_file; exit
    fi


       # get login init commands, "::" is replacing a space
    line=""
    grep  "$remote_host_string" $config_file | grep "%login_init_cmd"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
             # EVENTUELLE DOPPELPUNKTE AUS DIREKTIVENSTRING ENTFERNEN
          init_cmds=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g'`
       fi
       init_cmds="${init_cmds};"
    done < $tmp_mbuild


       # get modules to be loaded
    line=""
    grep  "$remote_host_string" $config_file | grep "%modules"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
             # EVENTUELLE DOPPELPUNKTE AUS DIREKTIVENSTRING ENTFERNEN
          modules=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g'`
       fi

    done < $tmp_mbuild


       # LADER-OPTIONEN ERMITTELN
    line=""
    found=false
    grep  "$remote_host_string" $config_file | grep "%lopts"  >  $tmp_mbuild
    while read line1
    do

       if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
       then
          line="$line1"
       fi

       if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
       then
             # EVENTUELLE DOPPELPUNKTE AUS DIREKTIVENSTRING ENTFERNEN
          loader_options=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g'`
          found=true

             # NETCDF-LIBRARY HINZUFUEGEN
          loader_options="$loader_options $netcdf_lib"
       fi

    done < $tmp_mbuild

    if [[ $found = false ]]
    then
       printf "\n  +++ no loader options found in configuration file"
       printf "\n      for \"$remote_host_string\" "
       locat=config_file; exit
    fi


    printf "\n\n#------------------------------------------------------------------------# \n"
    if [[ $remote_host = $local_host ]]
    then
       column1="remote_host:"; column2="$remote_host (= local host!)"
    else
       column1="remote_host:"; column2=$remote_host
    fi
    printf "| $column1$column2 | \n"
    printf "|                                                                        | \n"
    column1="conditions:"; column2="$condition1 $condition2"
    printf "| $column1$column2 | \n"
    column1="make depository:"; column2=$remote_md
    printf "| $column1$column2 | \n"
    line=$(echo "$remote_md" | cut -c51-)
    while [[ "$line" != "" ]]
    do
       column1=""
       column2=$line
       printf "| $column1$column2 | \n"
       line=$(echo "$line" | cut -c51-)
    done
    if [[ $compile_utility_programs = true ]]
    then
       column1="utility directory:"; column2=$remote_ud
       printf "| $column1$column2 | \n"
    fi
    column1="username:"; column2=$remote_username
    printf "| $column1$column2 | \n"
    column1="addres:"; column2=$remote_addres
    printf "| $column1$column2 | \n"
    column1="compiler:"; column2=$compiler_name
    printf "| $column1$column2 | \n"
    if [[ $compile_utility_programs = true ]]
    then
       column1="serial compiler:"; column2=$compiler_name_ser
       printf "| $column1$column2 | \n"
    fi
    if [[ "$make_options" != "" ]]
    then
       column1="make options:"; column2=$make_options
       printf "| $column1$column2 | \n"
    fi
    column1="cpp options:"; column2=$cpp_options
    printf "| $column1$column2 | \n"
    line=$(echo "$cpp_options" | cut -c51-)
    while [[ "$line" != "" ]]
    do
       column1=""
       column2=$line
       printf "| $column1$column2 | \n"
       line=$(echo "$line" | cut -c51-)
    done
    column1="compiler options:"; column2=$compiler_options
    printf "| $column1$column2 | \n"
    line=$(echo "$compiler_options" | cut -c51-)
    while [[ "$line" != "" ]]
    do
       column1=""
       column2=$line
       printf "| $column1$column2 | \n"
       line=$(echo "$line" | cut -c51-)
    done
    column1="loader options:"; column2=$loader_options
    printf "| $column1$column2 | \n"
    line=$(echo "$loader_options" | cut -c51-)
    while [[ "$line" != "" ]]
    do
       column1=""
       column2=$line
       printf "| $column1$column2 | \n"
       line=$(echo "$line" | cut -c51-)
    done
    if [[ $modules != "" ]]
    then
       column1="modules to be load:"; column2=$modules
       printf "| $column1$column2 | \n"
       line=$(echo "$modules" | cut -c51-)
       while [[ "$line" != "" ]]
       do
          column1=""
          column2=$line
          printf "| $column1$column2 | \n"
          line=$(echo "$line" | cut -c51-)
       done
    fi
    printf "#------------------------------------------------------------------------# \n"

    if [[ $silent = false ]]
    then
       answer=dummy
       printf "\n\n"
       while [[ "$answer" != y  &&  "$answer" != Y  && "$answer" != c  &&  "$answer" != C  && "$answer" != s  &&  "$answer" != S  &&  "$answer" != a  &&  "$answer" != A ]]
       do
          printf " >>> continue (y(es)/c(ontinue)/a(bort)/s(skip)) ?  "
          read  answer
       done
       if [[ $answer = a  ||  $answer = A ]]
       then
          locat=user_abort; exit
       fi
       if [[ $answer = c  ||  $answer = C ]]
       then
          silent=true
       fi
       if [[ $answer = s  ||  $answer = S ]]
       then
          continue
       fi
    fi


       # make on remote host
    if [[ $remote_host != $local_host ]]
    then
       if [[ $compile_utility_programs = false ]]
       then

             # AKTUELLE QUELLTEXTVERSION INS REMOTE-QUELLTEXTVERZEICHNIS KOPIEREN
             # FALLS DIESES NOCH NICHT EXISTIERT, WIRD ES ERZEUGT
          echo "  *** copying \"${mainprog}_sources.tar\" to \"${remote_addres}:${remote_md}/\" "
          if [[ $remote_host != lctit ]]
          then
             ssh  ${remote_username}@${remote_addres} "[[ ! -d ${remote_md} ]]  &&  (echo \"  *** ${remote_md} will be created\"; mkdir -p  ${remote_md})"
          else
                # TIT ERLAUBT NUR DIE AUSF�HRUNG GANZ BESTIMMTER KOMMANDOS
                # MIT SSH, DESHALB AUFRUF PER PIPE
             print "[[ ! -d ${remote_md} ]]  &&  (echo \"  *** ${remote_md} will be created\"; mkdir -p  ${remote_md})"  |  ssh ${remote_username}@${remote_addres}  2>&1
          fi
          if [[ $local_host = decalpha ]]
          then
                # DECALPHA BENUTZT BEI NICHTANGABE DES VOLLST�NDIGEN PFADES
                # IRGENDEIN ANDERES SCP (WAS NICHT FUNKTIONIERT). AUSSERDEM
                # KOENNEN DOLLAR-ZEICHEN NICHT BENUTZT WERDEN
             remote_md=`echo $remote_md | sed 's/\$HOME\///'`
             /bin/scp  ${local_source_path}/${mainprog}_sources.tar  ${remote_username}@${remote_addres}:${remote_md}/${mainprog}_sources.tar
          else
             scp  ${local_source_path}/${mainprog}_sources.tar  ${remote_username}@${remote_addres}:${remote_md}/${mainprog}_sources.tar
          fi



             # FALLS VORHANDEN, LETZTE VERSION AUF DEM REMOTE-RECHNER AUSPACKEN
          echo "  *** untar previous update on remote host, if existing"
          if [[ $remote_host != lctit ]]
          then
             ssh  ${remote_username}@${remote_addres}  "cd ${remote_md}; [[ -f ${mainprog}_current_version.tar ]]  &&  tar -xf  ${mainprog}_current_version.tar"
          else
                # TIT ERLAUBT NUR DIE AUSF�HRUNG GANZ BESTIMMTER KOMMANDOS
                # MIT SSH, DESHALB AUFRUF PER PIPE
             print  "cd ${remote_md}; [[ -f ${mainprog}_current_version.tar ]]  &&  tar -xf  ${mainprog}_current_version.tar"  |  ssh  ${remote_username}@${remote_addres}  2>&1
          fi


             # AKTUELLE QUELLTEXTVERSION AUF REMOTE-RECHNER AUSPACKEN
          echo "  *** untar actual sources on remote host"
          if [[ $remote_host != lctit ]]
          then
             ssh  ${remote_username}@${remote_addres}  "cd ${remote_md}; tar -xf  ${mainprog}_sources.tar"
          else
                # TIT ERLAUBT NUR DIE AUSF�HRUNG GANZ BESTIMMTER KOMMANDOS
                # MIT SSH, DESHALB AUFRUF PER PIPE
             print  "cd ${remote_md}; tar -xf  ${mainprog}_sources.tar"  |  ssh  ${remote_username}@${remote_addres}  2>&1
          fi


             # MAKE MIT ZUVOR ERMITTELTEN OPTIONEN AUF REMOTE RECHNER AUSFUEHREN
             # KOMMANDOUEBERGABE AN SSH PER PIPE, DA SO DIE SYSTEM- UND
             # BENUTZERPROFILE VOLLSTAENDIG AUSGEFUEHRT WERDEN (SONST FEHLEN MAKE
             # Z.B. DIE PFADE ZUM COMPILER)
          echo "  *** execute \"make\" on remote host"


             # generate make call with make options
          if [[ $remote_host = nech ]]
          then
             make_call_string="sxmake  $make_options  PROG=$mainprog  F90=$compiler_name  COPT=\"$cpp_options\"  F90FLAGS=\"$compiler_options\"  LDFLAGS=\"$loader_options\" "
          else
             make_call_string="make  $make_options  PROG=$mainprog  F90=$compiler_name  COPT=\"$cpp_options\"  F90FLAGS=\"$compiler_options\"  LDFLAGS=\"$loader_options\" "
          fi

             # generate command to load modules, if modules are given
          if [[ "$modules" != "" ]]
          then
             if [[ $remote_host = lctit ]]
             then
                module_calls=". $modules"
             else
                module_calls="module load ${modules};"
             fi

                # bugfix for wrong netcdf module
             if [[ $remote_host = lcsgib  ||  $remote_host = lcsgih ]]
             then
                if [[ $(echo $module_calls | grep -c netcdf/3.6.3-intel) != 0 ]]
                then
                   module_calls="$module_calls export LD_LIBRARY_PATH=/sw/dataformats/netcdf/3.6.3-intel/lib:\$LD_LIBRARY_PATH;"
                fi
             fi
          else
             module_calls=""
          fi

          if [[ $remote_host = ibmkisti  ||  $remote_host = ibms  ||  $remote_host = ibmy ]]
          then

             ssh  ${remote_username}@${remote_addres}  "$init_cmds $module_calls cd ${remote_md}; echo '$make_call_string' > LAST_MAKE_CALL; chmod u+x LAST_MAKE_CALL; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" 2>&1 | tee ${remote_host}_last_make_protokoll

          elif [[ $remote_host = ibmh ]]
          then

             print "$init_cmds $module_calls export OBJECT_MODE=64; cd ${remote_md}; echo $make_call_string > LAST_MAKE_CALL; chmod u+x LAST_MAKE_CALL; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" | ssh  ${remote_username}@${remote_addres} 2>&1 | tee ${remote_host}_last_make_protokoll

          elif [[ $remote_host = lcsgib  ||  $remote_host = lcsgih ]]
          then
#             print ". /usr/share/modules/init/bash; $module_calls  cd ${remote_md}; echo $make_call_string > LAST_MAKE_CALL; chmod u+x LAST_MAKE_CALL; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" | ssh  ${remote_username}@${remote_addres} 2>&1 | tee ${remote_host}_last_make_protokoll
             print "$init_cmds $module_calls cd ${remote_md}; echo $make_call_string > LAST_MAKE_CALL; chmod u+x LAST_MAKE_CALL; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" | ssh  ${remote_username}@${remote_addres} 2>&1 | tee ${remote_host}_last_make_protokoll

          elif [[ $remote_host = lctit ]]
          then

             echo  " "  >  ${remote_host}_last_make_protokoll
             while [[ $(cat ${remote_host}_last_make_protokoll | grep -c "Forwarding to N1GE") = 0 ]]
             do
                print "cd ${remote_md}; echo $make_call_string > LAST_MAKE_CALL; chmod u+x LAST_MAKE_CALL; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" | ssh  ${remote_username}@${remote_addres} 2>&1 | tee ${remote_host}_last_make_protokoll
             done

          elif [[ $remote_host = lcxe6 ]]
          then

             ssh  ${remote_username}@${remote_addres} "$init_cmds $module_calls cd ${remote_md}; echo $make_call_string > LAST_MAKE_CALL; chmod u+x LAST_MAKE_CALL; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" 2>&1 | tee ${remote_host}_last_make_protokoll

          else

             print "$init_cmds $module_calls cd ${remote_md}; echo $make_call_string > LAST_MAKE_CALL; chmod u+x LAST_MAKE_CALL; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" | ssh  ${remote_username}@${remote_addres} 2>&1 | tee ${remote_host}_last_make_protokoll

          fi

          if [[ $(grep -c MAKE_ERROR ${remote_host}_last_make_protokoll) != 0 ]]
          then
             printf "\a\n  +++ error(s) occurred during compiling or linking on host \"$remote_host\" "
             if [[ $silent = false ]]
             then
                answer=dummy
                printf "\n"
                while [[ "$answer" != c  &&  "$answer" != k ]]
                do
                   printf "  >>> continue / list errors / kill mbuild (c/l/k) ? "
                   read  answer
                   if [[ "$answer" = l ]]
                   then
                      more ${remote_host}_last_make_protokoll
                   fi
                done
                if [[ $answer = k ]]
                then
                   locat=user_abort; exit
                fi
             fi
          fi



             # NEUE VERSION AUF REMOTE-RECHNER ZUSAMMENPACKEN
          printf "\n  *** tar update on remote host ..."
          if [[ $remote_host != lctit ]]
          then
             ssh  ${remote_username}@${remote_addres}  "cd ${remote_md}; chmod u+w *; tar -cf  ${mainprog}_current_version.tar  ${mainprog}  *.f90 *.o *.mod"
          else
                # TIT ERLAUBT NUR DIE AUSF�HRUNG GANZ BESTIMMTER KOMMANDOS
                # MIT SSH, DESHALB AUFRUF PER PIPE
             print  "cd ${remote_md}; chmod u+w *; tar -cf  ${mainprog}_current_version.tar  ${mainprog}  *.f90 *.o *.mod"  |  ssh  ${remote_username}@${remote_addres}  2>&1
          fi


             # AKTUELLES VERSIONSVERZEICHNIS AUF REMOTE-RECHNER BEREINIGEN
#          printf "\n  *** \"make clean\" on remote host ..."
#          ssh  ${remote_username}@${remote_addres}  "cd ${remote_md}; make clean; rm ${mainprog}_sources.tar; rm *.f90 Makefile"
#          printf "\n"




          # GLEICHE AKTIONEN FUER DIE UTILITY-PROGRAMME DURCHFUEHREN
          # AKTUELLE QUELLTEXTVERSION INS REMOTE-QUELLTEXTVERZEICHNIS KOPIEREN
          # FALLS DIESES NOCH NICHT EXISTIERT, WIRD ES ERZEUGT
       elif [[ $compile_utility_programs = true ]]
       then

          printf "\n\n"
          echo "  *** copying scripts and utility programs to \"${remote_addres}:${remote_ud}/\" "
          cd  ${local_source_path}/../SCRIPTS

          if [[ $remote_host != lctit ]]
          then
             ssh  ${remote_username}@${remote_addres} "[[ ! -d ${remote_ud} ]]  &&  (echo \"  *** ${remote_ud} will be created\"; mkdir -p  ${remote_ud}); [[ ! -d ${remote_ud}/../SCRIPTS ]]  &&  (echo \"  *** ${remote_ud}/../SCRIPTS will be created\"; mkdir -p ${remote_ud}/../SCRIPTS)"
          else
                # TIT ERLAUBT NUR DIE AUSFUEHRUNG GANZ BESTIMMTER KOMMANDOS
                # MIT SSH, DESHALB AUFRUF PER PIPE
             print "[[ ! -d ${remote_ud} ]]  &&  (echo \"  *** ${remote_ud} will be created\"; mkdir -p  ${remote_ud}); [[ ! -d ${remote_ud}/../SCRIPTS ]]  &&  (echo \"  *** ${remote_ud}/../SCRIPTS will be created\"; mkdir -p  ${remote_ud}/../SCRIPTS)"  |  ssh ${remote_username}@${remote_addres}  2>&1
          fi

             # KOPIEREN DER SCRIPTE
          if [[ $local_host = decalpha ]]
          then
                # DECALPHA BENUTZT BEI NICHTANGABE DES VOLLSTAENDIGEN PFADES
                # IRGENDEIN ANDERES SCP (WAS NICHT FUNKTIONIERT). AUSSERDEM
                # KOENNEN DOLLAR-ZEICHEN NICHT BENUTZT WERDEN
             remote_ud=`echo $remote_ud | sed 's/\$HOME\///'`
             /bin/scp  batch_scp mbuild mrun process_dvr_output .dvrserver.config subjob  ${remote_username}@${remote_addres}:${remote_ud}/../SCRIPTS  >  /dev/null
          else
             scp  batch_scp mbuild mrun process_dvr_output .dvrserver.config subjob  ${remote_username}@${remote_addres}:${remote_ud}/../SCRIPTS  >  /dev/null
          fi

          cd  -  > /dev/null
          cd  ${local_source_path}/../UTIL


             # KOPIEREN DER UTILITY-PROGRAMME
          if [[ $local_host = decalpha ]]
          then
                # DECALPHA BENUTZT BEI NICHTANGABE DES VOLLST�NDIGEN PFADES
                # IRGENDEIN ANDERES SCP (WAS NICHT FUNKTIONIERT). AUSSERDEM
                # KOENNEN DOLLAR-ZEICHEN NICHT BENUTZT WERDEN
             remote_ud=`echo $remote_ud | sed 's/\$HOME\///'`
             /bin/scp  Makefile  *.f90  ${remote_username}@${remote_addres}:${remote_ud}  >  /dev/null
          else
             scp  Makefile  *.f90  ${remote_username}@${remote_addres}:${remote_ud}  >  /dev/null
          fi



             # MAKE MIT ZUVOR ERMITTELTEN OPTIONEN AUF REMOTE RECHNER AUSFUEHREN
             # KOMMANDOUEBERGABE AN SSH PER PIPE, DA SO DIE SYSTEM- UND
             # BENUTZERPROFILE VOLLSTAENDIG AUSGEFUEHRT WERDEN (SONST FEHLEN MAKE
             # Z.B. DIE PFADE ZUM COMPILER)
          echo "  *** execute \"make\" on remote host"

          if [[ $remote_host = nech ]]
          then
             make_call_string="sxmake  $make_options  BLOCK=$block  F90=$compiler_name  F90_SER=$compiler_name_ser  COPT=\"$cpp_options\"  F90FLAGS=\"$compiler_options\"  LDFLAGS=\"$loader_options\" "
          else
             make_call_string="make  $make_options  BLOCK=$block  F90=$compiler_name  F90_SER=$compiler_name_ser  COPT=\"$cpp_options\"  F90FLAGS=\"$compiler_options\"  LDFLAGS=\"$loader_options\" "
          fi

             # generate command to load modules, if modules are given
          if [[ "$modules" != "" ]]
          then
             if [[ $remote_host = lctit ]]
             then
                module_calls=". $modules"
             else
                module_calls="module load ${modules};"
             fi

                # bugfix for wrong netcdf module
             if [[ $remote_host = lcsgib  ||  $remote_host = lcsgih ]]
             then
                if [[ $(echo $module_calls | grep -c netcdf/3.6.3-intel) != 0 ]]
                then
                   module_calls="$module_calls export LD_LIBRARY_PATH=/sw/dataformats/netcdf/3.6.3-intel/lib:\$LD_LIBRARY_PATH;"
                fi
             fi
          else
             module_calls=""
          fi


          if [[ $remote_host = ibms  ||  $remote_host = ibmy ]]
          then

             ssh  ${remote_username}@${remote_addres}  "$init_cmds $module_calls cd ${remote_ud}; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR"

          elif [[ $remote_host = ibmh ]]
          then

             print "$init_cmds $module_calls export OBJECT_MODE=64; cd ${remote_ud}; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" | ssh  ${remote_username}@${remote_addres}

          elif [[ $remote_host = lctit ]]
          then

             echo  " "  >  ${remote_host}_last_make_protokoll
             while [[ $(cat ${remote_host}_last_make_protokoll | grep -c "Forwarding to N1GE") = 0 ]]
             do
                print "$init_cmds $module_calls cd ${remote_ud}; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" | ssh  ${remote_username}@${remote_addres} 2>&1 | tee ${remote_host}_last_make_protokoll
             done

          elif [[ $remote_host = lcxe6 ]]
          then

             ssh  ${remote_username}@${remote_addres} "$init_cmds $module_calls cd ${remote_ud}; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" 2>&1 | tee ${remote_host}_last_make_protokoll

          else

             print "$init_cmds $module_calls cd ${remote_ud}; $make_call_string; [[ \$? != 0 ]] && echo MAKE_ERROR" | ssh  ${remote_username}@${remote_addres} 2>&1 | tee ${remote_host}_last_make_protokoll

          fi

       fi    # ENDE UEBERSETZUNG DER UTILITY-PROGRAMME

       rm -rf  ${remote_host}_last_make_protokoll

       # make on local host
    else

          # workaround for lcxe6
       if [[ $remote_host = lcxe6 ]]
       then

          eval $init_cmds

       fi

       # first load modules, if given
       if [[ "$modules" != "" ]]
       then
          if [[ $remote_host = lctit ]]
          then
             . $modules
          else
             module load ${modules}
          fi
       fi


       if [[ $compile_utility_programs = false ]]
       then

             # DEPOSITORY VERZEICHNIS ERZEUGEN, FALLS NOCH NICHT VORHANDEN
          eval remote_md=$remote_md
          if [[ ! -d $remote_md ]]
          then
             if  mkdir $remote_md
             then
                printf "\n\n  *** directory for make depository:"
                printf "\n           $remote_md"
                printf "\n      was created\n"
             else
                printf "\n  +++ directory for make depository:"
                printf "\n           $remote_md"
                printf "\n      cannot be created"
                locat=local_depository_path; exit
             fi
          fi

             # QUELLTEXT-DATEIEN AUS REPOSITORY INS DEPOSITORY KOPIEREN
          echo " "
          echo "  *** updating sources in $remote_md"
          cd  $remote_md
          cp  $local_source_path/${mainprog}_sources.tar  .
          tar xf  ${mainprog}_sources.tar

             # MAKE MIT ZUVOR ERMITTELTEN OPTIONEN AUF LOKALEM RECHNER AUSFUEHREN
          echo " "
          echo "  *** execute \"make\" on local host"

          make  $make_options  PROG=$mainprog  F90=$compiler_name  COPT="$cpp_options"  F90FLAGS="$compiler_options"  LDFLAGS="$loader_options"  2>&1 | tee ${remote_host}_last_make_protokoll

          if [[ $? != 0 ]]
          then
             printf "\a\n  +++ error(s) occurred during compiling or linking on host \"$remote_host\" "
             if [[ $silent = false ]]
             then
                answer=dummy
                printf "\n"
                while [[ "$answer" != c  &&  "$answer" != k ]]
                do
                   printf "  >>> continue / list errors / kill mbuild (c/l/k) ? "
                   read  answer
                   if [[ "$answer" = l ]]
                   then
                      more ${remote_host}_last_make_protokoll
                   fi
                done
                if [[ $answer = k ]]
                then
                   locat=user_abort; exit
                fi
             fi
          fi


             # TAR NEW VERSION ON LOCAL HOST
          printf "\n  *** tar update on local host ..."
          tar -cf  ${mainprog}_current_version.tar  *.$suf *.o *.mod


          # COMPILE THE UTILITY PROGRAMS
       elif [[ $compile_utility_programs = true ]]
       then
          printf "\n\n"
          echo "  *** compiling the utility programs ..."
          cd ${local_source_path}/../UTIL

             # TOUCH FILES IN ORDER TO FORCE COMPILATION FOR EVERY BLOCK
          touch  *.f90
          make  $make_options  BLOCK=$block  F90=$compiler_name  F90_SER=$compiler_name_ser  COPT="$cpp_options"  F90FLAGS="$compiler_options"  LDFLAGS="$loader_options"

             # CHECK IF QMAKE IS AVAILABLE AND COMPILE MRUNGUI
          if [[ $util_compiled_localhost == false ]]
          then
             printf "\n\n"
             echo "  *** compiling the mrun GUI"
             if which qmake >/dev/null; then
                cd mrungui
                touch *
                qmake
                make
                make clean
                rm Makefile
                cd ..
             else
                echo "  +++ no qmake found. The (optional) GUI will not be compiled."
             fi

             # COMPILE CHECK_NAMELIST_FILES (ONLY FOR ONE BRANCH on LOCALHOST NEEDED)

             printf "\n\n"
             echo "  *** compiling check_namelist_files ..."

             # GET CHECK OPTIONS
             line=""
             found=false
             grep  "$remote_host_string" $config_file | grep "%cpp_options"  >  $tmp_mbuild
             while read line1
             do

                if [[ $(echo $line1 | cut -d" " -s -f3-) = "$remote_host_string" ]]
                then
                   line="$line1"
                fi

                if [[ "$line" != ""  &&  $(echo $line | cut -c1) != "#" ]]
                then
                   # EVENTUELLE DOPPELPUNKTE AUS OPTIONSSTRING UND ALLE -D ENTFERNEN
                   line="$line "
                   copts_check=`echo $line | cut -d" " -s -f2 | sed 's/::/%DUM%/g' | sed 's/:/ /g' | sed 's/%DUM%/:/g' | sed 's/-D[^ ]* //g' | sed 's/ -D.*//g'`
                   found=true
                fi

             done < $tmp_mbuild
             copts_check="$copts_check -D__check -D__parallel"

             check_depository_path=${local_source_path}/../UTIL
             cd $check_depository_path
             mkdir check_tmp
             cp ${local_source_path}/${mainprog}_sources_check.tar ./check_tmp
             cd check_tmp
             tar -xf  ${mainprog}_sources_check.tar
             rm -rf ${mainprog}_sources_check.tar

             make  -f Makefile_check $make_options  F90=$compiler_name_ser  COPT="$copts_check"  F90FLAGS="$compiler_options"
             tar -cf  check_namelist_files.tar Makefile_check check_namelist_files.x *.f90 *.o *.mod
             mv check_namelist_files.tar $check_depository_path
             mv check_namelist_files.x $PALM_BIN
             cd $check_depository_path
             rm -rf check_tmp
             util_compiled_localhost=true
          else
             cd $check_depository_path
             printf "\n\n"
             echo "  *** skipped compilation of mrun GUI."       
             printf "\n\n"
             echo "  *** skipped compilation of check_namelist_files."            
          fi

       fi
    fi
 done


 if [[ $host_found = false ]]
 then
    if [[ $host = all ]]
    then
       printf "\n  +++ no hosts found in configuration file"
    else
       printf "\n  +++ host \"$host\" not found in configuration file"
    fi
    locat=config_file; exit
 fi

 if [[ "$block_conditions" != none  &&  $block_conditions_found = false ]]
 then
    printf "\n  +++ block conditions \"$block_conditions\" not found for host \"$host\""
 fi


    # FINAL WORK
 rm -f  hosts_found_in_config_file
 rm -f ${local_source_path}/${mainprog}_sources_check.tar

