#!/bin/sh
#/bin/bash
#* Started by Francesco Angelo Brisa, cica January 2008.
#* Massively Modified by Geoff R. McLane, starting January, 2009
#
# Copyright (C) 2008 Francesco Angelo Brisa - http://brisa.homelinux.net
# email: francesco@brisa.homelinux.net   -   fbrisa@yahoo.it
# download: http://brisa.homelinux.net/fgfs/ download_and_compile.sh
# Copyright (C) 2009-2010 Geoff R. McLane - http://geoffair.net/fg
# email: report@geoffair.info
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# DOWNLOAD/UPDATE, and COMPILE git FlightGear, and its dependencies
PROJECT="FlightGear"
FGADD=1     # default to ON
FGRUNADD=1  # default is ON
TGADD=0     # default to OFF - see sanity overrides below
ADDSGCS=0   # ensure SG git source
#####################################################################

# FOR DEBUG ONLY
# set -v on
# set -x on
# discovering linux - get DISTRIB_ID, DISTRIB_RELEASE, DISTRIB_CODENAME, DISTRIB_DESCRIPTION
# . /usr/lsb-release
# or get a string
# LSB_REL=`lsb_release -a 2>/dev/null | tr "\n\t" " "`
# 1.1.3 - if OSG checkout, or switch to non-default OSGTRUNK, delete, and redo scripts
# 1.1.4 - 2010-02-07 - Do something about BOOST... done
# 1.1.5 - Checkout an 'install' boost headers
# 1.1.6 - 2010-02-20 - Update to OSG 2.9.6 developers release, and some other minor changes
# 1.1.7 - 2010-03-01 - Add OSGEXAM, to (a) co data-set, and (b) build examples
# 1.1.8 - 2010-07-10 - Change FG/SG to GIT, see http://gitorious.org/fg, and update OSG release 2.8.3 - 5th April 2010
# 1.1.9 - 2010-07-27 - Fix bug with 'make clean' being done with old 'config.status', with OLD paths
# 1.2.0 - 2010-08-20 - Allow a HELPON avoid a BOOST now action, and add NO_CONF, to NOT read CONF file
SCDATE="2010-08-21"
SCVERSION="1.2.0"
#
# 2009-10-31  - allow option to use alternate OpenAL sources
# OpenAL-Soft 1.9.563 compiled from source from http://kcat.strangesoft.net/openal.html
# http://kcat.strangesoft.net/openal-releases/openal-soft-1.9.563.tar.bz2
# or git clone git://repo.or.cz/openal-soft.git openal-soft
# and ALUT 1.1 compiled from source from
# http://connect.creativelabs.com/openal/Downloads/ALUT/freealut-1.1.0.tar.gz
#
# add --with-fgbase=prefix to Atlas configure step, since we know where the base is loaded
# add OSG stable release as default
# svn co http://www.openscenegraph.org/svn/osg/OpenSceneGraph/tags/OpenSceneGraph-2.8.1 or 2 vs OSGTRUNK
BNAME=`basename $0`
BN=$BNAME
RUNDATE=`date`
RUNTIME=`date +%s`
CURR_ELAPSED=""

# get absolute path of our start
CBD=$(pwd)
# This could be set to something specific, but may need some other changes
COMPILE_BASE_DIR=.
INSTDIR=install
FGFSSCRIPT=run_fgfs.sh
FGVIEWER=run_fgviewer.sh
FGRUNSCRIPT=run_fgrun.sh
ATLASSCRIPT=run_atlas.sh
TMP_DIR="tmp"

FG_VERS_TXT="$TMP_DIR/$PROJECT.txt"
FG_DATE_TXT="$TMP_DIR/fg_date.txt"
NEWDATE=0
OSG_VERS_TXT="$TMP_DIR/osg_version.txt"

# Establish program variables - with default values
#########################################################
RTPATH=""
MAKEFGCFG="$HOME/bin/makefg-$SCVERSION.conf"    # may be later overridden with runtime path
LOCALFGCFG="$CBD/$TMP_DIR/makefg-$SCVERSION.conf"    # a local version only
FROM_CONF_FILE=0
NO_CONF_FILE=0  # NO_CONF option - treat as if none

VALID=''    # hold valid command inputs
ALL_COMP=""
ALL_SKIP=""
ALL_DONE=""
FAILED=0
INVALID=''

NOPAUSE=0      # set to 1, or add command NOPAUSE, to skip all waits for input...
# This output to ./configure, etc to the log file
ADDDEBUG=0

DOUPDATE=0
DOUPD2=0       # skip past tools, PLIB, and OSG update, and update ALL after
TOOLUPD=0
TOOLUPDONLY=0
NO_TOOL_UPD=0    # skip tool and package update

PLIBADD=1      # default to ON
PLIBTRUNK=1    # default to using the svn 'trunk'
PLIBCO=0
PLIBUPD=0
PLIBAUTO=0
PLIBCLEAN=0
PLIBCONF=0
PLIBEX=0        # *** ALSO build the EXAMPLES ***
PLIBCONFEX=0
PLIBAUTOEX=0
PLIBDEBUG=0
PLIB_INSTALL_OK=0
PLIB_SOURCE_DIR=""
INSTALL_DIR_PLIB=""

OSGADD=1       # default to ON
OSGCO=0
OSGUPD=0
OSGCLEAN=0
OSGCMAKE=0
OSGNOUPD=0     # AVOID updating OSG - takes a lot of time
OSGTRUNK=0     # option, for 'trunk' version, else 2.8.1 release
# Install OSG to 'standard' path - BUT THIS FAILED???
OSGTOSTD=0
OSGFIX=1       # add OSG_FIX="-D LIB_POSTFIX=" when running cmake to use 'standard' library path
OSGLINK=1      # create link lib -> lib64, usually if NOT OSGFIX...
OSGUSEREL=1    # use the last general release, not the developers release
OSGNEW=0       # set if the OSG_VERS_TXT file is a different version - should CLEAN SG/FG
OSGEXAM=0      # download the OSG dataset, and build the examples
OSG_SOURCE_DIR=""
INSTALL_DIR_OSG=""
OSG_INSTALL_OK=0

SGADD=1        # default to ON
SGCO=0
SGUPD=0
SGAUTO=0
SGCLEAN=0
SGCONF=0
SGDEBUG=0
SGBOOST=1      # support 'boost' being in other than the DEFAULT install

BOOSTSVN=1     # do an SVN checkout of BOOST to a local folder
DONEBOOST=0
DOBOOSTUPD=0
BOOSTPATH=""   # paths to boost include and libraries
BOOSTLIBS=""   # should be loaded from a persistant boost-path.cfg file
BOOSTVF=""
BOOSTLF=""
WANT_BOOST_VERSION=""
boost_lib_version_req_major="1"
boost_lib_version_req_minor="37"
boost_lib_version_req_sub_minor="0"
BOOST_LIB_VERS="1.37.0"
BOOST_VER_FOUND=""
# download of boost source - presently no build done
BOOST_RSRC="boost_1_43_0"
BOOST_FILE="$BOOST_RSRC.tar.gz"
BOOST_DOWNLOAD="http://geoffair.org/fg/srczips/$BOOST_FILE"

FGCO=0
FGUPD=0
FGAUTO=0
FGCLEAN=0
FGCONF=0
FGDEBUG=0
FGUPDDATA=0
FGADDDATA=1    # for BOTH FG and TG, add data 1.0.6+
FGFS_SOURCE_DIR=""  # source location
FGFS_DATA_DIR=""    # location of FG data download/clone/install
INSTALL_DIR_FGFS="" # install directory

FGRUNCO=0
FGRUNUPD=0
FGRUNCLEAN=0
FGRUNAUTO=0
FGRUNCONF=0
FGRUNDEBUG=0

DELSCRIPTS=0

# OPTIONAL ITEMS
#########################################################
# Atlas
ATLASADD=0     # default to OFF
ATLAS=0
ATLASCO=0
ATLASUPD=0
ATLASAUTO=0
ATLASCONF=0
ATLASCLEAN=0
ATLASDEBUG=0

# TerraGear
TGCVS=0        # 1 to try TG CVS, with CVS SG so ADDSGCS should be OFF
TG=0
TGCO=0
TGUPD=0
TGAUTO=0
TGCONF=0
TGCLEAN=0
TGDEBUG=0
INST_FILE="$BNAME.conf.txt"
##################################################################

HELPON=0
MADETMP=0
DEBUG_ON=0
BASE_IS_SAME=0

# #############################################
# FUNCTIONS - helper functions
# ############################################
echo2()
{
   echo "$1"
   echo "$1" >> $OUTFILE
}
echod()
{
    if [ "$DEBUG_ON" = "1" ]; then
        echo2 "[DEBUG] $1"
    fi
}

secs_to_HHMMSS()
{
    MNS=`expr $1 / 60`
    TMP=`expr $MNS \* 60`
    SCS=`expr $1 - $TMP`
    HRS="0"
    if [ "$MNS" -gt "59" ]; then
        HRS=`expr $MNS / 60`
        TMP=`expr $HRS \* 60`
        MNS=`expr $MNS - $TMP`
    fi
    if [ "$HRS" -lt "10" ]; then
        HRS="0$HRS"
    fi
    if [ "$MNS" -lt "10" ]; then
        MNS="0$MNS"
    fi
    if [ "$SCS" -lt "10" ]; then
        SCS="0$SCS"
    fi
    CURR_ELAPSED="$HRS:$MNS:$SCS"
}

output_the_time()
{
    set +e
    SECS=`date +%s`
    #echod "$BN: Output runtime... $RUNTIME - $SECS"
    if [ "$SECS" = "$RUNTIME" ]; then
        echo2 "$BN: Current time $(date)... elapsed NONE"
    else
        DIFF=`expr $SECS - $RUNTIME`
        #echod "$BN: Output runtime... $DIFF"
        secs_to_HHMMSS $DIFF
        echo2 "$BN: Current time $(date)... elapsed $CURR_ELAPSED"
   fi
   set -e
}

# check if there is a DIFFERENT between the base part $CBD and $1
is_same_base_dir()
{
    BASE_IS_SAME=0
    # echod "is_same_base_dir: Checking [$1] is SAME base as [$CBD]"
    if [ "$#" -gt "0" ]; then
        #if [ -d "$1" ]; then
            LEN1=`expr length $1`
            LEN2=`expr length $CBD`
            # echod "is_same_base_dir: LEN1=$LEN1 LEN2=$LEN2"
            if [ "$LEN1" -gt "$LEN2" ]; then
                SUBD=`echo $1 | cut -b1-$LEN2`
                # echod "is_same_base_dir: Comparing [$SUBD] with [$CBD]"
                if [ "x$SUBD" = "x$CBD" ]; then
                    BASE_IS_SAME=1
                fi
            fi
        #fi
    else
        echo2 "$BN: INTERNAL ERROR! No param passed to 'is_same_base_dir'!"
        exit 1
    fi
}

make_tmp_directory()
{
   MADETMP=1
   mkdir $TMP_DIR
}

create_install_directory()
{
   if [ ! -d $INSTDIR ] ; then
      echo2 "WARNING: This is a NEW installation in the [$CBD] directory!"
      wait_for_input "Create the install directory $INSTDIR?"
      mkdir $INSTDIR
      if [ ! -d $INSTDIR ] ; then
         echo2 "ERROR: Failed to create [$INSTDIR]! Aborting!!"
         exit 1
      fi
   #else
   #  wait_for_input "Proceed to build FlightGear in the [$CBD] directory!"
   fi
}

check_boost_version()
{
    BVF="$1"
    NBVUP=2
    if [ -f "$BVF" ]; then
        BVER=`grep define[^\w]BOOST_VERSION[^\w][0-9] $BVF | awk '{ print $3}'`
        MAJBV=`echo $BVER | cut -b1-1`
        MINBV=`echo $BVER | cut -b2-4`
        BLDBV=`echo $BVER | cut -b5-6`
        if [ "$MAJBV" -lt "2" ]; then
            if [ "$MINBV" -lt "37" ]; then
                NBVUP=1
            else
                NBVUP=0
            fi
        else
            NBVUP=0
        fi
    fi
    echo $NBVUP
}

install_svn_boost()
{
    svn co http://svn.boost.org/svn/boost/trunk boost-trunk
    if [ -d boost-trunk ]; then
        cd boost-trunk
        if [ -f bootstrap.sh ]; then
            if [ ! -f bjam ]; then
                ./boostrap.sh
            fi
            if [ -f bjam ]; then
                ./bjam
            fi
        fi
    fi
}

install_boost_tar_gz()
{
   if [ ! -d "$CBD/install" ]; then

      echo2 "ERROR: Failed to create [$INSTDIR]! Aborting!!"
      exit 1
   fi
   cd "$CBD/install"
   echo2 "$BN: Running svn co http://svn.boost.org/svn/boost/trunk boost"
   svn co http://svn.boost.org/svn/boost/trunk boost
   if [ -d boost ]; then
      cd boost
      echo2 "$BN: Moving boost headers to an 'include' directory..."
      mkdir include
      if [ -d include ]; then
         mv boost include
         DIRLST="doc libs more people status tools wiki"
         echo2 "$BN: Cleaning up directories $DIRLST..."
         for arg in $DIRLST; do
            if [ -d $arg ]; then
               rm -rf $arg
            fi
         done
      else
         echo2 "ERROR: Creation of 'include' directory FAILED! Aborting!!"
         exit 1
      fi
      cd ..
      echo2 "$BN: Done Boost checkout, move and cleanup..."
   else
      echo2 "ERROR: svn checkout of Boost FAILED! Aborting!!"
      exit 1
   fi
   cd $CBD
}

show_boost_version()
{
    BVF="$1"
    NBVUP=2
    echod "$BN: Checking boost version in [$BVF]"
    BOOSTOK=0
    BOOSTFIND=0
    if [ -f "$BVF" ]; then
        BOOSTFIND=1
        BVER=`grep define[^\w]BOOST_VERSION[^\w][0-9] $BVF | awk '{ print $3}'`
        MAJBV=`echo $BVER | cut -b1-1`
        MINBV=`echo $BVER | cut -b2-4`
        CHR1=`echo $MINBV | cut -b1-1`
        if [ "$CHR1" = "0" ]; then
            MINBV=`echo $MINBV | cut -b2-3`
        fi
        BLDBV=`echo $BVER | cut -b5-6`
        # BOOST_LIB_VERS="1.37.0"
        BOOST_VER_FOUND="$MAJBV.$MINBV.$BLDBV"
        echod "$BN: Got boost version [$BVER] [$MAJBV.$MINBV.$BLDBV]"
        if [ "$MAJBV" -gt "$boost_lib_version_req_major" ]; then
            BOOSTOK=1
            NBVUP=0
        elif [ "$MAJBV" -lt "$boost_lib_version_req_major" ]; then
            BOOSTOK=0
            NBVUP=1
        else
            # major version is the same, so depends on minor version
            if [ "$MINBV" -ge "$boost_lib_version_req_minor" ]; then
                BOOSTOK=1
                NBVUP=0
            else
                BOOSTOK=0
                NBVUP=1
            fi
        fi
    else
        BOOSTOK=2
        BOOSTFIND=0
        echo2 "$BN: Unable to locate file [$BVF]!"
    fi
}


check_boost_path()
{
    echod "Checking BOOST PATH [$BOOSTPATH]"
    if [ -z "$BOOSTPATH" ]; then
        echod "$BN: No BOOSTPATH given..."
        if [ "$NO_CONF_FILE" = "1" ]; then
            echo2 "$BN: Assume CONF file... defaulting to [/usr]..."
            BOOSTPATH="/usr"
        elif [ -f "$MAKEFGCFG" ]; then
            echod "$BN: Load from [$MAKEFGCFG] config file..."
            TMP=`grep -v '#' $MAKEFGCFG | grep BOOSTPATH`
            if [ "$?" = "0" ] && [ ! -z "$TMP" ]; then
                echod "$BN: TMP=[$TMP]"
                CNT=`expr length $TMP`
                echod "$BN: CNT=[$CNT]"
                if [ "$CNT" -gt "11" ]; then
                    BOOSTPATH=`echo $TMP | cut -b11-$CNT`
                    echo2 "$BN: From CONF file, BOOSTPATH=[$BOOSTPATH]"
                    FROM_CONF_FILE=`expr $FROM_CONF_FILE + 1`
                else
                    echo2 "$BN: length error! No BOOSTPATH in CONF file... defaulting to [/usr]..."
                    BOOSTPATH="/usr"
                fi
            else
                echo2 "$BN: grep failed! No BOOSTPATH in CONF file... defaulting to [/usr]..."
                BOOSTPATH="/usr"
            fi
        else
            echo2 "$BN: No CONF file... defaulting to [/usr]..."
            BOOSTPATH="/usr"
        fi
    fi
    if [ -z "$BOOSTLIBS" ]; then
        echod "$BN: No BOOSTLIBS given..."
        if [ "$NO_CONF_FILE" = "1" ]; then
            echo2 "$BN: Assume no CONF file... defaulting to [$BOOSTPATH]..."
            BOOSTLIBS="$BOOSTPATH"
        elif [ -f "$MAKEFGCFG" ]; then
            echod "$BN: Load from [$MAKEFGCFG] config file..."
            TMP=`grep -v '#' $MAKEFGCFG | grep BOOSTLIBS`
            if [ "$?" = "0" ] && [ ! -z "$TMP" ]; then
                echod "$BN: TMP=[$TMP]"
                CNT=`expr length $TMP`
                echod "$BN: CNT=[$CNT]"
                if [ "$CNT" -gt "11" ]; then
                    BOOSTLIBS=`echo $TMP | cut -b11-$CNT`
                    echo2 "$BN: From CONF file, BOOSTLIBS=[$BOOSTLIBS]"
                    FROM_CONF_FILE=`expr $FROM_CONF_FILE + 1`
                else
                    echo2 "$BN: length error! No BOOSTLIBS in CONF file... defaulting to [$BOOSTPATH]..."
                    BOOSTLIBS="$BOOSTPATH"
                fi
             else
                echo2 "$BN: grep failed! No BOOSTLIBS in CONF file... defaulting to [$BOOSTPATH]..."
                BOOSTLIBS="$BOOSTPATH"
             fi
        else
            echo2 "$BN: No CONF file... defaulting to [$BOOSTPATH]..."
            BOOSTLIBS="$BOOSTPATH"
        fi
    fi

    # check for known LIBRARY
    if [ -d "$BOOSTLIBS" ]; then
        BLF="$BOOSTLIBS/lib/libboost_serialization.a"
        if [ ! -f "$BLF" ]; then
            BLF="$BOOSTLIBS/stage/lib/libboost_serialization.a"
            if [ -f "$BLF" ]; then
                BOOSTLIBS="$BOOSTLIBS/stage"
                echod "$BN: Found stage library [$BLF]. Set BOOSTLIBS=[$BOOSTLIBS]"
            else
                echod "$BN: WARNING: Library [$BLF] NOT FOUND"
            fi
        else
            echod "$BN: Found library [$BLF]"
        fi
    fi

    # check for version.hpp
    if [ -d "$BOOSTPATH" ]; then
        BVF="$BOOSTPATH/boost/version.hpp"
        if [ ! -f "$BVF" ]; then
            # maybe only given the ROOT like /usr
            BVF="$BOOSTPATH/include/boost/version.hpp"
        fi
        show_boost_version $BVF
        if [ "$BOOSTOK" = "1" ]; then
            echo2 "$BN: No BOOST update needed. Need [$BOOST_LIB_VERS], found [$BOOST_VER_FOUND]."
        elif [ "$BOOSTOK" = "0" ]; then
            echo2 "$BN: Need update of installed BOOST, or a new BOOST checkout..."
        else
            echo2 "$BN: WARNING: Need DOWNLOAD, or INSTALL of BOOST!"
        fi
    else
        echo2 "PATH [$BOOSTPATH] DOES NOT EXIST"
        BOOSTOK=3
    fi
}

