Index: palm/trunk/SCRIPTS/palm_simple_build
===================================================================
--- palm/trunk/SCRIPTS/palm_simple_install	(revision 2718)
+++ palm/trunk/SCRIPTS/palm_simple_build	(revision 2850)
@@ -1,3 +1,3 @@
-#!/bin/bash
+#!/usr/bin/env bash
 #--------------------------------------------------------------------------------#
 # This file is part of the PALM model system.
@@ -24,4 +24,7 @@
 # -----------------
 # $Id$
+# Refactoring of the palm_simple_ build and run scripts
+# 
+# 2718 2018-01-02 08:49:38Z maronga
 # Corrected "Former revisions" section
 #
@@ -40,69 +43,117 @@
 # palm_simple_install - a script for simple installation and compilation of
 #                       the palm code without using mbuild
-     # This script creates (from the working copy of the palm repository)
-     # a subdirectory MAKE_DEPOSITORY_simple which contains a copy of the
-     # palm source code and  a modified makefile which loads required compiler
-     # and preprocessor settings via "include MAKE.inc"
+# This script creates (from the working copy of the palm repository)
+# a subdirectory MAKE_DEPOSITORY_simple which contains a copy of the
+# palm source code and  a modified makefile which loads required compiler
+# and preprocessor settings via "include MAKE.inc"
 
-     # Options: -i <include file>
-     #          one of the include files in ~/palm/current_version/trunk/INSTALL
+# Options: -i <include file>
+#          one of the include files in ~/palm/current_version/trunk/INSTALL
 
-     # Last changes:
-     # 25/01/10 - Siggi - Generating the first version
-     # 18/03/10 - Siggi - switch to palm/current_version removed: working
-     #                    copy can be in any directory
+# Last changes:
+# 25/01/10 - Siggi - Generating the first version
+# 18/03/10 - Siggi - switch to palm/current_version removed: working
+#                    copy can be in any directory
+#------------------------------------------------------------------------------#
+SOURCE="${BASH_SOURCE[0]}"
+while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
+  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+  SOURCE="$(readlink "$SOURCE")"
+  # if $SOURCE was a relative symlink, we need to resolve it
+  # relative to the path where the symlink file was located
+  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
+done
+SCRIPT_LOCATION="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+
+working_dir=$(readlink -f "${SCRIPT_LOCATION}/../../")
+trunk_dir=$(readlink -f "${SCRIPT_LOCATION}/../")
 
 
-    # Variable declarations + default values
- include_file=unknown
+get_number_of_cpu_cores() {
+   {
+      n=$(sysctl -n machdep.cpu.core_count 2> /dev/null)
+   } || {
+      n=$(grep -c ^processor /proc/cpuinfo 2> /dev/null)
+   } || {
+   if ! [[ $n =~ ^-?[0-9]+$ ]]; then
+      n=1
+   fi
+   }
+   echo $n
+}
+
+print_help() {
+   echo "Usage: palm_simple_run -b <build-config> [-x]"
+   echo ""
+   echo "List of allowed option:"
+   echo ""
+   echo "   -b <build-config>   Suffix of any MAKE.inc.<build-config> file in the trunk/INSTALL dir."
+   echo "   -x                  Clean the build directory from last build attempts"
+   echo ""
+}
+
+build_config=unknown
+clean="false"
+
+# Read shellscript options
+while  getopts  :b:xh  option
+do
+  case  $option  in
+      (b)   build_config=$OPTARG;;
+      (x)   clean="true";;
+      (h)   print_help
+            exit 0;;
+      (\?)  echo "Unknown option -$OPTARG"
+            print_help
+            exit 1;;
+  esac
+done
+
+if [[ ${build_config} == "unknown" ]]; then
+   echo "Missing option -b <build-config>"
+   print_help
+   exit 1
+fi
 
 
-    # Read shellscript options
- while  getopts  :i:  option
- do
-   case  $option  in
-       (i)   include_file=$OPTARG;;
-       (\?)  printf "\n  +++ unknown option $OPTARG \n"
-             printf "\n      allowed option are -d, -f, -l, -s \n"
-             exit;;
-   esac
- done
+build_dir=${working_dir}/MAKE_DEPOSITORY_$build_config
 
 
-
-    # Check, if include file exists
- if [[ ! -f trunk/INSTALL/$include_file ]]
- then
-    echo "+++ ERROR: include file"
-    echo "    \"trunk/INSTALL/$include_file\""
-    echo "    not found"
-    exit
- fi
+# Check, if include file exists
+build_config_file=${trunk_dir}/INSTALL/MAKE.inc.$build_config
+if [[ ! -f ${build_config_file} ]]; then
+   echo "+++ ERROR: no such make include file:"
+   echo "    \"${build_config_file}\""
+   exit 1
+fi
 
 
-     # Create the make depository
- if [[ ! -d MAKE_DEPOSITORY_simple ]]
- then
-    mkdir  MAKE_DEPOSITORY_simple
- else
-    rm  MAKE_DEPOSITORY_simple/*
- fi
+# Create the make depository
+if [[ ! -d ${build_dir} ]]; then
+   mkdir -p ${build_dir}
+else
+   if [[ ${clean} == "true" ]]; then
+      echo "Cleaning the build directory..."
+      rm -rf ${build_dir}/*
+   fi
+fi
 
 
-     # Copy makefile and all source code files to make depository
- cp  trunk/SOURCE/Makefile        MAKE_DEPOSITORY_simple/Makefile_old
- cp  trunk/INSTALL/$include_file  MAKE_DEPOSITORY_simple/MAKE.inc
- cp  trunk/SOURCE/*.f90           MAKE_DEPOSITORY_simple
+# Copy makefile and all source code files to make depository
+cp -n ${trunk_dir}/SOURCE/Makefile  ${build_dir}/Makefile_in
+cp -n ${build_config_file}          ${build_dir}/MAKE.inc
+cp -n ${trunk_dir}/SOURCE/*.f90     ${build_dir}
 
 
-     # Replace comment in makefile by include statement
- sed  's/#to_be_replaced_by_include/include MAKE.inc/g'  MAKE_DEPOSITORY_simple/Makefile_old  >  MAKE_DEPOSITORY_simple/Makefile
- rm  MAKE_DEPOSITORY_simple/Makefile_old
+# Replace comment in makefile by include statement
+sed  's/#to_be_replaced_by_include/include MAKE.inc/g'  ${build_dir}/Makefile_in  >  ${build_dir}/Makefile
+rm  ${build_dir}/Makefile_in
+
+cd ${build_dir}
+make -j $(get_number_of_cpu_cores)
+cd ../
 
 
-     # Create directory for input files
- if [[ ! -d JOBS/example_cbl/INPUT ]]
- then
-    mkdir -p  JOBS/example_cbl/INPUT
-    cp trunk/INSTALL/example_cbl_p3d  JOBS/example_cbl/INPUT
- fi
+echo "*** PALM build finished. Executable can be found in:"
+echo "    \"${build_dir}\""
+exit 0
