#!/bin/sh

# The name of the file within the stic-scripts directory 
keyset_file=simv_keyset

# The name of the Tck/Tk script within stic-scripts to serve as frontend
frontend_file=simv_frontend.tcltk

#
# The target lists for simv_frontend.tcltk and its descendents :
#
# If ${target_list} is not empty and any of ${target_list}_[1-4] exists,
# then it will be displayed as list in the frontend's target box.
# So, up to four lists may appear and it is perfectly ok if a list file
# appears more than once.
# If ${target_list} exists it will be displayed as ${target_list}_[1-3]
# but may be overridden by the actual existence of those three files.
# If at least one target list is found, there will be no button menu
# .payload unless the initial setting of variable  what_to_show  in
# the frontend is changed.
#
# This is for a filename within the stic-scripts directory
target_list=image_targets

# The name of the file within the stic-scripts directory which contains 
# the address of the trash directory. If it is empty or does not exist
# then the trash directory from $keyset_file remains in effect.
trash_target_file=image_trash


# --------- these full-path file locations may override above settings

# If a name outside the stic-scripts directory is desired, set this variable
# In that case $keyset_file will be ignored
keyset_adr=

# for an arbitrary file location. see above keyset_adr
frontend_adr=

# for an arbitrary base address of target lists. see above target_list
target_list_adr=

# for an arbitrary file location. see above trash_target_file
trash_target_adr=


# the interpreter for the frontend script
frontend_interpreter=wish


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

# determine the installation directory of stic_std_variables and execute inline
if test -n "$STIC_MAIN_DIR"
then
  stic_init_dir=$STIC_MAIN_DIR
elif test -r $HOME/.stic_main_dir
then
  stic_init_dir=$(cat $HOME/.stic_main_dir)
fi
if test "$stic_init_dir" = ""
then
  stic_init_dir=$(dirname "$0")/
else
  stic_init_dir="$stic_init_dir"/scripts/
fi
if test -r "$stic_init_dir"stic_std_variables
then
  . "$stic_init_dir"stic_std_variables
elif test  -z "$STIC_NO_BASH" -a -n "$(type -p stic_std_variables)"
then
  . stic_std_variables
fi


remove_pipe () {
  #just in case it remains open
  if test -e "$pipename"
  then
    rm "$pipename"
  fi
  return 0
}

remove_gdbfile () {
  #just in case it remains open
  if test -e "$gdbfile"
  then
    rm "$gdbfile"
  fi
  return 0
}

final_procedure() {
  remove_pipe
}


# --- main

start_debugger=0
export pipename=silly_name_that_most_probably_does_not_exist__i_hope
export gdbfile=silly_name_that_most_probably_does_not_exist__i_hope


if test "$1" = "-help" -o "$1" = "-h" -o $# -lt 2
then
  echo usage : $0 '[-tcltk]' 'file(s)' 
  echo starts STIC simv optionally with Tcl/Tk frontend
  echo please note that -tcltk has to be the first argument if ever
  exit 1
fi


trap final_procedure EXIT

# evaluate options for simv
if test -n "$stic_scripts_dir"
then
  scriptdir=$stic_scripts_dir
else
  scriptdir=$(dirname $0)/
fi
if test -n "$stic_bin_dir"
then
  progdir=$stic_bin_dir
else
  progdir=$(dirname $0)/
fi
if test -z "$keyset_adr"
then
  keyset_adr="${scriptdir}${keyset_file}"
fi
if test -z "$target_list_adr"
then
  if test -n "$target_list"
  then
    target_list_adr="${scriptdir}${target_list}"
  fi
fi

keyset="-keyset:readfrom:$keyset_adr"
if test -z "$trash_target_adr"
then
  trash_target_adr="${scriptdir}${trash_target_file}"
fi
if test -n "$trash_target_adr"
then
  if test -s "$trash_target_adr"
  then
    keyset="$keyset -keyset:trashdir:$(cat $trash_target_adr)"
  fi
fi

if test -z "$frontend_adr"
then
  frontend_adr="${scriptdir}${frontend_file}"
fi

pipename="/tmp/stic_pipe_$$"
ipc_services=""
frontend=""
if test "$1" = "-tcltk"
then
  shift 1
  ipc_services="-pipe_service:2:$pipename"
# about this, see above explanation at initialization of variable target_list
  tadr1=
  tadr2=
  tadr3=
  tadr4=
  if test -n "$target_list_adr"
  then
    if test -s "$target_list_adr"
    then
      tadr1="$target_list_adr"
      tadr2="$target_list_adr"
      tadr3="$target_list_adr"
    fi
    if test -s "${target_list_adr}_1"
    then
      tadr1="${target_list_adr}_1"
    fi
    if test -s "${target_list_adr}_2"
    then
      tadr2="${target_list_adr}_2"
    fi
    if test -s "${target_list_adr}_3"
    then
      tadr3="${target_list_adr}_3"
    fi
    if test -s "${target_list_adr}_4"
    then
      tadr4="${target_list_adr}_4"
    fi
  fi

  echo
  echo "target_list_adr=$target_list_adr"
  echo "tadr= $tadr1 : $tadr2 : $tadr3 : $tadr4"
  echo

  frontend="-shell:${frontend_interpreter} ${frontend_adr} $pipename $tadr1 $tadr2 $tadr3 $tadr4 -geometry -12+0 &"
fi
misc_opt="-end_on:list_end"


echo  
echo $ipc_services "$frontend"
echo  

if test "$start_debugger" = 1
then

  options="$keyset $ipc_services $misc_opt"
  gdbfile=/tmp/stic_start_gdb_$$
  echo debugger file : $gdbfile
  rm $gdbfile
# do not change the number of bytes in each line while this script is running
# use one of the "# echo b" lines below to add start commands for gdb .
  echo b   Smem_protest                                             >>$gdbfile
# echo b   smem.c:189                                               >>$gdbfile
# echo b   smem.c:210                                               >>$gdbfile
# echo b   smem.c:215                                               >>$gdbfile
# echo b   sregex.c:354                                             >>$gdbfile
# echo b imvapp.c:1355                                              >>$gdbfile
  echo run "$options" "$@" >>$gdbfile
  gdb ${progdir}simv -x $gdbfile
  rm $gdbfile

else

  if test -n "$frontend"
  then
    ${progdir}simv $keyset $ipc_services "$frontend" $misc_opt "$@"
  else
    ${progdir}simv $keyset $ipc_services $misc_opt "$@"
  fi

fi