set_boost_path()
{
    #                 12345678901 = 11th posn
    # command of form BOOSTPATH=<something>
    echo2 "$BN: Setting boost path from [$1]"
    CNT=`expr length $1`
    BOOSTPATH=`echo $1 | cut -b11-$CNT`
    if [ ! -d "$BOOSTPATH" ]; then
        echo2 "$BN: ERROR: path [$BOOSTPATH] does NOT exist!"
        exit 1
    fi
    echo2 "$BN: Set BOOST PATH [$BOOSTPATH]"
}

set_boost_libs()
{
    #                 12345678901 = 11th posn
    # command of form BOOSTLIBS=<something>
    echo2 "$BN: Setting boost library path from [$1]"
    CNT=`expr length $1`
    BOOSTLIBS=`echo $1 | cut -b11-$CNT`
    if [ ! -d "$BOOSTLIBS" ]; then
        echo2 "$BN: ERROR: path [$BOOSTLIBS] does NOT exist!"
        exit 1
    fi
    echo2 "$BN: Set BOOST PATH [$BOOSTLIBS]"
}

deal_with_boost()
{
    if [ "$SGBOOST" = "1" ];then
        # we NEED BOOST
        BOOST_VER_FOUND=""
        check_boost_path
        if [ "$BOOSTOK" = "1" ]; then
            SG_BOOST="--with-boost=$BOOSTPATH"
            BOOSTEXP="export BOOST_ROOT=$BOOSTLIBS"
            echod "$BN: Adding SG_BOOST=$SG_BOOST, and"
            echod "$BN: exporting BOOST_ROOT=$BOOSTLIBS"
        else
            INSTALL_DIR_BOOST="$CBD/$BOOST_RSRC"
            # echo "BOOSTPATH=$BOOSTPATH" >>$MAKEFGCFG
            if [ -z "$BOOST_VER_FOUND" ]; then
                echo2 "$BN: BOOST - Need [$BOOST_LIB_VERS], but NONE found at [$BOOSTPATH]."
            else
                echo2 "$BN: BOOST - Need [$BOOST_LIB_VERS], found only [$BOOST_VER_FOUND] at [$BOOSTPATH]."
            fi
            if [ -d "$INSTALL_DIR_BOOST" ]; then
                echo2 "$BN: Checking this path [$INSTALL_DIR_BOOST] for boost..."
                BOOSTPATH="$INSTALL_DIR_BOOST"
                BOOSTLIBS="$BOOSTPATH"
                check_boost_path
                if [ "$BOOSTOK" = "1" ]; then
                    SG_BOOST="--with-boost=$BOOSTPATH"
                    BOOSTEXP="export BOOST_ROOT=$BOOSTLIBS"
                    echod "$BN: Adding SG_BOOST=$SG_BOOST, and"
                    echod "$BN: exporting BOOST_ROOT=$BOOSTLIBS"
                fi
            else
                echo2 "$BN: New install [$INSTALL_DIR_BOOST] does NOT exist!"
            fi
            if [ "$HELPON" = "1" ] && [ ! "$BOOSTOK" = "1" ]; then
                echo2 "$BN: Would normally need to install BOOST first, but this is a HELP run so..."
                BOOSTPATH="$INSTALL_DIR_BOOST"
                BOOSTLIBS="$BOOSTPATH"
                SG_BOOST="--with-boost=$BOOSTPATH"
                BOOSTEXP="export BOOST_ROOT=$BOOSTLIBS"
            elif [ ! "$BOOSTOK" = "1" ]; then
                echo " "
                echo2 "$BN: If you have the BOOST headers downloaded, then you can use BOOSTPATH=<dir> to use those."
                echo2 " *** OR ***"
                echo2 "$BN: This script can download [$BOOST_FILE], unpack and use this?"
                echo " "
                echo2 "$BN: If you continue, boost would be sited in [$INSTALL_DIR_BOOST]"
                echo2 "$BN: This action would be done NOW, and added to CONF file at end, for next time."
                wait_input "$BN: Continue to do download? Use 'n' if you want to exit, and use BOOSTPATH=<path>"
                echo2 "$BN: Downloading [$BOOST_FILE]... moment... abt 40MB... 10-20 minutes..."
                if [ ! -f "$BOOST_FILE" ]; then
                    wget -nc $BOOST_DOWNLOAD
                fi
                BOOSTPATH="$CBD/$BOOST_RSRC"
                if [ -f "$BOOST_FILE" ]; then
                    echo2 "$BN: Got $BOOST_FILE..."
                    if [ ! -d "$BOOSTPATH" ]; then
                        echo2 "$BN: Unpacking $BOOST_FILE..."
                        tar -xzf $BOOST_FILE
                    fi
                else
                    echo2 "$BN: ERROR: wget from $BOOST_DOWNLOAD FAILED! Aborting..."
                    exit 1
                fi
                if [ -d "$BOOSTPATH" ]; then
                    BOOSTLIBS="$BOOSTPATH"
                    BOOST_VER_FOUND=""
                    check_boost_path
                    if [ -z "$BOOST_VER_FOUND" ]; then
                        echo2 "$BN: BOOST - Need [$BOOST_LIB_VERS], but NONE found at [$BOOSTPATH]."
                    else
                        echo2 "$BN: BOOST - Need [$BOOST_LIB_VERS], found [$BOOST_VER_FOUND] at [$BOOSTPATH]."
                    fi
                    if [ "$BOOSTOK" = "1" ]; then
                        SG_BOOST="--with-boost=$BOOSTPATH"
                        BOOSTEXP="export BOOST_ROOT=$BOOSTLIBS"
                        echo2 "$BN: Adding SG_BOOST=$SG_BOOST, and"
                        echo2 "$BN: exporting BOOST_ROOT=$BOOSTLIBS"
                        wait_for_input "$BN: BOOST appears OK - Continue?"
                        # echo "BOOSTPATH=$BOOSTPATH" >>$MAKEFGCFG
                        # echo "BOOSTLIBS=$BOOSTLIBS" >>$MAKEFGCFG
                    else
                        echo2 "$BN: STILL A BOOST ERROR? can only exit!!!"
                        exit 1
                    fi
                else
                    echo2 "$BN: ERROR: tar -xf $BOOST_FILE FAILED to create [$BOOSTPATH]! Aborting..."
                    exit 1
                fi
            fi
        fi
    else
        echo2 "$BN: Set to EXCLUDE BOOST..."
    fi
}

# FGDATADIR=*) set_fgdata_path $arg ;;
set_fgdata_path()
{
    echo2 "$BN: Setting FG DATA path from [$1]"
    CNT=`expr length $1`
    FGFS_DATA_PATH=`echo $1 | cut -b11-$CNT`
    if [ ! -d "$FGFS_DATA_PATH" ]; then
        echo2 "$BN: ERROR: path [$FGFS_DATA_PATH] does NOT exist!"
        exit 1
    fi
    if [ ! -f "$FGFS_DATA_PATH/version" ]; then
        echo2 "$BN: ERROR: path [$FGFS_DATA_PATH] does NOT contain 'version' file!"
        exit 1
    fi
    echo2 "$BN: Set FG DATA PATH [$FGFS_DATA_PATH]"
}

check_installed_osg()
{
    BAD=0
    if [ ! -d "$1" ]; then
        echo2 "$BN: ERROR: path [$1] does NOT exist!"
        BAD=1
    fi
    if [ ! -d "$1/include/osg" ]; then
        echo2 "$BN: ERROR: path [$1/include/osg] does NOT exist!"
        BAD=1
    fi
    if [ ! -f "$1/include/osg/Version" ]; then
        echo2 "$BN: ERROR: file [$1/include/osg/Version] does NOT exist!"
        BAD=1
    fi
    if [ ! -d "$1/lib" ]; then
        echo2 "$BN: ERROR: path [$1/lib] does NOT exist!"
        BAD=1
    fi
    # could search for some known directory/files
    if [ "$BAD" = "1" ]; then
        echo2 "$BN: Check location given of [$2], and CORRECT!"
        exit 1
    fi
}

# User INPUT = OSGPATH=*) set_installed_osg_path $arg ;;
set_installed_osg_path()
{
    echo2 "$BN: Setting OSG (installed) path from [$1]"
    CNT=`expr length $1`
    INSTALL_DIR_OSG=`echo $1 | cut -b9-$CNT`
    check_installed_osg $INSTALL_DIR_OSG $1
    # OSG looks valid - maybe should turn off OSGADD if not base path
    is_same_base_dir $INSTALL_DIR_OSG
    if [ "$BASE_IS_SAME" = "0" ]; then
        echo2 "$BN: Set OSG (installed) PATH [$INSTALL_DIR_OSG], but NO update allowed"
        OSGADD=0
    else
        echo2 "$BN: Set OSG (installed) PATH [$INSTALL_DIR_OSG]"
    fi
    OSG_INSTALL_OK=1
    OSGTRUNK=0
}

check_installed_plib()
{
    BAD=0
    if [ ! -d "$1" ]; then
        echo2 "$BN: ERROR: path [$1] does NOT exist!"
        BAD=1
    fi
    if [ ! -d "$1/include/plib" ]; then
        echo2 "$BN: ERROR: path [$1/include/plib] does NOT exist!"
        BAD=1
    fi
    if [ ! -d "$1/lib" ]; then
        echo2 "$BN: ERROR: path [$1/lib] does NOT exist!"
        BAD=1
    fi
    # could seach for some known files also
    if [ "$BAD" = "1" ]; then
        echo2 "$BN: Check location given of [$2], and CORRECT!"
        exit 1
    fi
}


# User INPUT = PLIBPATH=*) set_installed_plib_path $arg ;;
set_installed_plib_path()
{
    echo2 "$BN: Setting PLIB (installed) path from [$1]"
    CNT=`expr length $1`
    INSTALL_DIR_PLIB=`echo $1 | cut -b10-$CNT`
    check_installed_plib $INSTALL_DIR_PLIB $1
    # PLIB looks valid - mabe should turn off PLIBADD if not base path
    echo2 "$BN: Set PLIB (installed) PATH [$INSTALL_DIR_PLIB]"
    PLIB_INSTALL_OK=1
}

# Read from CONF file
set_plib_paths()
{
    echod "$BN: Setting PLIB PATH..."
    # INSTALL_DIR_PLIB=/home/geoff/fg/????/install/plib
    # PLIB_SOURCE_DIR=plib
    if [ "$NO_CONF_FILE" = "1" ]; then
        echo2 "$BN: Assume no CONF file [$MAKEFGCFG] available to set PLIB!"
    elif [ -f "$MAKEFGCFG" ]; then
        # search for PLIB installation
        if [ -z "$INSTALL_DIR_PLIB" ]; then
            SRCH="INSTALL_DIR_PLIB"
            LEN=`expr length $SRCH`
            LEN=`expr $LEN + 2`
            TMP=`grep -v '#' $MAKEFGCFG | grep $SRCH`
            if [ "$?" = "0" ] && [ ! -z "$TMP" ]; then
                CNT=`expr length $TMP`
                if [ "$CNT" -gt "$LEN" ]; then
                    INSTALL_DIR_PLIB=`echo $TMP | cut -b$LEN-$CNT`
                    if [ ! -d "$INSTALL_DIR_PLIB" ]; then
                        echo2 "$BN: CONFIGURATION ERROR in file $MAKEFGCFG"
                        echo2 "$BN: Extracted [$TMP], but [$INSTALL_DIR_PLIB] does NOT EXIST"
                        echo2 "$BN: Fix or delete the CONF file, and try again!"
                        exit 1
                    fi
                    check_installed_plib $INSTALL_DIR_PLIB $TMP
                    PLIB_INSTALL_OK=1
                    echo2 "$BN: From CONF file, set $SRCH=$INSTALL_DIR_PLIB."
                    FROM_CONF_FILE=`expr $FROM_CONF_FILE + 1`
                else
                    echo2 "$BN: CONFIGURATION ERROR in CONF file [$MAKEFGCFG]! Got [$TMP] for $SRCH?"
                    echo2 "$BN: Fix or delete the CONF file, and try again!"
                    exit 1
                fi
            else
                echo2 "$BN: No '$SRCH' in CONF file [$MAKEFGCFG]!"
            fi
        fi
        # search for PLIB SOURCE directory - no need, I htink
    else
        echo2 "$BN: No CONF file [$MAKEFGCFG] available!"
    fi
}


set_osg_install_dir()
{
    echod "$BN: Setting INSTALL_DIR_OSG..."
    if [ "$NO_CONF_FILE" = "1" ]; then
        echo2 "$BN: Assume no CONF file [$MAKEFGCFG] available for OSG!"
    elif [ -f "$MAKEFGCFG" ]; then
        # search for OSG installation
        SRCH="INSTALL_DIR_OSG"
        LEN=`expr length $SRCH`
        LEN=`expr $LEN + 2`
        TMP=`grep -v '#' $MAKEFGCFG | grep $SRCH`
        if [ "$?" = "0" ] && [ ! -z "$TMP" ]; then
            CNT=`expr length $TMP`
            if [ "$CNT" -gt "$LEN" ]; then
                INSTALL_DIR_OSG=`echo $TMP | cut -b$LEN-$CNT`
                if [ ! -d "$INSTALL_DIR_OSG" ]; then
                    echo2 "$BN: CONFIGURATION ERROR in file $MAKEFGCFG"
                    echo2 "$BN: Extracted [$TMP], but [$INSTALL_DIR_OSG] does NOT EXIST"
                    echo2 "$BN: Fix or delete the CONF file, and try again!"
                    exit 1
                fi
                echo2 "$BN: From CONF file, set $SRCH=$INSTALL_DIR_OSG."
                FROM_CONF_FILE=`expr $FROM_CONF_FILE + 1`
                OSG_INSTALL_OK=1
            else
                echo2 "$BN: CONFIGURATION ERROR in CONF file [$MAKEFGCFG]! Got [$TMP] for $SRCH?"
                echo2 "$BN: Fix or delete the CONF file, and try again!"
                exit 1
            fi
        else
            echo2 "$BN: No '$SRCH' in CONF file [$MAKEFGCFG]!"
        fi
        # search for OSG SOURCE directory - NO, no need..
    else
        echo2 "$BN: No CONF file [$MAKEFGCFG] available!"
    fi
}


set_fgfs_data_path()
{
    echod "$BN: Setting FG DATA PATH..."
    if [ "$NO_CONF_FILE" = "1" ]; then
        echo2 "$BN: Assumed no CONF file [$MAKEFGCFG] available for fgfs!"
    elif [ -f "$MAKEFGCFG" ]; then
        if [ -z "$FGFS_DATA_DIR" ]; then
            SRCH="FGFS_DATA_DIR"
            LEN=`expr length $SRCH`
            LEN=`expr $LEN + 2`
            TMP=`grep -v '#' $MAKEFGCFG | grep $SRCH`
            if [ "$?" = "0" ] && [ ! -z "$TMP" ]; then
                CNT=`expr length $TMP`
                if [ "$CNT" -gt "$LEN" ]; then
                    FGFS_DATA_DIR=`echo $TMP | cut -b$LEN-$CNT`
                    if [ ! -d "$FGFS_DATA_DIR" ]; then
                        echo2 "$BN: CONFIGURATION ERROR in file $MAKEFGCFG"
                        echo2 "$BN: Extracted [$TMP], but [$FGFS_DATA_DIR] does NOT EXIST"
                        echo2 "$BN: Fix or delete the CONF file, and try again!"
                        exit 1
                    fi
                    echo2 "$BN: From CONF file, set $SRCH=$FGFS_DATA_DIR."
                    FROM_CONF_FILE=`expr $FROM_CONF_FILE + 1`
                else
                    echo2 "$BN: CONFIGURATION ERROR in CONF file [$MAKEFGCFG]! Got [$TMP] for $SRCH?"
                    echo2 "$BN: Fix or delete the CONF file, and try again!"
                    exit 1
                fi
            else
                echo2 "$BN: No '$SRCH' in CONF file [$MAKEFGCFG]!"
            fi
        fi
        # search for SOURCE directory? - NO NEED
    else
        echo2 "$BN: No CONF file [$MAKEFGCFG] available!"
    fi
}

check_fgfs_install_path()
{
    echod "$BN: Checking FG INSTALL PATH $1"
    if [ "$NO_CONF_FILE" = "1" ]; then
        echod "$BN: Assume no CONF file!"
    elif [ -f "$MAKEFGCFG" ]; then
        SRCH="INSTALL_DIR_FGFS"
        LEN=`expr length $SRCH`
        LEN=`expr $LEN + 2`
        TMP=`grep -v '#' $MAKEFGCFG | grep $SRCH`
        if [ "$?" = "0" ] && [ ! -z "$TMP" ]; then
            CNT=`expr length $TMP`
            if [ "$CNT" -gt "$LEN" ]; then
                TMP=`echo $TMP | cut -b$LEN-$CNT`
                if [ -d "$TMP" ]; then
                    if [ ! "$TMP" = "$1" ]; then
                        echo ""
                        echo2 "$BN: WARNING: This is a NEW INSTALL PATH for fgfs"
                        echo2 "$BN: Last run was [$TMP]"
                        echo2 "$BN: Now path is  [$1]"
                        echo2 "$BN: *** BE VERY SURE THIS IS YOUR INTENTION! ***"
                        echo ""
                    fi
                fi
            fi
        fi
    fi
}

