#!/bin/sh

#-------------------------------------------------------------------------------

saveFile ()
{
    fileToBackup=$1
    whereToSave=$2
    
    #----------------------------------------------
    # Make backup copy of user's file for safety
    # Don't overwrite backup, if it already exists.
    #----------------------------------------------
    if [ ! -f $whereToSave ]; then
        if [ -f "$fileToBackup" ]; then
            echo "Saving copy of $fileToBackup in $whereToSave"
            cp -p $fileToBackup $whereToSave
        fi
    fi
}

#-------------------------------------------------------------------------------

getExpanderDirectory ()
{
    expDir=`echo $0|grep /`
    
    if [ "$expDir" = "" ]; then
        expDir=`pwd`         # in the directory already
    else
        expDir=`dirname $0`  # executing from elsewhere
    fi
}

#-------------------------------------------------------------------------------
# Have to generate this one because csh is too dumb to know where it lives.

generate_csh_environ ()
{
    getExpanderDirectory
    ce=$expDir/exp_environ.csh
    
    echo "#!/bin/csh" > $ce
    echo "" >> $ce
    echo "#---------------------" >> $ce
    echo "# Required definitions" >> $ce
    echo "#---------------------" >> $ce
    echo "setenv EXPANDER_DIR $expDir"           >> $ce
    echo "setenv EXPANDER_DEF $expDir/defs"      >> $ce
    echo "setenv EXPANDER_TPL $expDir/templates" >> $ce
    echo "setenv EXP_DEFINITIONS   \$EXPANDER_DEF/definitions" >> $ce
    echo "setenv EXP_NO_AUTOFIELD  \"yes\""      >> $ce
    echo "" >> $ce
    echo "#------------------------------------------------ Default values           " >> $ce
    echo '# setenv EXP_DATE_FORMAT   "%b %d, %Y"          # eg. Nov 28, 2000         ' >> $ce
    echo '# setenv EXP_TIME_FORMAT   "%H:%M:%S"           # eg. 15:02:40             ' >> $ce
    echo '# setenv EXP_DTIME_FORMAT  "%b %d, %Y %H:%M:%S" # eg. Nov 28, 2000 15:02:52' >> $ce
    echo '# setenv EXP_INDENT_SIZE   4                                               ' >> $ce
    echo '# setenv EXP_USE_TABS      0                                               ' >> $ce
    echo '# setenv EXP_KEYWORD_DELIM " \\t"                                          ' >> $ce
    echo '# setenv EXP_EXTENT        ".def"               # only used by expander.nm ' >> $ce
    echo '# setenv EXP_USER          "<yourName>"         # $LOGNAME, $USER          ' >> $ce
    echo "#------------------------------------------------                          " >> $ce
    echo "" >> $ce
    chmod 755 $ce
}

#-------------------------------------------------------------------------------

binLocation=${1:-$BINDIR}
dotnm_bkup="$HOME/neditmacro_B4installExpander"
dotn_bkup="$HOME/nedit_B4installExpander"

#-------------------------------
# Validate installation process.
#-------------------------------
if [ "$binLocation" = "" ]; then
    echo ""
    echo "Usage: build directory [no_strtok_r]"
    echo ""
    echo "  Please either supply the location to where the expander"
    echo "  package executables are to reside as the first parameter"
    echo "  to the build program, or define that location in"
    echo "  the environment variable BINDIR (eg. \$HOME/bin)."
    echo ""
    echo "  If you receive compiler messages indicating that"
    echo "  the function strtok_r is being redefined, append"
    echo "  a second argument onto the build line, such as"
    echo "  'build \$HOME/bin no_strtok_r', which tells the"
    echo "  build process not to include the internal definition"
    echo "  of the strtok_r function, because the local system"
    echo "  has one of its own."
    echo ""
else
    #-----------------------------------------------------------
    # Create package destination directory if it does not exist.
    #-----------------------------------------------------------
    if [ ! -d $binLocation ]; then
        mkdir -p $binLocation
    fi
    
    saveFile $HOME/.nedit      $dotn_bkup
    saveFile $HOME/.neditmacro $dotnm_bkup
    
    #---------------------------------------------------------------
    # When user does not indicate that the local system
    # supports strtok_r, then include this packages internal one.
    #---------------------------------------------------------------
    if [ "$2" = "" ]; then
        cflags=-DNEED_STRTOK_R
    fi
    
    make BINDIR=$binLocation CFLAGS=$cflags
    generate_csh_environ
    
    #---------------------
    # For cleanup purposes
    #---------------------
    echo "binLocation=$binLocation" > $expDir/.binLocation
    chmod 755 $expDir/.binLocation
fi
