#!/bin/sh
################################################################################
#                                                                              #
# tgdb installation script						       #
# (c) 1994 HighTec EDV-Systeme GmbH                                            #
#                                                                              #
################################################################################
set -e

#-------------------------------------------------------------------------------
# Build mkxtty and callcld executables.
# Note: there's no makefile necessary, since make's default rules should
# be sufficient to build the programs, but you may change the following
# lines to reflect your personal taste (e.g. 'make "CC=gcc" ...').
#-------------------------------------------------------------------------------
make mkxtty
make callcld

#-------------------------------------------------------------------------------
# User-changeable variables which control tgdb's behaviour:
#-------------------------------------------------------------------------------
# TGDB_DEBUGGER is the gdb that tgdb will use; gdb's version should be > 4.10
#    Note: you must only specify the file name (not the complete path)!
# TGDB_PROMPT is the prompt that the above mentioned gdb ($TGDB_DEBUGGER) uses
# TGDB_PATH points to the directory where all tgdb related files (binaries,
#    scripts and icons) reside
# TGDB_APPDEF_PATH points to the directory where tgdb's X application defaults
#    live (Tgdb and Tgdb-small)
# TGDB_SMALLICONS=1 directs tgdb to use 16x16 icons and the application defaults
#    found in $TGDB_APPDEF_PATH/Tgdb-small
# TGDB_EXEC_PATH points to the directory where the tgdb startup script and
#    tgdb_wish should be copied to
#-------------------------------------------------------------------------------
# NOTE: If you really want to change the default values for the various
#       directories below, make sure to always specify absolute path names
#       (i.e. path names starting with a "/", *not* with a ".")!
#-------------------------------------------------------------------------------
tgdb_debugger=gdb
tgdb_prompt="($tgdb_debugger) "
tgdb_path=`pwd`
tgdb_appdef_path=`pwd`
tgdb_smallicons=0
tgdb_exec_path=`pwd`

#-------------------------------------------------------------------------------
# End of user-changeable values; don't modify the lines below!
#-------------------------------------------------------------------------------

if [ ! -d $tgdb_path ]; then
  echo "Making directory \"$tgdb_path\"."
  mkdir -p $tgdb_path || true
fi
if [ ! -d $tgdb_exec_path ]; then
  echo "Making directory \"$tgdb_exec_path\"."
  mkdir -p $tgdb_exec_path || true
fi
if [ ! -d $tgdb_appdef_path ]; then
  echo "Making directory \"$tgdb_appdef_path\"."
  mkdir -p $tgdb_appdef_path || true
fi
echo "Creating tgdb startup script..."
xtgdb_debugger=`echo $tgdb_debugger | sed -e 's/\//\\\\\//g'`
xtgdb_path=`echo $tgdb_path | sed -e 's/\//\\\\\//g'`
xtgdb_appdef_path=`echo $tgdb_appdef_path | sed -e 's/\//\\\\\//g'`
if [ "$tgdb_prompt" != "($tgdb_debugger) " ]; then
  sed -e "s/\\(TGDB_DEBUGGER\\):=/\\1=$xtgdb_debugger/" \
      -e "s/\\(TGDB_PROMPT\\):=[^;]*/\\1=\\\"$tgdb_prompt\\\"/" \
      -e "s/\\(TGDB_PATH\\):=/\\1=$xtgdb_path/" \
      -e "s/\\(TGDB_APPDEF_PATH\\):=/\\1=$xtgdb_appdef_path/" \
      -e "s/\\(TGDB_SMALLICONS\\):=/\\1=$tgdb_smallicons/" \
      tgdb.in > tgdb
else
  sed -e "s/\\(TGDB_DEBUGGER\\):=/\\1=$xtgdb_debugger/" \
      -e "s/\\(TGDB_PROMPT\\):=[^;]*/\\1=\\\"(\$TGDB_DEBUGGER) \\\"/" \
      -e "s/\\(TGDB_PATH\\):=/\\1=$xtgdb_path/" \
      -e "s/\\(TGDB_APPDEF_PATH\\):=/\\1=$xtgdb_appdef_path/" \
      -e "s/\\(TGDB_SMALLICONS\\):=/\\1=$tgdb_smallicons/" \
      tgdb.in > tgdb
fi
chmod 755 tgdb

if [ "$tgdb_exec_path" != "`pwd`" ]; then
  echo "Copying tgdb executables to \"$tgdb_exec_path\"..."
  cp tgdb tgdb-wish $tgdb_exec_path
  rm -f tgdb tgdb_wish
fi
if [ "$tgdb_appdef_path" != "`pwd`" ]; then
  echo "Copying application defaults to \"$tgdb_appdef_path\"..."
  cp Tgdb Tgdb-small $tgdb_appdef_path
fi
if [ "$tgdb_path" != "`pwd`" ]; then
  echo "Copying tgdb files to \"$tgdb_path\"..."
  rcp -r `pwd` $tgdb_path
  if [ "$tgdb_exec_path" != "$tgdb_path" ]; then
    rm -f $tgdb_path/tgdb $tgdb_path/tgdb_wish
  fi
  if [ "$tgdb_appdef_path" != "$tdg_path" ]; then
    rm -f $tgdb_path/Tgdb $tgdb_path/Tgdb-small
  fi
fi

echo ""
echo "********************************"
echo "* tgdb successfully installed. *"
echo "********************************"
echo ""
add_path() {
  IFS=":"; set $PATH
  for dir in "$@"; do [ "$dir" = "$tgdb_exec_path" ] && return 1; done
  return 0
}
if add_path; then
  echo "==> Don't forget to add the directory \"$tgdb_exec_path\""
  echo "==> to the PATH environment variable!"
  echo ""
fi

### EOF ########################################################################