set_directories()
{
    echod "$BN: Processing set_directories()..."
    # setup the commands
    SUB_INSTALL_DIR=install
    INSTALL_DIR=$CBD/$SUB_INSTALL_DIR

    # PLIB - Use stable 1.8.5, or devel trunk 1.8.6++
    if [ -z "$PLIB_SOURCE_DIR" ] || [ -z "$INSTALL_DIR_PLIB" ]; then
        set_plib_paths
    fi
    if [ -z "$PLIB_SOURCE_DIR" ]; then
        if [ "$PLIBTRUNK" = "1" ] ; then
            PLIB_SOURCE_DIR=plib
        else
            PLIB_SOURCE_DIR=plib-1.8.5
        fi
        if [ -z "$INSTALL_DIR_PLIB" ]; then
            INSTALL_DIR_PLIB=$INSTALL_DIR/$PLIB_SOURCE_DIR
        fi
    fi
    if [ -z "$INSTALL_DIR_PLIB" ]; then
        INSTALL_DIR_PLIB=$INSTALL_DIR/$PLIB_SOURCE_DIR
    fi
    is_same_base_dir $INSTALL_DIR_PLIB
    if [ "$BASE_IS_SAME" = "0" ]; then
        echo2 "$BN: Using NON base path PLIB... No PLIB UPDATE/BUILD possible..."
        PLIBADD=0
    fi
    # now ready to SET other stuff
    PLIB_CONF="--prefix=$INSTALL_DIR_PLIB --exec-prefix=$INSTALL_DIR_PLIB"
    # 2009-02-05 - Add building examples
    PLIB_EXAMPLE_DIR=$PLIB_SOURCE_DIR/examples
    #PLIB_CONF_EX="$PLIB_CONF --with-plib=$INSTALL_DIR_PLIB"
    #PLIB_CONF_EX="$PLIB_CONF"
    PLIB_CONF_EX="$PLIB_CONF LDFLAGS=-L$INSTALL_DIR_PLIB/lib CPPFLAGS=-I$INSTALL_DIR_PLIB/include"

    # OpenSceneGraph
    if [ "$OSGTRUNK" = "1" ]; then
        OSG_SOURCE_DIR=OSGtrunk
        OSG_SVN="http://www.openscenegraph.org/svn/osg/OpenSceneGraph/trunk"
        OSG_SVN_DATA="http://www.openscenegraph.org/svn/osg/OpenSceneGraph-Data/trunk"
        OSG_DATA_DIR="OpenSceneGraph-Data-trunk"
    else
        # release May, 2009
        # OSG_SOURCE_DIR=OSG281
        # OSG_SVN="http://www.openscenegraph.org/svn/osg/OpenSceneGraph/tags/OpenSceneGraph-2.8.1"
        # release  2.8.2 released July 2009
        if [ -z "$INSTALL_DIR_OSG" ]; then
            set_osg_install_dir
        fi
        # if we GOT NO INSTALL directory
        if [ -z "$INSTALL_DIR_OSG" ]; then
            if [ "$OSGUSEREL" = "1" ]; then
                OSG_SOURCE_DIR=OSG283
                OSG_SVN="http://www.openscenegraph.org/svn/osg/OpenSceneGraph/tags/OpenSceneGraph-2.8.3"
                #OSG_SOURCE_DIR=OSG282
                #OSG_SVN="http://www.openscenegraph.org/svn/osg/OpenSceneGraph/tags/OpenSceneGraph-2.8.2"
            else
                # use the developers release 10 Dec 2009
                OSG_SOURCE_DIR=OSG296
                OSG_SVN="http://www.openscenegraph.org/svn/osg/OpenSceneGraph/tags/OpenSceneGraph-2.9.6"
            fi
            OSG_SVN_DATA="http://www.openscenegraph.org/svn/osg/OpenSceneGraph-Data/tags/OpenSceneGraph-Data-2.8.0"
            OSG_DATA_DIR="OpenSceneGraph-Data-OSG28"
            OSG_FIX=''
            if [ "$OSGFIX" = "1" ]; then
                OSG_FIX="-D LIB_POSTFIX="
            fi
            if [ "$OSGTOSTD" = "1" ]; then
                OSG_CMAKE="-D CMAKE_BUILD_TYPE=Release -D CMAKE_CXX_FLAGS=-O3 -D CMAKE_C_FLAGS=-O3 $OSG_FIX"
            else
                INSTALL_DIR_OSG=$INSTALL_DIR/$OSG_SOURCE_DIR
                OSG_CMAKE="-D CMAKE_BUILD_TYPE=Release -D CMAKE_CXX_FLAGS=-O3 -D CMAKE_C_FLAGS=-O3 $OSG_FIX -D CMAKE_INSTALL_PREFIX:PATH=$INSTALL_DIR_OSG"
            fi
            if [ "$OSGEXAM" = "1" ]; then
                # above set OSG_SVN_DATA and OSG_DATA_DIR to also download the examples data
                OSG_MAKE="$OSG_MAKE -DBUILD_OSG_EXAMPLES=1"
            fi
        else
            is_same_base_dir $INSTALL_DIR_OSG
            if [ "$BASE_IS_SAME" = "0" ]; then
                # DIFFERENT BASE DIRECTORY, so check option
                OSGEXIT=0
                if [ "$OSGCO" = "1" ]; then
                    echo2 "$BN: Conflict in options! OSGCO requested for another directory..."
                    OSGEXIT=1
                fi
                if [ "$OSGUPD" = "1" ]; then
                    echo2 "$BN: Conflict in options! OSGUPD requested for another directory..."
                    OSGEXIT=1
                fi
                if [ "$OSGCLEAN" = "1" ]; then
                    echo2 "$BN: Conflict in options! OSGCLEAN requested for another directory..."
                    OSGEXIT=1
                fi
                if [ "$OSGCMAKE" = "1" ]; then
                    echo2 "$BN: Conflict in options! OSGCMAKE requested for another directory..."
                    OSGEXIT=1
                fi
                if [ "$OSGEXAM" = "1" ]; then
                    echo2 "$BN: Conflict in options! OSGEXAM requested for another directory..."
                    OSGEXIT=1
                fi
                if [ "$OSGTRUNK" = "1" ]; then
                    echo2 "$BN: Conflict in options! OSGTRUNK requested for another directory..."
                    OSGEXIT=1
                fi
                if [ "$OSGEXIT" = "1" ]; then
                    echo2 "$BN: Value read from CONF [OSG_SOURCE_DIR=$OSG_SOURCE_DIR] conflicts with requested option."
                    echo2 "$BN: Delete or fix the CONF file, $MAKEFGCFG, or use NO_CONF option... Aborting..."
                    exit 1
                fi
                OSGNOUPD=1     # AVOID updating OSG - takes a lot of time
                OSGTOSTD=0
                OSGADD=0
                OSG_INSTALL_OK=1
            fi
        fi
    fi

    # SIMGEAR
    # Alternatives: 1 ADDSGCS = git, else 2 cvs SG
    # related to TGCVS=1 # try TG CVS, with CVS SG so ADDSGCS should be OFF
    # 20091118 - try to use boost in another location
    SG_BOOST=""
    BOOSTEXP=""
    if [ "$ADDSGCS" = "1" ]; then
    SIMGEAR_SOURCE_DIR=simgear-cs
    SIMGEAR_INSTALL_DIR=simgear-cs
    else
    SIMGEAR_SOURCE_DIR=simgear/source
    SIMGEAR_INSTALL_DIR=simgear
    fi

    deal_with_boost

INSTALL_DIR_SIMGEAR=$INSTALL_DIR/$SIMGEAR_INSTALL_DIR
if [ "$OSGTOSTD" = "1" ]; then
SG_CONF="--prefix=$INSTALL_DIR_SIMGEAR --exec-prefix=$INSTALL_DIR_SIMGEAR --with-plib=$INSTALL_DIR_PLIB --with-jpeg-factory $SG_BOOST"
else
SG_CONF="--prefix=$INSTALL_DIR_SIMGEAR --exec-prefix=$INSTALL_DIR_SIMGEAR --with-osg=$INSTALL_DIR_OSG --with-plib=$INSTALL_DIR_PLIB --with-jpeg-factory $SG_BOOST"
fi

# FGFS
FGFS_SOURCE_DIR=fgfs
INSTALL_DIR_FGFS=$INSTALL_DIR/$FGFS_SOURCE_DIR
if [ "$OSGTOSTD" = "1" ]; then
FG_CONF="--prefix=$INSTALL_DIR_FGFS --exec-prefix=$INSTALL_DIR_FGFS --with-simgear=$INSTALL_DIR_SIMGEAR --with-plib=$INSTALL_DIR_PLIB --enable-osgviewer $SG_BOOST"
else
FG_CONF="--prefix=$INSTALL_DIR_FGFS --exec-prefix=$INSTALL_DIR_FGFS --with-osg=$INSTALL_DIR_OSG --with-simgear=$INSTALL_DIR_SIMGEAR --with-plib=$INSTALL_DIR_PLIB $SG_BOOST"
# --enable-osgviewer
fi
# FGFS DATA - is in the root build/fgfs/data
if [ -z "$FGFS_DATA_DIR" ]; then
    set_fgfs_data_path
    if [ -z "$FGFS_DATA_DIR" ]; then
        FGFS_DATA_DIR="$CBD/fgfs/data"
    fi
fi
is_same_base_dir $FGFS_DATA_DIR
if [ "$BASE_IS_SAME" = "0" ]; then
    echo2 "$BN: FG DATA DIRECTORY is NOT this base... some restrictions apply, like NO UPDATE possible..."
    FGADDDATA=0
fi

# FGRUN
FGRUN_INSTALL_DIR=fgrun
FGRUN_SOURCE_DIR=fgrun/fgrun
#FGRUN_SOURCE_DIR=fgrun/trunk/fgrun
INSTALL_DIR_FGRUN=$INSTALL_DIR/$FGRUN_INSTALL_DIR
if [ "$OSGTOSTD" = "1" ]; then
FGRUN_CONF="--prefix=$INSTALL_DIR_FGRUN --exec-prefix=$INSTALL_DIR_FGRUN --with-osg --with-simgear=$INSTALL_DIR_SIMGEAR --with-plib=$INSTALL_DIR_PLIB --with-plib-includes=$INSTALL_DIR_PLIB/include --with-plib-libraries=$INSTALL_DIR_PLIB/lib"
else
FGRUN_CONF="--prefix=$INSTALL_DIR_FGRUN --exec-prefix=$INSTALL_DIR_FGRUN --with-osg=$INSTALL_DIR_OSG --with-simgear=$INSTALL_DIR_SIMGEAR --with-plib=$INSTALL_DIR_PLIB --with-plib-includes=$INSTALL_DIR_PLIB/include --with-plib-libraries=$INSTALL_DIR_PLIB/lib"
fi

# Atlas
ATLAS_SOURCE_DIR=Atlas
INSTALL_DIR_ATLAS=$INSTALL_DIR/$ATLAS_SOURCE_DIR
ATLAS_CONF="--prefix=$INSTALL_DIR_ATLAS --exec-prefix=$INSTALL_DIR_ATLAS --with-osg=$INSTALL_DIR_OSG --with-simgear=$INSTALL_DIR_SIMGEAR --with-plib=$INSTALL_DIR_PLIB --with-fgbase=$CBD/$FGFS_SOURCE_DIR/data"

# terragear
if [ "$TGCVS" = "1" ]; then
# BUT to do this you MUST use an OLDER SG CVS
TG_SOURCE_DIR=terragear
TG_INSTALL_DIR=terragear
INSTALL_DIR_TG=$INSTALL_DIR/$TG_INSTALL_DIR
else
TG_SOURCE_DIR=terragear-cs
TG_INSTALL_DIR=$HOME
INSTALL_DIR_TG=$HOME
fi
TG_CONF="--prefix=$INSTALL_DIR_TG --exec-prefix=$INSTALL_DIR_TG --with-osg=$INSTALL_DIR_OSG --with-simgear=$INSTALL_DIR_SIMGEAR --with-plib=$INSTALL_DIR_PLIB" #--with-plib-includes=$INSTALL_DIR_PLIB/include --with-plib-libraries=$INSTALL_DIR_PLIB/lib"
if [ -z $LD_LIBRARY_PATH ]; then
TG_POPT="LD_LIBRARY_PATH=$INSTALL_DIR_PLIB/lib/"
else
TG_POPT="LD_LIBRARY_PATH=$INSTALL_DIR_PLIB/lib/:$LD_LIBRARY_PATH"
fi

# JUST A DEBUG OUTPUT
if [ "$ADDDEBUG" = "1" ]; then
   show_variables_for_debug
fi
    echod "$BN: Done set_directories()...INSTALL_DIR_OSG=$INSTALL_DIR_OSG"
}

write_conf_file()
{
    if [ ! -z $1 ] && [ ! -f $1 ]; then
        # UPDATE the conf file - KEEP FIRST LINE CONSTANT SIZE, for easy parsing...
        #     1234567890123456789012345678901234567890
        echo "# makefg VERSION=$SCVERSION DATE=$SCDATE Current time $(date)" >$1
        echo "BOOSTPATH=$BOOSTPATH" >>$1
        echo "BOOSTLIBS=$BOOSTLIBS" >>$1
        if [ ! -z "$INSTALL_DIR_OSG" ]; then
            echo "INSTALL_DIR_OSG=$INSTALL_DIR_OSG" >>$1
        fi
        if [ ! -z "$OSG_SOURCE_DIR" ]; then
            echo "OSG_SOURCE_DIR=$OSG_SOURCE_DIR" >>$1
        fi
        if [ ! -z "$INSTALL_DIR_PLIB" ]; then
            echo "INSTALL_DIR_PLIB=$INSTALL_DIR_PLIB" >>$1
        fi
        if [ ! -z "$PLIB_SOURCE_DIR" ]; then
            echo "PLIB_SOURCE_DIR=$PLIB_SOURCE_DIR" >>$1
        fi
        if [ ! -z "$FGFS_DATA_DIR" ]; then
            echo "FGFS_DATA_DIR=$FGFS_DATA_DIR" >>$1
        fi
        if [ ! -z "$FGFS_SOURCE_DIR" ]; then
            echo "FGFS_SOURCE_DIR=$FGFS_SOURCE_DIR" >>$1
        fi
        if [ ! -z "$INSTALL_DIR_FGFS" ]; then
            echo "INSTALL_DIR_FGFS=$INSTALL_DIR_FGFS" >>$1
        fi
    fi
}

update_conf_file()
{
    echo2 "$BN: Updating file $MAKEFGCFG..."
    if [ -f "$LOCALFGCFG" ]; then
        rm -f "$LOCALFGCFG"
    fi
    write_conf_file $LOCALFGCFG
    if [ -f "$MAKEFGCFG" ]; then
        if [ "$NO_CONF_FILE" = "1" ]; then
            echo2 "$BN: All actions have been completed except this CONF file update, but"
            echo2 "$BN: you have chosen the NO_CONF action, to perhaps no need to write a CONF file."
            wait_input "$BN: Continue, or exit now?"
        fi
        rm -f "$MAKEFGCFG"
    fi
    write_conf_file $MAKEFGCFG
}

show_variables_for_debug()
{
   echo2 "ADDDEBUG OUTPUT... CBD=$CBD"
   echo2 "SUB_INSTALL_DIR=$SUB_INSTALL_DIR"
   echo2 "INSTALL_DIR=$INSTALL_DIR"
   # PLIB
   echo2 "== PLIB - PLIBADD=$PLIBADD"
   echo2 "PLIB_SOURCE_DIR=$PLIB_SOURCE_DIR"
   echo2 "INSTALL_DIR_PLIB=$INSTALL_DIR_PLIB"
   echo2 "PLIB_CONF=$PLIB_CONF"
   # 2009-02-05 - Add building examples
   echo2 "PLIB_EXAMPLE_DIR=$PLIB_EXAMPLE_DIR"
   echo2 "PLIB_CONF_EX=$PLIB_CONF_EX"
   # OSG
   echo2 "== OpenSceneGraph - OSGADD=$OSGADD"
   echo2 "OSG_SOURCE_DIR=$OSG_SOURCE_DIR"
   echo2 "OSG_SVN=$OSG_SVN"
   echo2 "OSG_CMAKE=$OSG_CMAKE"
   echo2 "OSG_DATA_DIR=$OSG_DATA_DIR"
   echo2 "OSG_SVN_DATA=$OSG_SVN_DATA"
   echo2 "INSTALL_DIR_OSG=$INSTALL_DIR_OSG"
   # SG
   echo2 "== SIMGEAR - SGADD=$SGADD - SGBOOST=$SGBOOST"
   echo2 "SIMGEAR_SOURCE_DIR=$SIMGEAR_SOURCE_DIR"
   echo2 "SIMGEAR_INSTALL_DIR=$SIMGEAR_INSTALL_DIR"
   echo2 "INSTALL_DIR_SIMGEAR=$INSTALL_DIR_SIMGEAR"
   echo2 "SG_CONF=$SG_CONF"
   # FG
   echo2 "== FGFS - FGADD=$FGADD"
   echo2 "FGFS_SOURCE_DIR=$FGFS_SOURCE_DIR"
   echo2 "INSTALL_DIR_FGFS=$INSTALL_DIR_FGFS"
   echo2 "FG_CONF=$FG_CONF"
   # FGRUN
   echo2 "== FGRUN = FGRUNADD=$FGRUNADD"
   echo2 "FGRUN_INSTALL_DIR=$FGRUN_INSTALL_DIR"
   echo2 "FGRUN_SOURCE_DIR=$FGRUN_SOURCE_DIR"
   echo2 "INSTALL_DIR_FGRUN=$INSTALL_DIR_FGRUN"
   echo2 "FGRUN_CONF=$FGRUN_CONF"
   # Atlas
   echo2 "== Atlas - ATLASADD=$ATLASADD"
   echo2 "ATLAS_SOURCE_DIR=$ATLAS_SOURCE_DIR"
   echo2 "INSTALL_DIR_ATLAS=$INSTALL_DIR_ATLAS"
   echo2 "ATLAS_CONF=$ATLAS_CONF"
   # TG
   echo2 "== terragear - TGADD=$TGADD"
   echo2 "TG_SOURCE_DIR=$TG_SOURCE_DIR"
   echo2 "TG_INSTALL_DIR=$TG_INSTALL_DIR"
   echo2 "INSTALL_DIR_TG=$INSTALL_DIR_TG"
   echo2 "TG_CONF=$TG_CONF"
   echo2 "TG_POPT=$TG_POPT"
   # FGDATA
   echo2 "== FG DATA - FGADDDATA=$FGADDDATA"
}

set_runtime_dir()
{
    #echo $0 | sed "s/$BN//"
    RTPATH=`echo $0 | sed "s/\/$BN//"`
    MAKEFGCFG="$RTPATH/makefg-$SCVERSION.conf"
}

add_2_valid()
{
   if [ "$VALID" = "" ] ; then
      VALID=$1
   else
      for val in $VALID; do
         if [ "$val" = "$1" ]; then
            #echo2 "GOT [$1] ok"
            return
         fi
      done
      VALID="$VALID $1"
    fi
    #echo2 "CMD [$1] ok"
}

out_help_list()
{
   echo2 "For PLIB:   PLIB PLIBUPD PLIBAUTO PLIBCONF PLIBCLEAN PLIBDEBUG NOPLIB PLIBPATH=<path>"
   echo2 "For PLIBEX: PLIBEX (=also build examples) PLIBCONFEX PLIBAUTOEX"
   echo2 "For OSG:    OSG OSGEXAM OSGUPD OSGCMAKE OSGCLEAN OSGNOUPD OSGTRUNK NOOSG OSGNOFIX OSGNOLINK OSGUSEDEV OSGPATH"
   echo2 "For SG:     SG SIMGEAR SGAUTO SGUPD SGNOUPD SGCLEAN SGCONF SGDEBUG SGBOOST SGNOBOOST"
   if [ "$FGADD" = "1" ]; then        # ON for flighgear - OFF for terragear
      echo2 "For FG:     FG FGFS FGUPD FGNOUPD FGAUTO FGCLEAN FGCONF FGDEBUG - FGDATA FGUPDDATA FGNODATA FGDATADIR=<path>"
      echo2 "For BOOST:  BOOSTPATH=<dir> BOOSTLIBS=<dir>"
   fi
   #if [ "$FGRUN" = "1" ]; then
      echo2 "For FGRUN:  FGRUN FGRUNUPD FGRUNAUTO FGRUNCLEAN FGRUNCONF FGRUNDEBUG FGRUNNO NOFGRUN"
      echo2 "For ATLAS:  ATLAS ATLASUPD ATLASAUTO ATLASCLEAN ATLASCONF ATLASDEBUG NOATLAS"
   #fi
   if [ "$TGADD" = "1" ]; then
      echo2 "For TG:     TG TGUPD TGAUTO TGCLEAN TGCONF TGDEBUG"
      echo2 "FG, FGRUN, and ATLAS are OFF by default, AND SG and TG are from the git repository."
      echo2 "and note FG will not build against SG git. Use 'makefg' for cvs respository."
   else
      echo2 "ATLAS and TG are OFF by default. Use ATLAS or ATLASADD to add Atlas,"
      echo2 "and note TG will not build against SG cvs. Use 'maketg' for git respository."
   fi
   echo2 "In all cases, the FG base data will be installed, since it is required for FG and TG!"
   echo2 "Others:"
   echo2 "    DOUPD       Force update of ALL tools, sources. Checkout always done if not existing."
   echo2 "    DOUPD2      Do update of sources after OSG."
   echo2 "    TOOLUPD     Force apt-get tool and package update"
   echo2 "    TOOLUPDONLY Only update tools, and exit"
   echo2 "    NO_TOOL_UPD Ensure NO tools or package update done"
   echo2 "    ADDDEBUG    More output to the (incrementing) LOG file"
   echo2 "    DELSCRIPTS  Delete, and re-write run scripts. This can happen automatically for certain version changes."
   echo2 "    NO_CONF     Assume NO CONF file available."
   echo2 "    NOPAUSE     Do not pause (frequently), and wait for input."
   echo2 "    HELP        Output help, and exit."
   echo2 "Installation:"
   if [ "$TGADD" = "1" ]; then
      echo2 "The TG suite of tools, about 25, will be installed in your \$HOME/bin directory."
      echo2 "To be able to run these tools globally, add the following to \$HOME/.bashrc (if in bash)"
      echo2 "export PATH=\$PATH:\$HOME/bin. Or put this in say \$HOME/.bash_aliases, if this file used."
   else
      echo2 "The main 'fgfs' application will be installed in $(pwd)/install/$FGFS_SOURCE_DIR."
      echo2 "but run_fgfs.sh, and run_fgrun.sh are written to $(pwd) for a quick start."
   fi
   echo2 ""
   echo2 "It is difficult to describe the interaction of many parameters, but suffice it to say"
   echo2 "run without parameters builds the default set PLIB, OSG, SG, FG and FGRUN, if 'boost' found, but"
   echo2 "some parameters, like say 'FG' cause only that component to be addressed."
   echo2 "The file $MAKEFGCFG can contain parameters previously used,"
   echo2 "and will become the 'default' on the next run. This is especially helpful for parameters like"
   echo2 "BOOSTPATH, FGDATADIR, PLIBPATH, ... to use the previous <path> given, or used."
}

show_new_directory()
{
   if [ "$FGADDDATA" = "1" ]; then
       echo2 "A full build of 'all' consumes about 6-8GB of space, with FG data being about 5GB of that..."
       echo " "
   fi
   echo2 "        QUESTION? QUESTION? QUESTION? QUESTION?"
   echo2 " *** THIS APPEARS TO BE A NEW INSTALLATION in [$CBD] ***"
   echo " "
}

show_help()
{
   HELPON=1 # ensure NO EXIT due to problems, since this HELP will exit
   set_directories   # establish all the directories being used
   echo2 " "
   echo2 "########################################################################################"
   echo2 "# $BNAME - a script for downloading, making, and installing $PROJECT - $SCDATE $SCVERSION"
   echo2 "########################################################################################"
   echo2 " "
   echo2 "Help for running makefg script, version: $SCVERSION - geoff mclane - email: report [at] geoffair [dot] info"
   echo2 "The script pauses for input of a 'y' frequently. Any other input, or none, and it will exit."
   echo2 "If you want to proceeed WITHOUT any pause, then use NOPAUSE in the command line."
   echo2 " "
   echo2 "Run without any command, or 'DOUPD' will do checkouts, (or updates) from the repositories."
   echo2 "The tools can be updated by 'TOOLUPD', or 'TOOLUPDONLY' to exit after this 'tool' update."
   echo2 "'DOUPD' will also update the tools, and update each of the other components, if the checkout already done."
   echo2 "But 'NO_TOOL_UPD' will ensure NO tool or package update done."
   echo2 " "
   if [ "$ADDDEBUG" = "0" ]; then
      echo2 "NOTE: The command ADDDEBUG before the help request would give MORE information."
   fi
   echo2 "Full set of valid commands is:"
   out_help_list
   echo2 " "
   if [ ! -d $INSTDIR ]; then
      show_new_directory
   fi
   if [ "$ADDDEBUG" = "1" ]; then
       echo "$BN: All the above information is in the file [$OUTFILE] for review..."
   else
       rm -f $OUTFILE
   fi
   exit 0
}

# NOTE: Add new commands, as desired
check_cmds()
{
   FAILED=0
   INVALID=''
   # echo2 "$BNAME: Running in $LSB_REL"
   if [ "$#" -gt "0" ]; then
      echo2 "Checking $# arguments ... [$*]"
   fi
   for arg in $@
   do
      case $arg in
         PLIB) add_2_valid $arg ;;
         PLIBUPD) add_2_valid "PLIB"; PLIBUPD=1 ;;
         PLIBAUTO) add_2_valid "PLIB"; PLIBAUTO=1 ;;
         PLIBCONF) add_2_valid "PLIB"; PLIBCONF=1 ;;
         PLIBCLEAN) add_2_valid "PLIB"; PLIBCLEAN=1 ;;
         PLIBEX) add_2_valid "PLIB"; PLIBEX=1 ;;
         PLIBCONFEX) add_2_valid "PLIB"; PLIBEX=1; PLIBCONFEX=1 ;;
         PLIBAUTOEX) add_2_valid "PLIB"; PLIBEX=1; PLIBCONFEX=1; PLIBAUTOEX=1 ;;
         PLIBDEBUG) add_2_valid "PLIB"; PLIBDEBUG=1 ;;
         PLIBPATH=*) set_installed_plib_path $arg ;;
         NOPLIB) PLIBADD=0 ;;
         OSG) add_2_valid $arg ;;
         OSGTRUNK) add_2_valid "OSG"; OSGTRUNK=1 ;;
         OSGEXAM) add_2_valid "OSG"; OSGEXAM=1 ;;
         OSGUPD) add_2_valid "OSG"; OSGUPD=1 ;;
         OSGCLEAN) add_2_valid "OSG"; OSGCLEAN=1 ;;
         OSGCMAKE) add_2_valid "OSG"; OSGCMAKE=1 ;;
         OSGNOUPD) OSGNOUPD=1 ;;
         NOOSG) OSGADD=0 ;;
         OSGNOFIX) OSGFIX=0 ;;
         OSGNOLINK) OSGLINK=0 ;;
         OSGUSEDEV) OSGUSEREL=0 ;;
         OSGPATH=*) set_installed_osg_path $arg ;;
         SG) add_2_valid $arg ;;
         SIMGEAR) add_2_valid "SG" ;;
         SGAUTO) add_2_valid "SG"; SGAUTO=1 ;;
         SGUPD) add_2_valid "SG"; SGUPD=1 ;;
         SGCLEAN) add_2_valid "SG"; SGCLEAN=1 ;;
         SGCONF) add_2_valid "SG"; SGCONF=1 ;;
         SGDEBUG) add_2_valid "SG"; SGCONF=1; SGDEBUG=1 ;;
         SGBOOST) SGBOOST=1 ;;
         SGNOBOOST) SGBOOST=0 ;;
         NOSG) SGADD=0 ;;
         SGNOUPD) SGUPD=0 ;;
         FG) add_2_valid $arg ;;
         FGFS) add_2_valid $arg ;;
         FGDATA) add_2_valid $arg ;;
         FGUPD) add_2_valid "FG"; FGUPD=1 ;;
         FGNOUPD) FGUPD=0 ;;
         FGAUTO) add_2_valid "FG"; FGAUTO=1 ;;
         FGCLEAN) add_2_valid "FG"; FGCLEAN=1 ;;
         FGCONF) add_2_valid "FG"; FGCONF=1 ;;
         FGDEBUG) add_2_valid "FG"; FGDEBUG=1 ;;
         FGUPDDATA) FGUPDDATA=1; FGADDDATA=1 ;;
         FGNODATA) FGUPDDATA=0; FGADDDATA=0 ;;
         FGDATADIR=*) set_fgdata_path $arg ;;
         FGRUN) add_2_valid $arg ;;
         FGRUNUPD) add_2_valid "FGRUN"; FGRUNUPD=1 ;;
         FGRUNAUTO) add_2_valid "FGRUN"; FGRUNAUTO=1 ;;
         FGRUNCONF) add_2_valid "FGRUN"; FGRUNCONF=1 ;;
         FGRUNCLEAN) add_2_valid "FGRUN"; FGRUNCLEAN=1 ;;
         FGRUNDEBUG) add_2_valid "FGRUN"; FGRUNDEBUG=1 ;;
         FGRUNNO) FGRUNADD=0 ;;
         NOFGRUN) FGRUNADD=0 ;;
         ATLASADD) ATLASADD=1 ;;
         ATLAS) add_2_valid $arg; ATLASADD=1 ;;
         ATLASUPD) add_2_valid "ATLAS"; ATLASUPD=1; ATLASADD=1 ;;
         ATLASCONF) add_2_valid "ATLAS"; ATLASCONF=1; ATLASADD=1 ;;
         ATLASAUTO) add_2_valid "ATLAS"; ATLASAUTO=1; ATLASADD=1 ;;
         ATLASCLEAN) add_2_valid "ATLAS"; ATLASCLEAN=1; ATLASADD=1 ;;
         ATLASDEBUG) add_2_valid "ATLAS"; ATLASDEBUG=1; ATLASADD=1 ;;
         NOATLAS) ATLASADD=0 ;;
         TGADD) TGADD=1 ;;
         TG) add_2_valid $arg; TGADD=1 ;;
         TGUPD) add_2_valid "TG"; TGUPD=1; TGADD=1 ;;
         TGCONF) add_2_valid "TG"; TGCONF=1; TGADD=1 ;;
         TGAUTO) add_2_valid "TG"; TGAUTO=1; TGADD=1 ;;
         TGCLEAN) add_2_valid "TG"; TGCLEAN=1; TGADD=1 ;;
         TGDEBUG) add_2_valid "TG"; TGDEBUG=1; TGADD=1 ;;
         DOUPD) DOUPDATE=1; TOOLUPD=1; DELSCRIPTS=1 ;;
         DOUPD2) DOUPD2=1 ;;
         TOOLUPD) TOOLUPD=1 ;;
         TOOLUPDONLY) TOOLUPD=1; TOOLUPDONLY=1 ;;
         NO_TOOL_UPD) NO_TOOL_UPD=1 ;;
         DELSCRIPTS) DELSCRIPTS=1 ;;
         ADDDEBUG) ADDDEBUG=1 ;;
         DEBUG_ON) DEBUG_ON=1 ;;
         NOPAUSE) NOPAUSE=1 ;;
         NO_PAUSE) NOPAUSE=1 ;;
         NO_CONF) NO_CONF_FILE=1 ;;
         DOPAUSE) NOPAUSE=0 ;;
         HELP) show_help ;;
         BOOSTPATH=*) set_boost_path $arg ;;
         BOOSTLIBS=*) set_boost_libs $arg ;;
         --help) show_help ;;
         -h) show_help ;;
         ?) show_help ;;
         -?) show_help ;;
         *)
            echo2 "ERROR: Invalid argument [$arg]"
            FAILED=`expr $FAILED + 1`
            if [ "$INVALID" = "" ] ; then
               INVALID=$arg
            else
               INVALID="$INVALID $arg"
            fi
            ;;
      esac
   done
}

# Try to SHOW exaactly WHAT will be ACTIONED
# This is HARD, due to the many options, and interaction of the options
post_cmd_list()
{
   if [ "$OSGNEW" = "1" ]; then
      SGCLEAN=1
      FGCLEAN=1
   fi
   # add items
   # tools
   if [ "$NO_TOOL_UPD" = "0" ]; then
      if [ "$MADETMP" = "1" ] || [ "$TOOLUPD" = "1" ] || [ ! -d $INSTDIR ]; then
         if [ "$TOOLUPDONLY" = "1" ]; then
            ALL_COMP="TOOLUP ONLY AND EXIT!"
         else
            ALL_COMP="TOOLUPD"
         fi
      else
         if [ "$DOBOOSTUPD" = "1" ]; then
            ALL_COMP="BOOSTUPD"
         fi
      fi
   fi

   # plib - this should be cleared if antoher install given/used
   if [ "$PLIBADD" = "1" ] && [ `is_in_args "PLIB"` -gt "0" ] ; then
      ALL_COMP="$ALL_COMP PLIB"
   fi

   # OSG
   if [ "$OSGADD" = "1" ] && [ `is_in_args "OSG"` -gt "0" ]; then
      ALL_COMP="$ALL_COMP $OSG_SOURCE_DIR"
      if [ "$OSGEXAM" = "1" ]; then
         ALL_COMP="$ALL_COMP +Examples"
      fi
   fi

   # SG
   if [ "$SGADD" = "1" ] && [ `is_in_args "SG"` -gt "0" ]; then
        if [ -d $SIMGEAR_SOURCE_DIR ]; then
            if [ "$SGCLEAN" = "1" ]; then
                ALL_COMP="$ALL_COMP SGCLEAN"
            else
                ALL_COMP="$ALL_COMP SG"
            fi
        else
            ALL_COMP="$ALL_COMP SGCO"
        fi
   fi
   # FG
   if [ "$FGADD" = "1" ] && ( [ `is_in_args "FG"` -gt "0" ] || [ `is_in_args "FGFS"` -gt "0" ] ) ; then
        if [ -d "$FGFS_SOURCE_DIR/source" ]; then
            if [ "$FGCLEAN" = "1" ]; then
                ALL_COMP="$ALL_COMP FGCLEAN"
            else
                ALL_COMP="$ALL_COMP FG"
            fi
        else
            ALL_COMP="$ALL_COMP FGCO"
        fi
   fi
   # fgrun
   if [ "$FGRUNADD" = "1" ] && [ "$FGADD" = "1" ] && [ `is_in_args "FGRUN"` -gt "0" ]; then
        if [ -d $FGRUN_SOURCE_DIR ]; then
            ALL_COMP="$ALL_COMP FGRUN"
        else
            ALL_COMP="$ALL_COMP FGRUNCO"
        fi
   fi
   # add in OPTIONAL items
   if [ "$FGADD" = "1" ] && [ "$ATLASADD" = "1" ] && [ `is_in_args "ATLAS"` -gt "0" ]; then
   #if [ "$ATLASADD" = "1" ] || [ "$ATLAS" = "1" ]; then
      if [ "$ATLASCLEAN" = "1" ]; then
         ALL_COMP="$ALL_COMP ATLAS+CLEAN"
      else
         ALL_COMP="$ALL_COMP ATLAS"
      fi
   fi
   # TG
   if [ "$TGADD" = "1" ] && [ `is_in_args "TG"` -gt "0" ]; then
      ALL_COMP="$ALL_COMP TG"
   fi
   if [ "$FGADDDATA" = "1" ]; then
      ALL_COMP="$ALL_COMP FGDATA"
   fi
   # add in script reset
   if [ "$DELSCRIPTS" = "1" ]; then
      ALL_COMP="$ALL_COMP REDO_SCRIPTS"
   fi
   # is there a problem
   if [ "$FAILED" = "0" ] ; then
      if [ "$#" = "0" ] ; then
         echo2 "$BN: No arguments given. Will process ALL components: [$ALL_COMP]."
      else
         echo2 "$BN: Args ok [$VALID]. Will process: [$ALL_COMP]"
      fi
      echo2 "$BN: The script pauses for input of a 'y' frequently. Any other input, or none, and it will exit."
   else
      if [ -z "$VALID" ] ; then
         echo2 "$BN: No valid arguments. Failed [$FAILED] ... Invalid argument(s) [$INVALID] ..."
      else
         echo2 "$BN: Args [$VALID] ok, BUT failed [$FAILED] - Invalid argument(s) [$INVALID] ..."
      fi
      echo2 "$BN: Valid arguments are:"
      out_help_list
      if [ -z "$VALID" ] ; then
         echo2 "$BN: No valid arguments. Failed [$FAILED] ... Invalid argument(s) [$INVALID] ..."
      else
         echo2 "$BN: Args [$VALID] ok, BUT failed [$FAILED] - Invalid argument(s) [$INVALID] ..."
      fi
      echo2 "$BN: Aborting..."
      exit 1
   fi
}

wait_for_input()
{
    if [ "$#" -gt "0" ] ; then
        echo2 "$1"
    fi
    if [ "$NOPAUSE" = "1" ]; then
        echod "$BN: Got NOPAUSE... continuing..."
    else
        echo -n "$BNAME: Enter y to continue : "
        read char
        if [ "$char" = "y" -o "$char" = "Y" ]; then
            echo2 "$BNAME: Got [$char] ... continuing ..."
        else
            if [ "$char" = "" ] ; then
                echo2 "$BNAME: Aborting ... no input!"
            else
                echo2 "$BNAME: Aborting ... got [$char]!"
            fi
            exit 1
        fi
    fi
    echod "$BN: Done wait_for_input..."
    output_the_time
}

wait_input()
{
   # unconditional WAIT
   SAVENOPAUSE=$NOPAUSE
   NOPAUSE=0
   wait_for_input "$@"
   NOPAUSE=$SAVENOPAUSE
}
show_no_pause()
{
    if [ "$NOPAUSE" = "1" ]; then
        echo2 "$BN: Due to NOPAUSE, unless there is a problem, this will be the only 'pause'"
    fi
}
is_in_args()
{
result=0
   if [ "$VALID" = "" ]; then
      result=1    # no argument = ALL ON
   else
      for arg in $VALID
      do
         if [ "$1" = "$arg" ]
         then
            result=1
            break;
         fi
      done
   fi
   # NOTE: Must output using echo, NOT echo2
   echo $result
}

# do NOT delete - just rename to .old if .old does not exist, or to .bak otherwise
rename_to_old_bak()
{
   if [ -f "$1" ]; then
      NEWF="$1.old"
      if [ -f "$NEWF" ]; then
         NEWF="$1.bak"
         mv -vf $1 $NEWF
      else
         mv -v $1 $NEWF
      fi
   fi
}

# 2010-02-25 - since I sometimes modify these files
# I now choose to RENAME rather than DELETE!!!
delete_scripts()
{
   if [ -f $FGFSSCRIPT ] ; then
      echo2 "deleting $FGFSSCRIPT"
      rename_to_old_bak "$FGFSSCRIPT"
   fi
   if [ -f $FGVIEWER ] ; then
      echo2 "deleting $FGVIEWER"
      rename_to_old_bak "$FGVIEWER"
   fi
   if [ -f $FGRUNSCRIPT ] ; then
      echo2 "deleting $FGRUNSCRIPT"
      rename_to_old_bak "$FGRUNSCRIPT"
   fi
   if [ -f $ATLASSCRIPT ] ; then
      echo2 "deleting $ATLASSCRIPT"
      rename_to_old_bak "$ATLASSCRIPT"
   fi
}

before_autogen()
{
    echod "$BN: before_autogen: deleting 'configure' and 'autom4te.cache'..."
   if [ -f configure ]; then
      rm -vf configure
   fi
   if [ -d autom4te.cache ]; then
      rm -r -vf autom4te.cache
   fi
}

run_autogen_sh()
{
   echod "$BN: run_autogen_sh: check for 'autogen.sh' file..."
   if [ ! -f autogen.sh ]; then
      echo2 "$BN: ERROR: can not locate autogen.sh in $(pwd)! aborting..."
      exit 1
   fi
   if [ ! -x autogen.sh ]; then
      echo2 "$BN: WARNING: Setting autogen.sh as executable."
      chmod +x autogen.sh
   fi
   before_autogen
   if [ "$ADDDEBUG" = "1" ] ; then
      echo2 "$BN: Running autogen.sh in [$(pwd)]... output to $OUTFILE"
      ./autogen.sh >> $OUTFILE
   else
      echo2 "$BN: Running autogen.sh in [$(pwd)]..."
      ./autogen.sh
   fi
}

get_install()
{
   echo2 "Installing $@"
   sudo apt-get install $@
}

get_install_boost()
{
   if [ "$SGBOOST" = "1" ]; then
      echo2 "$BN: Boost headers have been installed locally..."
   else
      # MAYBE this could just be apt-get install "libboost-*"
      # get_install "libboost-date-time-dev  libboost-date-time1.34.1  libboost-dev  libboost-doc libboost-filesystem-dev  libboost-filesystem1.34.1   libboost-graph-dev   libboost-graph1.34.1   libboost-iostreams-dev   libboost-iostreams1.34.1 libboost-program-options-dev   libboost-program-options1.34.1   libboost-python-dev   libboost-python1.34.1   libboost-regex-dev   libboost-regex1.34.1   libboost-signals-dev   libboost-signals1.34.1   libboost-test-dev   libboost-test1.34.1   libboost-thread-dev   libboost-thread1.34.1"
      echo2 "$BN: Installing all Boost headers and librairies..."
      get_install "libboost-*"
   fi
   DONEBOOST=1
}

before_configure()
{
   if [ -f config.log ]; then
      rm -vf config.log
   fi
   if [ -f Makefile ]; then
      rm -vf Makefile
   fi
   if [ -f config.status ]; then
      rm -vf config.status
   fi

}

after_configure()
{
   if [ ! -f Makefile ]; then
      echo2 "$BNAME: ERROR: configuration FAILED to write Makefile!"
      if [ -f config.log ]; then
         echo2 "$BNAME: copying the config.log to the LOG file..."
         cat config.log >> $OUTFILE
      else
         echo2 "$BNAME: it appears even the config.log file is missing!"
      fi
      echo2 "$BNAME: This suggests a catastrophic failure, and can not continue! aborting..."
      exit 1
   fi
}

#########################################################
# should cycle this LOG file
get_next_log()
{
   if [ ! -d $TMP_DIR ]; then
      make_tmp_directory
      if [ ! -d $TMP_DIR ]; then
         echo "ERROR: can not make '$TMP_DIR' directory! aborting..."
         exit 1
      fi
   fi
   FILTIT="$TMP_DIR/templog"
   CNT=1
   NXFIL=${FILTIT}${CNT}.txt
   while [ -f $NXFIL ] ; do
      CNT=`expr $CNT + 1`
      NXFIL=${FILTIT}${CNT}.txt
   done
   echo $NXFIL
}

get_next_cvs_log()
{
   if [ ! -d "$CBD/tmp" ]; then
      mkdir "$CBD/tmp"
      if [ ! -d "$CBD/tmp" ]; then
         echo "ERROR: can not make '$CBD/tmp' directory! aborting..."
         exit 1
      fi
   fi
   FILTIT="$CBD/tmp/tempcvs"
   CNT=1
   NXFIL=${FILTIT}${CNT}.txt
   while [ -f $NXFIL ] ; do
      CNT=`expr $CNT + 1`
      NXFIL=${FILTIT}${CNT}.txt
   done
   echo $NXFIL
}

show_scripts()
{
   if [ "$FGADD" = "1" ] && [ -f "$FGFSSCRIPT" ]; then
      if [ -f "$FGVIEWER" ]; then
         echo2 "To view a model, run the ./$FGVIEWER, followed by a model (.ac) file name"
      fi
      echo2 "To start fgfs, run the ./$FGFSSCRIPT file"
   fi
   if [ "$FGADD" = "1" ] && [ "$FGRUNADD" = "1" ] && [ -f "$FGRUNSCRIPT" ] && [ -d "$INSTALL_DIR_FGRUN" ]; then
      echo2 "To start fgrun, run the ./$FGRUNSCRIPT file"
   fi
   if [ "$FGADD" = "1" ] && [ "$ATLASADD" = "1" ] && [ -f "$ATLASSCRIPT" ] && [ -d "$INSTALL_DIR_ATLAS" ]; then
      echo2 "To start Atlas, run the ./$ATLASSCRIPT file"
   fi
}

write_fgviewer_script()
{
   cat <<EOT4 > $FGVIEWER
#!/bin/sh
#< $FGVIEWER version=$SCVERSION, $SCDATE - run from anywhere
BN=\`basename \$0\`
cd $CBD
HERE=\$PWD
AC="\$HERE/fgfs/data/Aircraft/c172p/Models/c172p.ac"
DATA="\$HERE/fgfs/data"
FGV="./fgviewer"
fgv_show_help()
{
   echo "\$BN: Version $SCVERSION"
   echo "\$BN: Enter name of .ac model to load"
   echo "\$BN: Help exit."
   exit 0
}
fgv_check_cmds()
{
   if [ "\$#" -gt "0" ]; then
      echo "\$BN: Checking \$# arguments ... [\$*]"
   else
      echo "\$BN: No argument. Using default [\$AC] file..."
   fi
   for arg in \$@
   do
      case \$arg in
         --help) fgv_show_help ;;
         -h) fgv_show_help ;;
         *)
            AC="\$HERE/\$arg"
            if [ ! -f "\$AC" ]; then
               AC="\$arg"
               if [ ! -f "\$AC" ]; then
                  echo "ERROR: Invalid file [\$arg]!";
                  exit 1
               fi
            fi
         ;;
      esac
   done
}
fgv_check_cmds \$@
cd $SUB_INSTALL_DIR/$FGFS_SOURCE_DIR/bin
if [ -f "\$AC" ]; then
   if [ -f "\$DATA/version" ]; then
      if [ -f "\$FGV" ]; then
         export LD_LIBRARY_PATH=../../$OSG_SOURCE_DIR/lib
         export FG_ROOT=\$DATA
         echo "\$BN: Running: \$FGV \$AC \$@ &"
         \$FGV \$AC \$@ &
      else
         echo "\$BN: ERROR: Can NOT locate [\$FGV] file!"
         exit 1
      fi
   else
      echo "\$BN: ERROR: Can NOT locate [\$DATA/version] file!"
      exit 1
   fi
else
   echo "\$BN: ERROR: Can NOT locate file [\$AC]!"
   exit 1
fi
EOT4
   chmod 755 $FGVIEWER
   echo2 "Created $FGVIEWER"

}

# export LD_LIBRARY_PATH=../../$PLIB_SOURCE_DIR/lib:../../$OSG_SOURCE_DIR/lib:../../$SIMGEAR_INSTALL_DIR/lib
#echo "\$BN: Running: ./fgfs --fg-root=\$HERE/$FGFS_SOURCE_DIR/data \$@ &"
#./fgfs --fg-root=\$HERE/$FGFS_SOURCE_DIR/data \$@ &
write_fgfs_script()
{
   cat <<EOT1 > $FGFSSCRIPT
#!/bin/sh
#< $FGFSSCRIPT version=$SCVERSION, $SCDATE - run from anywhere
BN=\`basename \$0\`
cd $CBD
HERE=\$PWD
cd $SUB_INSTALL_DIR/$FGFS_SOURCE_DIR/bin
export LD_LIBRARY_PATH=$INSTALL_DIR_OSG/lib
echo "\$BN: Running: ./fgfs --fg-root=$FGFS_DATA_DIR \$@ &"
./fgfs --fg-root=$FGFS_DATA_DIR \$@ &
EOT1
   chmod 755 $FGFSSCRIPT
   echo2 "Created $FGFSSCRIPT"
   write_fgviewer_script
}

#echo "\$BN: Running [./fgrun --fg-exe=../../$FGFS_SOURCE_DIR/bin/$FGFS_SOURCE_DIR --fg-root=../../../$FGFS_SOURCE_DIR/data \$@ &"
#./fgrun --fg-exe=../../$FGFS_SOURCE_DIR/bin/$FGFS_SOURCE_DIR --fg-root=../../../$FGFS_SOURCE_DIR/data \$@ &
write_fgrun_script()
{
   cat <<EOT2 > $FGRUNSCRIPT
#!/bin/sh
#< $FGRUNSCRIPT version=$SCVERSION, $SCDATE - run from anywhere
BN=\`basename \$0\`
cd $CBD
HERE=\$PWD
cd $SUB_INSTALL_DIR/$FGRUN_INSTALL_DIR/bin
export LD_LIBRARY_PATH=$INSTALL_DIR_OSG/lib
echo "\$BN: Running [./fgrun --fg-exe=$INSTALL_DIR_FGFS/bin/fgfs --fg-root=$FGFS_DATA_DIR \$@ &"
./fgrun --fg-exe=$INSTALL_DIR_FGFS/bin/fgfs --fg-root=$FGFS_DATA_DIR/data \$@ &
EOT2
   chmod 755 $FGRUNSCRIPT
   echo2 "Created $FGRUNSCRIPT"
}

write_atlas_script()
{
   cat <<EOT3 > $ATLASSCRIPT
#!/bin/sh
#< $ATLASSCRIPT version=$SCVERSION, $SCDATE - run from anywhere
BN=\`basename \$0\`
BASE_DIR=$CBD
# then Atlas binary should be in
# install/Atlas/bin
cd \$BASE_DIR
ATLAS=Atlas
MAP=Map
MAP_INSTALL=\$BASE_DIR/install/\$ATLAS/bin
MAP_APP=\$MAP_INSTALL/\$MAP
ATLAS_INSTALL=\$BASE_DIR/install/\$ATLAS/bin
ATLAS_APP=\$ATLAS_INSTALL/\$ATLAS
ATLAS_CMD="--airport=KSFO --udp"
ATLAS_MAP_DIR=\$BASE_DIR/install/\$ATLAS/maps
ATLAS_PAL_DIR=\$BASE_DIR/$ATLAS_SOURCE_DIR/src/data/Palettes/default.ap
FG_DATA_DIR=$FGFS_DATA_DIR
FG_VERSION=\$FG_DATA_DIR/version
HERE=\$(pwd)
echo "Running \$BN in \$HERE..."
if [ -e \$ATLAS_APP ]; then
    echo "\$BN: Found [\$ATLAS_APP] application..."
    if [ -f \$FG_VERSION ]; then
        echo -n "\$BN: Found FG data version file. Version "
        echo \`cat \$FG_VERSION\`
        ATLAS_CMD="\$ATLAS_CMD --fg-root=\$FG_DATA_DIR"
        if [ -d \$ATLAS_MAP_DIR ]; then
            echo "\$BN: Found [\$ATLAS_MAP_DIR] directory, to hold maps created"
        else
            echo "\$BN: Creating an [\$ATLAS_MAP_DIR] directory, to hold maps created"
            mkdir \$ATLAS_MAP_DIR
            if [ ! -d \$ATLAS_MAP_DIR ]; then
                echo "\$BN: FAILED: Can not create directory to hold maps! aborting!"
                exit 1
            fi
            mkdir \$ATLAS_MAP_DIR/lowres
        fi
        ATLAS_CMD="\$ATLAS_CMD --atlas=\$ATLAS_MAP_DIR --palette=\$ATLAS_PAL_DIR"
        #if [ -e \$MAP_APP ]; then
        #    echo "\$BN: Found MAP application, \$MAP_APP"
        #    ATLAS_CMD="\$ATLAS_CMD --map-executable=\$MAP_APP"
        #else
        #    echo "\$BN: Warning: Unable to locate MAP application, \$MAP_APP"
        #fi
        echo "\$ATLAS_APP \$ATLAS_CMD"
        \$ATLAS_APP \$ATLAS_CMD &
    else
        echo "\$BN: FAILED: Can NOT locate [\$FG_VERSION] file..."
        exit 1
    fi
else
    echo "\$BN: FAILED: Unable to locate [\$ATLAS_APP] application..."
    exit 1
fi

exit 0
EOT3
   chmod 755 $ATLASSCRIPT
   echo2 "Created $ATLASSCRIPT"
}

process_cvs_log()
{
   if [ -f "$1" ]; then
      set +e
      # -v = invert match - find all lines NOT starting 'cvs update:'
      CVSLIST=`grep -v -e "^cvs update:" $1`
      set -e
      if [ -z "$CVSLIST" ]; then
         echo2 "$BN: CVS appears fully up-to-date..."
      else
         echo2 "$BN: CVS update list..."
         echo2 "$CVSLIST"
      fi
   else
      echo2 "$BN: file [$1] not found!"
   fi
}

check_version_files()
{
   TMPDEL=$DELSCRIPTS
   TMPMSG=""
   if [ -f "$FG_VERS_TXT" ]; then
      TMP=`cat $FG_VERS_TXT`
      if [ ! "$TMP" = "$SCVERSION" ]; then
         DELSCRIPTS=1
         TMPMSG="$TMPMSG Diff script version $TMP vs $SCVERSION"
      fi
   else
      DELSCRIPTS=1
      TMPMSG="$TMPMSG No script version $FG_VERS_TXT"
   fi
   if [ -f "$FG_DATE_TXT" ]; then
      TMP=`cat $FG_DATE_TXT`
      if [ ! "$TMP" = "$SCDATE" ]; then
         DELSCRIPTS=1
         TMPMSG="$TMPMSG Diff script date $TMP vs $SCDATE"
         NEWDATE=1
      fi
   else
      DELSCRIPTS=1
      TMPMSG="$TMPMSG No script date $FG_DATE_TXT"
      NEWDATE=1
   fi
   # 2010-07-21 - fix this to check the INSTALL directory, NOT source
   if [ -f "$OSG_VERS_TXT" ]; then
      TMP=`cat $OSG_VERS_TXT`
      TMP2="$CBD/$INSTALL_DIR_OSG"
      if [ "$TMP" = "$INSTALL_DIR_OSG" ] || [ "$TMP" = "$TMP2" ]; then
         echod "$BN: Got SAME OSG VERSION directory [$TMP]..."
      else  # if [ ! "$TMP" = "$INSTALL_DIR_OSG" ]; then
         DELSCRIPTS=1
         OSGNEW=1
         TMPMSG="$TMPMSG Diff OSG version $TMP vs $INSTALL_DIR_OSG (OSGNEW)"
      fi
   else
      DELSCRIPTS=1
      OSGNEW=2
      TMPMSG="$TMPMSG No OSG version $OSG_VERS_TXT (OSGNEW)"
   fi
   if [ "$DELSCRIPTS" = "1" ] && [ ! "$TMPDEL" = "$DELSCRIPTS" ]; then
      echo2 "$BN: Add delete scripts due to new version(s), or change..."
      echod "$BN: $TMPMSG"
   fi

}

# END FUNCTIONS
########################################################

########################
##### MAIN PROGRAM #####
########################
# Output LOG set, and initialised
if [ ! -d $TMP_DIR ]; then
   MADETMP=1
fi
NEXTLOG=`get_next_log`
OUTFILE="$CBD/$NEXTLOG"

# INITIALISE THE OUT LOG FILE - after use only echo2
echo "Processing $BNAME, version $SCVERSION, of $SCDATE, on $RUNDATE, with command [$@], next log=[$OUTFILE]" > $OUTFILE
echo "Processing $BNAME, version $SCVERSION, of $SCDATE, on $RUNDATE,"
echo "with command [$@], next log is [$OUTFILE]"

set_runtime_dir     # establish run-time, and CONF file
# check all command inputs are valid
check_cmds $@
set_directories      # establish all the directories being used
check_version_files  # potentially delete previous scripts, if big change in version
post_cmd_list $@     # get list of what is being done

if [ ! -d $INSTDIR ]; then
   echo " "
   show_new_directory
   show_no_pause
   wait_input "$BN: *** ARE YOU SURE YOU WANT TO CONTINUE? ***"
else
    check_fgfs_install_path "$INSTALL_DIR_FGFS"
    #wait_for_input "$BN: Commands checked. Running [$BNAME] in [$CBD]. Continue?"
    show_no_pause
    wait_input "$BN: Commands checked. Running [$BNAME] in [$CBD]. Continue?"
fi

# 2. Update TOOLS, if requested, or a new installation
if [ "$DOUPDATE" = "1" ] || [ "$TOOLUPD" = "1" ] || [ "$TOOLUPDONLY" = "1" ] || [ ! -d $INSTDIR ]; then
   if [ "$NO_TOOL_UPD" = "1" ]; then
      echo2 "$BNAME: Tool and package update skipped by command."
      ALL_SKIP="$ALL_SKIP TOOLUPD due NO_TOOL_UPD"
   else
      echo2 "$BNAME: About to do a TOOL and PACKAGE UPDATE, covering many things..."
      echo2 "$BNAME: This may add, or update lots of things to your system, SO BE SURE!"
      echo2 "$BNAME: If you want to avoid this, abort, and run with NO_TOOL_UPD command added."
      echo2 "$BNAME: This does no harm if you already have the latest. The list includes -"
      echo2 "build-essential make automake libtool autoconf"
      echo2 "libglut3-dev libopenal-dev libalut-dev libalut0 libopenal0a libfltk1.1-dev"
      echo2 "libfltk1.1 zlib1g-dev zlib1g libwxgtk2.8-0 libwxgtk2.8-dev"
      echo2 "libjpeg62-dev libjpeg62 libxi-dev libxi6 libxmu-dev libxmu6"
      echo2 "fluid gawk gettext gcc g++ cmake cvs subversion libopenthreads6..."
      if [ "$TGADD" = "1" ]; then
         echo2 "$BNAME: And added for terragear-cs..."
         echo2 "git-core curl xlibmesa-gl-dev freeglut3-dev glutg3-dev libglut3-dev xorg-dev"
         echo2 "libalut-dev zlib1g-dev libtiff4 libtiff4-dev libtiff-tools libtiff-opengl"
      fi
      wait_input "$BN: Proceed with tool package update. You will need to give your PASSWORD when asked."
      echo2 "$BNAME: Doing tool and package update... you will need to enter your PASSWORD"
      get_install "build-essential make automake libtool autoconf"
      get_install "libglut3-dev libopenal-dev libalut-dev libalut0 libopenal0a libfltk1.1-dev"
      get_install "libfltk1.1 zlib1g-dev zlib1g libwxgtk2.8-0 libwxgtk2.8-dev"
      get_install "libjpeg62-dev libjpeg62 libxi-dev libxi6 libxmu-dev libxmu6"
      get_install "fluid gawk gettext"
      get_install "gcc g++"
      get_install "cmake cvs subversion"
      echo2 "$BNAME: package update... The following should NOT be required, but with the present SG/FG it is!"
      #echo2 "Will do 'sudo apt-get install libopenthreads6 openscenegraph'"
      echo2 "$BNAME: Will do 'sudo apt-get install libopenthreads6'"
      #wait_for_input "Continue to update these?"
      get_install "libopenthreads6"
      if [ "$TGADD" = "1" ]; then
         echo2 "$BNAME: Added for terragear-cs..."
         get_install "git-core curl"
         get_install "xlibmesa-gl-dev freeglut3-dev glutg3-dev libglut3-dev xorg-dev"
         get_install "libalut-dev zlib1g-dev"
         get_install "libtiff4 libtiff4-dev libtiff-tools libtiff-opengl"
      fi
      # get_install "libboost-dev"
      # this has to be done BEFORE here!!! -> get_install_boost
      if [ "$TOOLUPDONLY" = "1" ] ; then
         echo2 "$BNAME: Done tool update only ..."
         exit 0
      fi
      ALL_DONE="$ALL_DONE TOOLUPD"
      wait_for_input "$BNAME: Done tool and package update. Continue?"
   fi
else
   echo2 "$BNAME: Tool and package update skipped."
   ALL_SKIP="$ALL_SKIP TOOLUPD"
fi

#######################################################
# SET TO EXIT ON AN ERROR
# Remove to continue on error, but not a good idea! It is better to abort, fix, and then continue ...
set -e

# cd into compile base directory
cd "$COMPILE_BASE_DIR"

# create the install directory for all built components
create_install_directory

#######################################################
# PLIB
#######################################################
# PLIB_SOURCE_DIR=plib-1.8.5 OR plib, depending on PLIBTRUNK
# INSTALL_DIR_PLIB=$INSTALL_DIR/$PLIB_SOURCE_DIR
cd "$CBD"
#svn co http://plib.svn.sourceforge.net/svnroot/plib/trunk plib
#cd plib
if [ "$PLIBADD" = "1" ] && [ `is_in_args "PLIB"` -gt "0" ] ; then
   echo2 "$BNAME: Processing PLIB ..."
   if [ "$PLIBTRUNK" = "1" ] ; then
      # USE PLIB SVN TRUNK
      if [ -d $PLIB_SOURCE_DIR ] ; then
         # do update?
         if [ "$DOUPDATE" = "1" ] || [ "$PLIBUPD" = "1" ] ; then
            cd $PLIB_SOURCE_DIR
            svn up
            cd ..
            PLIBUPD=1
            echo2 "$BNAME: Done plib update."
        else
            echo2 "$BNAME: No PLIB svn update ..."
        fi
      else
         # need to do a CHECKOUT
         echo2 "$BNAME: PLIB checkout with svn co http://plib.svn.sourceforge.net/svnroot/plib/trunk $PLIB_SOURCE_DIR"
         echo2 "$BNAME: Doing PLIB checkout of trunk ... moment..."
         svn co http://plib.svn.sourceforge.net/svnroot/plib/trunk $PLIB_SOURCE_DIR
         PLIBCO=1
         echo2 "$BNAME: Done plib checkout. Force SG/FG AUTO and CLEAN"
         SGAUTO=1
         FGAUTO=1
         SGCLEAN=1
         FGCLEAN=1
         DELSCRIPTS=1
      fi
   else
      # USE PLIB 1.8.5
      if [ ! -f plib-1.8.5.tar.gz ] ; then
         echo2 "$BNAME: Doing wget of plib-1.8.5 ... moment ..."
         wget -c http://plib.sourceforge.net/dist/plib-1.8.5.tar.gz
         if [ ! -f plib-1.8.5.tar.gz ] ; then
            echo2 "$BNAME: PLIB FAILED get of plib-1.8.5.tar.gz ... aborting!"
            exit 1
         fi
         tar zxvf plib-1.8.5.tar.gz
         echo2 "$BNAME: Done plib-1.8.5 get."
         PLIBCO=1
         SGAUTO=1
         FGAUTO=1
         SGCLEAN=1
         FGCLEAN=1
      fi
   fi

   if [ ! -d $PLIB_SOURCE_DIR ] ; then
      echo2 "$BN: ERROR: No PLIB [$PLIB_SOURCE_DIR] directory... aborting!"
      exit 1
   fi

   cd $PLIB_SOURCE_DIR
   if [ "$PLIBCO" = "1" ] || [ "$PLIBUPD" = "1" ] || [ "$PLIBAUTO" = "1" ] ; then
      run_autogen_sh
      wait_for_input "Done PLIB autogen.sh. Proceed?"
   fi

    if [ "$PLIBCO" = "1" ] || [ "$PLIBUPD" = "1" ] || [ "$PLIBAUTO" = "1" ] || [ "$PLIBCONF" = "1" ]; then
      echo2 "Running ./configure $PLIB_CONF"
      before_configure
      if [ "$ADDDEBUG" = "1" ] || [ "$PLIBDEBUG" = "1" ]; then
         ./configure $PLIB_CONF >> $OUTFILE
      else
         ./configure $PLIB_CONF
      fi
      after_configure
      wait_for_input "Done PLIB configure. Proceed?"
    fi

   if [ "$PLIBCLEAN" = "1" ] ; then
      wait_for_input "Done PLIB configure. Do make clean?"
      make clean
      wait_for_input "Done PLIB make clean. Do make?"
   fi

   make
   if [ ! -d $INSTALL_DIR_PLIB ] ; then
      echo2 "Creating PLIB install in $INSTALL_DIR_PLIB..."
      mkdir -p "$INSTALL_DIR_PLIB"
   fi

   wait_for_input "Done PLIB make. Continue to install?"
   make install
   cd ..

    # 2009-02-05 - add building the examples
    if [ "$PLIBEX" = "1" ]; then
        if [ -d $PLIB_EXAMPLE_DIR ]; then
            cd $PLIB_EXAMPLE_DIR
            if [ "$PLIBCO" = "1" ] || [ "$PLIBUPD" = "1" ] || [ "$PLIBAUTO" = "1" ] || \
                [ "$PLIBAUTOEX" = "1" ] || [ ! -f configure ]; then
                echo2 "Doing PLIB/examples ./autogen.sh ..."
                before_autogen
                LDFLAGS=-L$INSTALL_DIR_PLIB ./autogen.sh
                wait_for_input "Done PLIB/examples autogen.sh. Proceed to configure?"
                PLIBCONF=1
            fi
            if [ "$PLIBCO" = "1" ] || [ "$PLIBUPD" = "1" ] || [ "$PLIBAUTO" = "1" ] || \
                [ "$PLIBCONF" = "1" ] || [ "$PLIBCONFEX" = "1" ] || [ "$PLIBAUTOEX" = "1" ]; then
                wait_for_input "Run PLIB/examples ./configure $PLIB_CONF_EX ?"
                before_configure
                # LDFLAGS=-L$INSTALL_DIR_PLIB ./configure $PLIB_CONF_EX
                if [ "$ADDDEBUG" = "1" ] || [ "$PLIBDEBUG" = "1" ]; then
                   ./configure $PLIB_CONF_EX >> $OUTFILE
                else
                   ./configure $PLIB_CONF_EX
                fi
                after_configure
                wait_for_input "Done PLIB/examples configure. Proceed to make?"
            fi
            #LDFLAGS=-L$INSTALL_DIR_PLIB make
            make
            wait_for_input "Done PLIB/examples make. Proceed to install?"
            make install
            cd -
        else
            echo2 "WARNING: directory [$PLIB_EXAMPLE_DIR] does not exist!"
            echo2 "Can not build examples ..."
        fi
    fi

   ALL_DONE="$ALL_DONE PLIB"
   wait_for_input "Done all PLIB. Proceed?"
else
   echo2 "$BNAME: PLIB skipped."
   ALL_SKIP="$ALL_SKIP PLIB"
fi


#######################################################
# OpenSceneGraph
#######################################################
# OSG_SOURCE_DIR=OpenSceneGraph
# INSTALL_DIR_OSG=$INSTALL_DIR/$OSG_SOURCE_DIR
cd "$CBD"

if [ "$OSGADD" = "1" ] && [ `is_in_args "OSG"` -gt "0" ]; then
   if [ "$OSGEXAM" = "1" ]; then
      echo2 "Processing OpenSceneGraph, plus EXAMPLES..."
   else
      echo2 "Processing OpenSceneGraph..."
   fi
   if [ ! -d $OSG_SOURCE_DIR ]; then
      echo2 "Doing svn download of OSG ... moment ..."
      echo2 "CMD: svn co $OSG_SVN $OSG_SOURCE_DIR"
      svn co $OSG_SVN $OSG_SOURCE_DIR
      echo2 "Done OSG svn checkout ..."
      # echo2 "As this a FRESH checkout, you may have to run this script again,"
      # echo2 "to get OpenThreads, and other things to build correctly..."
      DELSCRIPTS=1
      OSGCO=1
      OSGNEW=1
      SGAUTO=1
      FGAUTO=1
      SGCLEAN=1
      FGCLEAN=1
   else
      if [ "$DOUPDATE" = "1" ] || [ "$OSGUPD" = "1" ] ; then
         if [ "$OSGNOUPD" = "1" ]; then
            echo2 "Avoiding OSG update deliberately..."
         else
            echo2 "Doing OSG svn update ... moment ..."
            cd $OSG_SOURCE_DIR
            svn up
            cd ..
            echo2 "Done OSG svn update."
            OSGUPD=1
         fi
      else
         echo2 "No OSG svn update ..."
      fi
   fi

   if [ ! -d $OSG_SOURCE_DIR ] ; then
      echo2 "ERROR: No $OSG_SOURCE_DIR! aborting ..."
      exit 1
   fi

   # added 2010-03-01 - Building the EXAMPLES, thus download the example data
   if [ "$OSGEXAM" = "1" ]; then
      # use OSG_SVN_DATA and OSG_DATA_DIR to also download the examples data
      if [ ! -d $OSG_DATA_DIR ]; then
         echo2 "Doing svn download of OSG DATA... moment ..."
         echo2 "CMD: svn co $OSG_SVN_DATA $OSG_DATA_DIR"
         svn co $OSG_SVN_DATA $OSG_DATA_DIR
         echo2 "Done svn OSG DATA checkout ..."
         if [ ! -d $OSG_DATA_DIR ] ; then
            echo2 "ERROR: No $OSG_DATA_DIR! aborting ..."
            exit 1
         fi
      else
         if [ "$DOUPDATE" = "1" ] || [ "$OSGUPD" = "1" ] ; then
            if [ "$OSGNOUPD" = "1" ]; then
               echo2 "Avoiding OSG DATA update deliberately..."
            else
               echo2 "Doing svn OSG DATA update... moment..."
               cd $OSG_DATA_DIR
               svn up
               cd ..
               echo2 "Done svn OSG DATA update."
            fi
         fi
      fi
   fi

   if [ "$OSGCO" = "1" ] || [ "$OSGUPD" = "1" ] ; then
      wait_for_input "Done CO or UPD. Proceed with cmake?"
   fi

   cd $OSG_SOURCE_DIR

   if [ "$DOUPDATE" = "1" ] || [ "$OSGUPD" = "1" ] || [ "$OSGCO" = "1" ] || [ "$OSGCLEAN" = "1" ] || [ "$OSGCMAKE" = "1" ]; then
      if [ "$OSGNOUPD" = "1" ]; then
         echo2 "Avoiding OSG update deliberately..."
      else
         rm -f CMakeCache.txt
         cmake .
         echo2 "NOTE WELL: Read the above CAREFULLY. It may contain warnings, and important advice."
         wait_for_input "Done cmake 1. Proceed to cmake with definitions?"
      fi
   fi

   echo2 "$BN: Running cmake $OSG_CMAKE ."
   cmake $OSG_CMAKE .

   if [ "$OSGCLEAN" = "1" ] ; then
      wait_for_input "$BN: Done cmake with definitions. Proceed to make clean?"
      make clean
      wait_for_input "Done make clean. Proceed to make?"
   else
      wait_for_input "Done cmake with definitions. Proceed to make?"
   fi
   make

   if [ "$OSGTOSTD" = "0" ]; then
      if [ ! -d $INSTALL_DIR_OSG ] ; then
         mkdir -p "$INSTALL_DIR_OSG"
      fi
   fi

   wait_for_input "Done make. Proceed to install?"
   make install
   cd -

   # check for link directory lib -> lib64
   # required because automake tools ONLY search for 'lib'
   # ############################################################################
   if [ "$OSGTOSTD" = "0" ]; then
      if [ -d $INSTALL_DIR_OSG ]; then
         # we have an OSG install directory, but WHICH type?
         if [ -d "$INSTALL_DIR_OSG/lib64" ]; then
            if [ -d "$INSTALL_DIR_OSG/lib" ]; then
               echo2 "OSG directory [$INSTALL_DIR_OSG/lib] exists, so FG should compile."
            else
               if [ "$OSGLINK" = "1" ]; then
                  cd $INSTALL_DIR_OSG
                  echo2 "NO directory [$INSTALL_DIR_OSG/lib] - Need to create symbolic LINK!"
                  echo2 "Doing [ln -s lib64 lib] in $(pwd)"
                  ln -s lib64 lib
                  cd "$CBD"
                  if [ ! -d "$INSTALL_DIR_OSG/lib" ]; then
                     echo2 "WARNING: OSG directory [$INSTALL_DIR_OSG/lib] link to lib64 FAILED!"
                  else
                     OSG_INSTALL_OK=1
                  fi
               else
                  echo2 "NO directory [$INSTALL_DIR_OSG/lib], and NO symbolic LINK! due OSGNOLINK switch"
               fi
            fi
         else
            # no 'lib64' - maybe OSG_FIX has worked
            if [ -d "$INSTALL_DIR_OSG/lib" ]; then
               echo2 "OSG directory [$INSTALL_DIR_OSG/lib] exists, so FG should compile."
               OSG_INSTALL_OK=1
            else
               echo2 "WARNING: OSG directory [$INSTALL_DIR_OSG/lib] nor [$INSTALL_DIR_OSG/lib64] does NOT exist!"
            fi
         fi
      else
         echo2 "WARNING: OSG directory [$INSTALL_DIR_OSG] does NOT exist!"
      fi
   fi
   ###############################################################################
   ALL_DONE="$ALL_DONE OSG"
   wait_for_input "Done OSG. Proceed?"
else
   echo2 "$BNAME: OSG skipped."
   ALL_SKIP="$ALL_SKIP OSG"
fi

#################################
# OpenThreads - included with OSG
#################################

#######################################################
# SIMGEAR
#######################################################
cd "$CBD"

if [ "$SGADD" = "1" ] && [ `is_in_args "SG"` -gt "0" ]; then
   echo2 "$BNAME: Processing $SIMGEAR_SOURCE_DIR ..."
   if [ "$ADDSGCS" = "1" ]; then
      if [ -d $SIMGEAR_SOURCE_DIR ]; then
         if [ "$DOUPDATE" = "1" ] || [ "$DOUPD2" = "1" ] || [ "$SGUPD" = "1" ]; then
            echo2 "$BNAME: Doing SG update (git pull)... moment ..."
            cd $SIMGEAR_SOURCE_DIR
            git pull
            cd -
            echo2 "$BNAME: Done SG update."
            SGUPD=1
         else
            echo2 "No SG update..."
         fi
      else
         echo2 "$BNAME: Doing SG git clone... moment..."
         echo2 "$BNAME: With cmd: git clone git://mapserver.flightgear.org/simgear-cs/"
         git clone git://mapserver.flightgear.org/simgear-cs/
         if [ ! -d $SIMGEAR_SOURCE_DIR ]; then
            echo2 "$BNAME: ERROR: git clone FAILED to create [$SIMGEAR_SOURCE_DIR]! aborting..."
            exit 1
         fi
         echo2 "$BNAME: Done SG clone."
         SGCO=1
      fi
   else
      # this gets put in simgear/source
      if [ ! -d "simgear" ]; then
         mkdir "simgear"
         if [ ! -d "simgear" ]; then
            echo2 "$BNAME: mkdir 'simgear' FAILED! aborting..."
            exit 1
         fi
      fi
      cd simgear
      if [ ! -d source ]; then
         echo2 "$BNAME: Doing SG GIT clone... moment..."
         echo2 "$BN: doing git clone git://gitorious.org/fg/simgear.git source"
         git clone git://gitorious.org/fg/simgear.git source
         echo2 "$BNAME: Done SG clone."
         SGCO=1
         DELSCRIPTS=1
         if [ ! -d source ]; then
            echo2 "$BNAME: ERROR in git clone! No 'source' created in $(pwd)! aborting..."
            exit 1
         fi
      else
         if [ "$DOUPDATE" = "1" ] || [ "$DOUPD2" = "1" ] || [ "$SGUPD" = "1" ] ; then
            CVSLOG=`get_next_cvs_log`
            echo2 "$BN: Doing SG git update... out to [$CVSLOG]... moment..."
            cd source
            echo2 "$BN: Doing: git pull to $CVSLOG, in $(pwd)..."
            git pull >$CVSLOG 2>&1
            cat $CVSLOG
            echo2 "$BNAME: Done SG update to [$CVSLOG] above..."
            cd ..
            SGUPD=1
         else
            echo2 "$BNAME: NO SG CVS update..."
         fi
      fi
      cd ..
   fi

   if [ ! -d $PLIB_SOURCE_DIR ]; then
      if [ "$PLIB_INSTALL_OK" = "0" ]; then
          echo2 "$BN: ERROR: No [$PLIB_SOURCE_DIR] directory... aborting..."
          exit 1
      fi
   fi
   if [ ! -d $OSG_SOURCE_DIR ]; then
      if [ "$OSG_INSTALL_OK" = "0" ]; then
          echo2 "$BN: ERROR: No [$OSG_SOURCE_DIR] directory... aborting ..."
          exit 1
      fi
   fi

   cd $SIMGEAR_SOURCE_DIR

   if [ ! -f configure ] || [ "$SGCO" = "1" ] || [ "$SGUPD" = "1" ] || [ "$SGAUTO" = "1" ] ; then
      wait_for_input "$BN: Done SG CO or UPD. Proceed with autogen.sh... run_autogen_sh..."
      run_autogen_sh
      wait_for_input "$BN: Done SG autogen.sh. Proceed with configure?"
      SGCONF=1
   fi

   if [ ! -f Makefile ] || [ "$SGCO" = "1" ] || [ "$SGUPD" = "1" ] || [ "$SGAUTO" = "1" ] || [ "$SGCONF" = "1" ] || [ "$SGDEBUG" = "1" ]; then
      echo2 "$BN: Doing SG ./configure $SG_CONF"
      before_configure
      if [ ! -z "$BOOSTEXP" ]; then
         echo2 "$BN: Doing $BOOSTEXP"
         $BOOSTEXP
      fi
      if [ "$ADDDEBUG" = "1" ] || [ "$SGDEBUG" = "1" ]; then
         echo2 "$BN: Doing SG ./configure $SG_CONF to $OUTFILE..."
         ./configure $SG_CONF >> $OUTFILE
      else
         ./configure $SG_CONF
      fi
      after_configure
      wait_for_input "$BN: Done SG ./configure. Continue to make?"
   else
      wait_for_input "$BN: Skipped SG ./configure. Continue to make?"
   fi

   if [ "$SGCLEAN" = "1" ] && [ -f Makefile ]; then
      wait_for_input "$BN: Proceed with make clean?"
      make clean
      wait_for_input "$BN: Done SG make clean. Proceed?"
   fi

   make

   wait_for_input "$BN: Done SG make. Proceed to install?"
   make install

   cd -
   ALL_DONE="$ALL_DONE SG"
   wait_for_input "$BN: SG all done. Continue?"
else
   echo2 "$BNAME: SG skipped."
   ALL_SKIP="$ALL_SKIP SG"
fi


#######################################################
# FGFS - Part 1 - data checkout/update is done last.
#######################################################
# FGFS_SOURCE_DIR=fgfs
# INSTALL_DIR_FGFS=$INSTALL_DIR/$FGFS_SOURCE_DIR
cd "$CBD"

if [ ! -d "$FGFS_SOURCE_DIR" ]; then
   echo2 "$BNAME: Creating $FGFS_SOURCE_DIR directory"
   mkdir "$FGFS_SOURCE_DIR"
   if [ ! -d "$FGFS_SOURCE_DIR" ]; then
      echo2 "$BNAME: Create [$FGFS_SOURCE_DIR] directory FAILED! aborting..."
      exit 1
   fi
fi

if [ "$FGADD" = "1" ]; then
   cd $FGFS_SOURCE_DIR
   if [ ! -d source ] || [ `is_in_args "FG"` -gt "0" ] || [ `is_in_args "FGFS"` -gt "0" ]; then
      if [ ! -d source ]; then
         echo2 "$BNAME: Doing FG GIT clone... moment..."
         echo2 "Doing: git clone git://gitorious.org/fg/flightgear.git source"
         git clone git://gitorious.org/fg/flightgear.git source
         echo2 "$BNAME: Done FG source clone out ..."
         FGCO=1
         DELSCRIPTS=1
         if [ ! -d source ]; then
            echo2 "$BNAME: ERROR: checkout of FlightGear FAILED!"
            echo2 "No 'source' directory in $(pwd)... aborting..."
            exit 1
         fi
      else
         if [ "$DOUPDATE" = "1" ] || [ "$DOUPD2" = "1" ] || [ "$FGUPD" = "1" ] ; then
            CVSLOG=`get_next_cvs_log`
            echo2 "$BNAME: Doing FG update... out to [$CVSLOG]... moment ..."
            cd source
            echo2 "$BN: Doing git pull to $CVSLOG..."
            git pull >$CVSLOG 2>&1
            cd ..
            cat $CVSLOG
            echo2 "$BNAME: Done FG update to [$CVSLOG]..."
            FGUPD=1
         else
            echo2 $BNAME: "NO FG update."
         fi
      fi

   fi

   cd ..

#  verify PLIB and OSG are there - actually this SHOULD be checking the 'INSTALL', not source, but fixed with _OK
   if [ ! -d $PLIB_SOURCE_DIR ] ; then
      if [ "$PLIB_INSTALL_OK" = "0" ]; then
          echo2 "$BNAME: ERROR: No $PLIB_SOURCE_DIR directory... aborting..."
          exit 1
      fi
   fi
   if [ ! -d $OSG_SOURCE_DIR ] ; then
      if [ "$OSG_INSTALL_OK" = "0" ]; then
          echo2 "$BNAME: ERROR: No $OSG_SOURCE_DIR... aborting..."
          exit 1
      fi
   fi

   if [ "$FGCO" = "1" ] || [ "$FGUPD" = "1" ] ; then
      wait_for_input "$BNAME: Done FG CO or UPD. Proceed?"
   fi

   cd $FGFS_SOURCE_DIR/source

   if [ "$FGCO" = "1" ] || [ `is_in_args "FGFS"` -gt "0" ] || [ `is_in_args "FG"` -gt "0" ]  || [ ! -f configure ] || [ ! -f Makefile ] || [ "$FGUPD" = "1" ] || [ "$FGUPD" = "1" ]; then

      #if [ "$OSGTOSTD" = "0" ]; then
      #   # this does NOT seem to help regarding the missing OSG library find
      #   echo2 "Adding: export LD_LIBRARY_PATH=$INSTALL_DIR_OSG/lib64:$LD_LIBRARY_PATH"
      #   export LD_LIBRARY_PATH=$INSTALL_DIR_OSG/lib64:$LD_LIBRARY_PATH
      #fi

      if [ "$FGCO" = "1" ] || [ "$FGUPD" = "1" ] || [ "$FGAUTO" = "1" ] || [ ! -f configure ]; then
         run_autogen_sh
         wait_for_input "$BN: Done FG autogen.sh. Proceed to configure?"
      fi

      if [ "$FGCO" = "1" ] || [ "$FGUPD" = "1" ] || [ "$FGAUTO" = "1" ] || [ "$FGCONF" = "1" ] || [ ! -f Makefile ]; then
         echo2 "$BNAME: Doing FG ./configure $FG_CONF"
         before_configure
         if [ "$ADDDEBUG" = "1" ] || [ "$FGDEBUG" = "1" ]; then
            if [ ! -z "$BOOSTEXP" ]; then
               echo2 "$BN: Doing $BOOSTEXP"
               $BOOSTEXP
            fi
            echo2 "$BNAME: Doing configure $FG_CONF, output to $OUTFILE..."
            ./configure $FG_CONF >> $OUTFILE 2>&1
            if [ -f config.log ]; then
               echo2 "$BN: Putting contents of config.log in the LOG file... between"
               echo2 "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*"
               cat config.log >> $OUTFILE
               echo2 "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*"
               echo2 "$BN: done..."
            else
               echo2 "$BN: WARNING: no config.log file written???"
            fi
         else
            echo2 "$BN: Doing configure $FG_CONF..."
            ./configure $FG_CONF
         fi
         after_configure
         wait_for_input "$BN: Done FG ./configure. Proceed to make"
      else
         wait_for_input "$BN: Skipped FG ./configure. Proceed to make?"
      fi

      if [ "$FGCLEAN" = "1" ] && [ -f Makefile ]; then
        wait_for_input "$BN: Proceed to make clean in FG [$(pwd)]?"
        make clean
        wait_for_input "$BN: Done FG make clean. Proceed?"
      fi

      make

      wait_for_input "$BN: Done FG make. Proceed to install?"
      make install

   fi
   cd ../..

   ALL_DONE="$ALL_DONE FG"
   wait_for_input "$BNAME: Done all FG. Proceed?"
else
   if [ ! "$TGADD" = "1" ]; then
      echo2 "$BNAME: FG skipped."
      ALL_SKIP="$ALL_SKIP FG"
   fi
fi

#######################################################
# FGRUN
#######################################################
# FGRUN_INSTALL_DIR=fgrun
# INSTALL_DIR_FGRUN=$INSTALL_DIR/$FGRUN_INSTALL_DIR
cd "$CBD"

if [ "$FGRUNADD" = "1" ] && [ "$FGADD" = "1" ] && [ `is_in_args "FGRUN"` -gt "0" ]; then
    echo2 "$BN: Doing FGRUN..."
   if [ ! -d $FGRUN_SOURCE_DIR ]
   then
      echo2 "$BN: Doing FGRUN svn checkout... moment..."
      svn co http://fgrun.svn.sourceforge.net/svnroot/fgrun/trunk fgrun
      echo2 "$BN: Done FGRUN svn checkout."
      FGRUNCO=1
      DELSCRIPTS=1
   else
      if [ "$DOUPDATE" = "1" ] || [ "$DOUPD2" = "1" ] || [ "$FGRUNUPD" = "1" ] ; then
         echo2 "$BN: Doing FGRUN update... moment ..."
         cd $FGRUN_SOURCE_DIR
         svn up
         cd -
         echo2 "$BN: Done FGRUN update."
         FGRUNUPD=1
      else
         echo2 "$BN: NO FGRUN update..."
      fi
   fi

   if [ ! -d $FGRUN_SOURCE_DIR ]; then
      echo2 "$BN: ERROR: no [$FGRUN_SOURCE_DIR] source folder... aborting..."
      exit 1
   fi

   if [ "$FGRUNCO" = "1" ] || [ "$FGRUNUPD" = "1" ] ; then
      wait_for_input "$BN: Done FGRUN CO or UPD. Proceed?"
   fi

   cd $FGRUN_SOURCE_DIR/

   if [ "$FGRUNCO" = "1" ] || [ "$FGRUNUPD" = "1" ]  || [ "$FGRUNAUTO" = "1" ] || \
   [ ! -f configure ]; then
      run_autogen_sh    # ./autogen.sh
      wait_for_input "$BN: Done FGRUN AUTO. Proceed?"
      FGRUNCONF=1
   fi

   if [ "$FGRUNCO" = "1" ] || [ "$FGRUNUPD" = "1" ] || [ "$FGRUNAUTO" = "1" ] || [ "$FGRUNCONF" = "1" ]; then
      before_configure
      if [ "$ADDDEBUG" = "1" ] || [ "$FGRUNDEBUG" = "1" ]; then
         echo2 "$BN: Running FGRUN $FGRUN_CONF output to $OUTFILE..."
         ./configure $FGRUN_CONF >> $OUTFILE
      else
         echo2 "$BN: Doing ./configure $FGRUN_CONF"
         ./configure $FGRUN_CONF
      fi
      after_configure
      wait_for_input "$BN: Done FGRUN configure. Proceed?"
   fi

   make

   wait_for_input "$BN: Done FGRUN make. Proceed to install?"
   make install

   cd -
   ALL_DONE="$ALL_DONE FGRUN"
   wait_for_input "$BN: Done FGRUN"
else
   if [ ! -d $FGRUN_SOURCE_DIR ] || [ ! -d $INSTALL_DIR_FGRUN ]; then
      FGRUNADD=0  # then do NOT write a script for it...
   fi
   if [ ! "$TGADD" = "1" ]; then
      echo2 "$BNAME: FGRUN skipped."
      ALL_SKIP="$ALL_SKIP FGRUN"
   fi
fi

####################################################################
# Atlas actions
####################################################################
if [ "$FGADD" = "1" ] && [ "$ATLASADD" = "1" ] && [ `is_in_args "ATLAS"` -gt "0" ]; then
    echo2 "$BN: Doing ATLAS..."
    if [ -d $ATLAS_SOURCE_DIR ]; then
        if [ "$DOUPDATE" = "1" ] || [ "$DOUPD2" = "1" ] || [ "$ATLASUPD" = "1" ]; then
            CVSLOG=`get_next_cvs_log`
            echo2 "$BN: Doing ATLAS CVS update... out to [$CVSLOG]... moment..."
            cd $ATLAS_SOURCE_DIR
            cvs up -dPA >$CVSLOG 2>&1
            process_cvs_log $CVSLOG
            echo2 "$BN: Done ATLAS update to [$CVSLOG]..."
            cd -
            ATLASUPD=1
        else
            echo2 "$BN: No ATLAS update..."
        fi
    else
        echo2 "$BN: Doing ATLAS cvs checkout... moment..."
        echo "$BN: cvs password: <enter> key"
        cvs -d:pserver:anonymous@atlas.cvs.sourceforge.net:/cvsroot/atlas login
        cvs -z3 -d:pserver:anonymous@atlas.cvs.sourceforge.net:/cvsroot/atlas co -P $ATLAS_SOURCE_DIR
        echo2 "Done ATLAS cvs checkout."
        ATLASCO=1
    fi

    if [ ! -d $ATLAS_SOURCE_DIR ]; then
        echo2 "$BN: ERROR: ATLAS: No [$ATLAS_SOURCE_DIR] directory... aborting..."
        exit 1
    fi

    if [ "$ATLASCO" = "1" ] || [ "$ATLASUPD" = "1" ] || [ "$ATLASAUTO" = "1" ]; then
        cd $ATLAS_SOURCE_DIR
        run_autogen_sh     # ./autogen.sh
        cd -
        wait_for_input "$BN: Done ATLAS autogen.sh. Proceed?"
    fi

    if [ "$ATLASCO" = "1" ] || [ "$ATLAS" = "1" ] || [ "$ATLASAUTO" = "1" ] || [ "$ATLASCONF" = "1" ] || [ "$ATLASCLEAN" = "1" ]; then
        cd $ATLAS_SOURCE_DIR

        wait_for_input "$BN: Do ATLAS configure? Proceed?"
        before_configure
        if [ "$ADDDEBUG" = "1" ] || [ "$ATLASDEBUG" = "1" ]; then
            echo2 "$BN: Doing ATLAS ./configure $ATLAS_CONF, to log file... moment..."
            ./configure $ATLAS_CONF >> $OUTFILE
        else
            echo2 "$BN: Doing ATLAS ./configure $ATLAS_CONF"
            ./configure $ATLAS_CONF
        fi
        after_configure
        wait_for_input "$BN: Done ATLAS configure. Proceed to make?"

        cd -
    fi

    cd $ATLAS_SOURCE_DIR

    if [ "$ATLASCLEAN" = "1" ] && [ -f Makefile ]; then
        echo2 "$BN: Doing ATLAS clean"
        make clean
        wait_for_input "$BN: Done ATLAS clean. Proceed?"
    fi

    echo2 "$BN: Doing ATLAS make..."
    make

    wait_for_input "$BN: Done ATLAS make. Proceed to install?"
    make install

    cd -
    ALL_DONE="$ALL_DONE ATLAS"
    wait_for_input "$BN: Done ATLAS"
else
   if [ ! -d $ATLAS_SOURCE_DIR ] || [ ! -d $INSTALL_DIR_ATLAS ]; then
      ATLASADD=0     # do NOT write a script for it
   fi
   if [ ! "$TGADD" = "1" ]; then
      echo2 "$BN: ATLAS skipped."
      ALL_SKIP="$ALL_SKIP ATLAS"
   fi
fi

cd "$CBD"

####################################################################
# TG actions
####################################################################
if [ "$TGADD" = "1" ] && [ `is_in_args "TG"` -gt "0" ]; then
   echo2 "Doing TerraGear... and components..."
   echo2 "Checking installation of newmat library, and headers.."
   if [ ! -f /usr/local/lib/libnewmat.a ] || [ ! -d /usr/local/include/newmat ] || [ ! -f /usr/local/include/newmat/newmat.h ]; then
      # library or include not installed
      echo2 "Moment, first need to create newmat library..."
      if [ ! -f newmat11.tar.gz ] || [ ! -d newmat ]; then
         if [ ! -f newmat11.tar.gz ]; then
            echo2 "Moment, fetching newmat source... using wget -c http://www.robertnz.net/ftp/newmat11.tar.gz"
            wget -c http://www.robertnz.net/ftp/newmat11.tar.gz
            if [ ! -f newmat11.tar.gz ]; then
               echo2 "ERROR: download of newmat11.tar.gz FAILED! aborting..."
               exit 1
            fi
         fi
         if [ ! -d newmat ]; then
            echo2 "Creating newmat folder..."
            mkdir newmat
         fi
         if [ ! -d newmat ]; then
            echo2 "Error: failed to create newmat folder... aborting..."
            exit 1
         fi
         cd newmat
         tar xzf ../newmat11.tar.gz
         if [ ! -f nm_gnu.mak ]; then
            echo2 "Error: can not locate file nm_gnu.mak! aborting..."
            exit 1
         fi
         if [ ! -f newmat.h ]; then
            echo2 "Error: can not locate file newmat.h! aborting..."
            exit 1
         fi
         cd ..
      fi
      if [ -d newmat ]; then
         echo2 "Building newmat library..."
         cd newmat
         make -f nm_gnu.mak
         if [ ! -f libnewmat.a ]; then
            echo2 "Error: failed the create libnewmat.a library. aborting..."
            exit 1
         fi
         if [ ! -d /usr/local ]; then
            echo2 "Need to create /usr/local directory."
            sudo mkdir /usr/local
         fi
         if [ ! -d /usr/local/lib ]; then
            echo2 "Need to create /usr/local/lib directory."
            sudo mkdir /usr/local/lib
         fi
         echo2 "Copying libnewmat.a to /usr/local/lib directory."
         sudo cp libnewmat.a /usr/local/lib
         if [ ! -d /usr/local/include/newmat ]; then
            echo2 "Creating /usr/local/include/newmat directory."
            sudo mkdir -p /usr/local/include/newmat
         fi
         echo2 "Copying newmat headers to /usr/local/include/newmat directory."
         sudo cp *.h /usr/local/include/newmat
         cd ..
      fi
      if [ -f /usr/local/lib/libnewmat.a ] && [ -d /usr/local/include/newmat ] && [ -f /usr/local/include/newmat/newmat.h ]; then
         wait_for_input "Done newmat, including installation. Continue?"
      else
         wait_for_input "$BNAME: WARNING: newmat not done! Continue?"
      fi
   fi
   echo2 "Checking installation of gpc... in /usr/local/lib and /usr/local/include..."
   if [ ! -f /usr/local/lib/libgenpolyclip.a ] || [ ! -f /usr/local/include/gpc.h ]; then
      echo2 "Moment, first need to create gpc library..."
      if [ ! -f gpc232.zip ] || [ ! -d gpc232 ]; then
         if [ ! -f gpc232.zip ]; then
            wget -c ftp://ftp.cs.man.ac.uk/pub/toby/gpc/gpc232.zip
            if [ ! -f gpc232.zip ]; then
               echo2 "Error: download of gpc232.zip failed! aborting..."
               exit 1
            fi
         fi
         if [ ! -d gpc232 ]; then
            unzip gpc232.zip
            if [ ! -d gpc232 ]; then
               echo2 "ERROR: Unzip of gpc232.zip failed! aborting..."
               exit 1
            fi
         fi
      fi
      # making gpc
      if [ -d gpc232 ]; then
         cd gpc232
         echo2 "making and installing gpc library..."
         if [ ! -f gpc.h ]; then
            echo2 "ERROR: can not locate gpc.h in folder gpc232! aborting..."
            exit 1
         fi
         sed "s/DBL_EPSILON/0.000001/" gpc.h > tempgpch.h
         ! diff gpc.h tempgpch.h
         echo2 "If the above substitution of 0.00001 for DBL_EPSILON"
         wait_for_input "looks correct, continue?"
         #rm -f gpc.h
         mv tempgpch.h gpc.h
         if [ ! -f gpc.c ]; then
            echo2 "ERROR: can not locate gpc.c in folder gpc232! aborting..."
            exit 1
         fi
         MKFIL=Makefile
         if [ ! -f $MKFIL ]; then
            echo2 "$BNAME: Attempting creation of gpc $MKFIL..."
            EOPT=
            UNSAFETAB=0
            SAVEDP=$NOPAUSE
            TRES=`echo "\t" | xxd -g 1 | cut -c10-11`
            if [ ! "$TRES" = "09" ]; then
               EOPT=-e
               TRES=`echo $EOPT "\t" | xxd -g 1 | cut -c10-11`
               if [ ! "$TRES" = "09" ]; then
                  NOPAUSE=0
                  echo2 "$BNAME: ***WARNING*** Can not seem to find the 'echo' option to expand a TAB!"
                  echo2 "$BNAME: thus the following code may not function. You may have to"
                  echo2 "        manually construct the Makefile to build the gpc library!"
                  wait_for_input "$BNAME: Continue anyway to try?"
                  NOPAUSE=$SAVEDP
                  UNSAFETAB=1
               fi
            fi
            echo "# Unix/Linux Makefile for GPC 2.32" > $MKFIL
            echo "# Riley Rainey  (riley.rainey@websimulations.com)" >> $MKFIL
            echo >> $MKFIL
            echo "CFLAGS = -O -g" >> $MKFIL
            echo "libgenpolyclip.a: gpc.o" >> $MKFIL
            echo $EOPT "\trm -f \$@" >> $MKFIL
            echo $EOPT "\tar cr \$@ \$<" >> $MKFIL
            echo $EOPT "\tranlib \$@" >> $MKFIL
            echo "clean:" >> $MKFIL
            echo $EOPT "\trm -f libgenpolyclip.a *.o core *~" >> $MKFIL
            echo "install: libgenpolyclip.a" >> $MKFIL
            echo $EOPT "\t-mkdir -p /usr/local/lib" >> $MKFIL
            echo $EOPT "\t-mkdir -p /usr/local/include" >> $MKFIL
            echo $EOPT "\tinstall libgenpolyclip.a /usr/local/lib/libgenpolyclip.a" >> $MKFIL
            echo $EOPT "\tinstall gpc.h /usr/local/include/gpc.h" >> $MKFIL
            echo >> $MKFIL
            cat $MKFIL
            if [ "$UNSAFETAB" = "1" ]; then
               cat $MKFIL >> $OUTFILE
               echo2 "$BNAME: Carefully CHECK the above output. There must be NO backslash t"
               echo2 "        before the rm -f, or ar cr, etc. If there is you MUST exit"
               echo2 "        and manually create the $MKFIL..."
               NOPAUSE=0
            fi
            wait_for_input "Created Makefile. Proceed to do make?"
            NOPAUSE=$SAVEDP
         fi
         echo2 "Proceeding to make the gpc library..."
         make
         if [ ! -f libgenpolyclip.a ]; then
            echo2 "$BNAME: Error: libgenpolyclip.a not made! aborting..."
            exit 1
         fi
         wait_for_input "Done make. Proceed to do (sudo) install?"
         sudo make install
         cd ..
      fi
      if [ -f /usr/local/lib/libgenpolyclip.a ] && [ -f /usr/local/include/gpc.h ]; then
         wait_for_input "Done gpc install. Continue?"
      else
         wait_for_input "gpc install FAILED! Continue?"
      fi
   fi
   echo2 "Checking for $TG_SOURCE_DIR..."
   if [ -d $TG_SOURCE_DIR ]; then
      if [ "$DOUPDATE" = "1" ] || [ "$DOUPD2" = "1" ] || [ "$TGUPD" = "1" ]; then
         cd $TG_SOURCE_DIR
         if [ "$TGCVS" = "1" ]; then
            CVSLOG=`get_next_cvs_log`
            echo2 "Doing TG CVS update... to [$CVSLOG]... moment ..."
            cvs up -dPA >$CVSLOG 2>&1
            process_cvs_log $CVSLOG
         else
            echo2 "Doing TG git update... moment ..."
            git pull
         fi
         cd -
         echo2 "Done TG update."
         TGUPD=1
      else
         echo2 "No TG update..."
      fi
   else
      if [ "$TGCVS" = "1" ]; then
         echo2 "Doing TG checkout into $TG_SOURCE_DIR... moment..."
         # for CVS terragear
         echo2 "CVS passwd: guest"
         cvs -d :pserver:cvs@cvs.terragear.org:/var/cvs/TerraGear-0.0 login
         cvs -d :pserver:cvs@cvs.terragear.org:/var/cvs/TerraGear-0.0 co source
         if [ ! -d "source" ]; then
            echo2 "ERROR: checkout source directory not created! aborting..."
            exit 1
         fi
         mv source $TG_SOURCE_DIR
         echo2 "Done TG checkout, renaming directory from 'source' to $TG_SOURCE_DIR."
      else
         echo2 "Doing TG git clone... moment..."
         echo2 "with cmd: git clone git://mapserver.flightgear.org/terragear-cs/"
         git clone git://mapserver.flightgear.org/terragear-cs/
         echo2 "Done TG got clone."
      fi
      TGCO=1
      if [ ! -d $TG_SOURCE_DIR ]; then
         echo2 "ERROR: cvs co or git clone failed to make directory [$TG_SOURCE_DIR]! aborting..."
         exit 1
      fi
      wait_for_input "Done TG co or git clone. Proceed to do autogen.sh?"
   fi

   if [ ! -d $TG_SOURCE_DIR ]; then
      echo2 "ERROR: ATLAS: No [$TG_SOURCE_DIR] directory... aborting..."
      exit 1
   fi

   cd $TG_SOURCE_DIR
   echo2 "Currently in folder $(pwd)"

   if [ "$TGCO" = "1" ] || [ "$TGUPD" = "1" ] || [ "$TGAUTO" = "1" ] || \
      [ ! -f configure ]; then
      run_autogen_sh
      wait_for_input "Done TG autogen.sh. Proceed?"
      TGCONF=1
    fi

    if [ "$TGCO" = "1" ] || [ "$TGUPD" = "1" ] || [ "$TGAUTO" = "1" ] || \
       [ "$TGCONF" = "1" ] || [ "$TGDEBUG" = "1" ]; then
        echo2 "Doing TG configuration..."
        before_configure
        if [ "$ADDDEBUG" = "1" ] || [ "$TGDEBUG" = "1" ]; then
            #echo2 "Doing TG $TG_POPT ./configure $TG_CONF, to log file... moment..."
            #export $TG_POPT && ./configure $TG_CONF >> $OUTFILE
            echo2 "Doing TG ./configure $TG_CONF, to LOG file... moment..."
            ./configure $TG_CONF >> $OUTFILE 2>&1
        else
            #echo2 "Doing TG $TG_POPT ./configure $TG_CONF"
            #export $TG_POPT && ./configure $TG_CONF
            echo2 "Doing TG ./configure $TG_CONF"
            ./configure $TG_CONF
        fi
        echo2 "Check the above CAREFULLY! Do NOT proceed if you find 'configure aborted'!"
        echo2 "It should end writing out the Makefile set..."
        echo2 "The configure MUST find -lplibul, plib/sg.h, abort if 'no' found..."
        echo2 "Use TGDEBUG to output all the configure to the LOG file '$OUTFILE'"
        echo2 "and/or read the config.log file carefully..."
        after_configure
        wait_for_input "Done TG configure. Proceed?"
    fi

    if [ "$TGCLEAN" = "1" ] && [ -f Makefile ]; then
        echo2 "$BN: Doing TG clean"
        make clean
        wait_for_input "$BN: Done TG clean. Proceed?"
    fi

    echo2 "$BN: Doing TG make..."
    make

    wait_for_input "Done TG make. Proceed to install?"
    echo2 "Installing TG tools to --exec-prefix=$INSTALL_DIR_TG/bin"
    if [ ! -d "$INSTALL_DIR_TG/bin" ]; then
      echo2 "This looks like a FIRST TIME install to this folder"
    fi
    make install
    if [ -d "$INSTALL_DIR_TG/bin" ]; then
      echo2 "Done install of TG tools to $INSTALL_DIR_TG/bin..."
      INFO_FILE="$INSTALL_DIR_TG/bin/$INST_FILE"
      if [ -f "$INFO_FILE" ]; then
         echo "Done $BNAME, v=$SCVERSION, $SCDATE, on $RUNDATE, cmd=[$@], log=[$OUTFILE]" >>$INFO_FILE
      else
         echo "Done $BNAME, v=$SCVERSION, $SCDATE, on $RUNDATE, cmd=[$@], log=[$OUTFILE](1)" >$INFO_FILE
      fi
    else
      echo2 "WARNING: Done install of TG tools, but $INSTALL_DIR_TG/bin does NOT seem to exist..."
    fi
    cd -
    ALL_DONE="$ALL_DONE TG"
    wait_for_input "Done TG"
else
   if [ "$TGADD" = "1" ]; then
      echo2 "$BNAME: TG skipped."
      ALL_SKIP="$ALL_SKIP TG"
   fi
fi

# build runtime scripts
# any checkout causes new scripts to be written
# or a change in this script version in fgversion.txt
if [ "$DELSCRIPTS" = "1" ] ; then
    delete_scripts
fi

if [ "$FGADD" = "1" ] && ( [ ! -f $FGFSSCRIPT ] || [ ! -f $FGVIEWER ] ); then
   write_fgfs_script
fi

if [ "$FGADD" = "1" ] && [ "$FGRUNADD" = "1" ] && [ ! -f $FGRUNSCRIPT ] && [ -d "$INSTALL_DIR_FGRUN" ]; then
   write_fgrun_script
fi

if [ "$FGADD" = "1" ] && [ "$ATLASADD" = "1" ] && [ ! -f $ATLASSCRIPT ] && [ -d "$INSTALL_DIR_ATLAS" ]; then
   write_atlas_script
fi

show_scripts

###############################################################
# FGFS - Part 2 - BASE DATA - THIS TAKES LOTS OF TIME AND SPACE
###############################################################
cd "$CBD"
if [ ! -d "$FGFS_SOURCE_DIR" ]; then
   mkdir $FGFS_SOURCE_DIR
   if [ ! -d "$FGFS_SOURCE_DIR" ]; then
      echo2 "ERROR: No [$FGFS_SOURCE_DIR] directory! aborting..."
      exit 1
   fi
fi

if [ "$FGADDDATA" = "1" ]; then
   cd $FGFS_SOURCE_DIR
   if [ ! -d data ]; then
      echo2 "$BN: If you have previously done a checkout of the base data,"
      echo2 "$BN: then you can update and use that ..."
      wait_for_input "Do FG data checkout?"
      echo2 "$BN: Doing FG data clone... moment ... be very, VERY patient..."
      echo2 "$BN: THIS CAN TAKE A LONG TIME!!! Be very patient!"
      echo2 "$BN: Doing: git clone --depth 1 git://gitorious.org/fg/fgdata.git data"
      git clone --depth 1 git://gitorious.org/fg/fgdata.git data
      if [ ! -d data ]; then
         echo2 "$BNAME: WARNING: No FG data directory... DATA CLONE FAILED!"
         #exit 1
      else
         echo2 "$BN: Done FG data clone --depth 1 ONLY."
         FGUPDDATA=1
      fi
   else
      # do we want an UPDATE?
      if [ "$DOUPDATE" = "1" ] || [ "$DOUPD2" = "1" ] || [ "$FGUPD" = "1" ]  || [ "$FGUPDDATA" = "1" ]; then
         CVSLOG=`get_next_cvs_log`
         echo2 "$BN: Doing FG data update... out to [$CVSLOG]... moment..."
         echo "$BN: THIS CAN TAKE A LONG TIME!!! Be very patient!"
         cd data
         git pull >$CVSLOG 2>&1
         cd ..
         cat $CVSLOG
         echo2 "$BN: Done FG data update. See [$CVSLOG]..."
         ALL_DONE="$ALL_DONE FGDATA"
         FGUPDDATA=1
      else
         echo2 "$BNAME: FG data skipped."
         ALL_SKIP="$ALL_SKIP FGDATA"
      fi
   fi
   if [ "$FGUPDDATA" = "1" ]; then
      echo2 "$BNAME: To repeat..."
      show_scripts
   fi

   cd ..
else
   echo2 "$BN: FG data skipped."
   ALL_SKIP="$ALL_SKIP FGDATA"
fi

echo2 "$BNAME: Done [$ALL_DONE], and the output log is [$OUTFILE]"
echo2 "$BNAME: Items skipped = [$ALL_SKIP]"
# at end, write the version to $FG_VERS_TXT
echo -n "$SCVERSION" > $FG_VERS_TXT
echo -n "$SCDATE" > $FG_DATE_TXT
#echo -n "$OSG_SOURCE_DIR" > $OSG_VERS_TXT
echo -n "$INSTALL_DIR_OSG" > $OSG_VERS_TXT
# wait_for_input "$BNAME: Update CONF file $MAKEFGCFG?"
update_conf_file
output_the_time
exit 0

# eof - make?g
