#! /bin/csh -f
#
# vthought - Script for running Visual Thought.
#
# Version 1.3
#
# Post:
#    status - return status:
#      0 = success
#      cmd_init:
#        10 = system not recognized
#        11 = vthoughtx command not available
#
# This routine uses the following utility scripts:
#   cmd_init - determine system directories and check for command existence
#
# Copyright (c) 1994 by
#
#    Confluent, Inc.
#    400 Spear St., Suite 207
#    San Francisco, CA 94105
#
# Voice: 415-764-1000
# Fax: 415-764-1008
# Internet: info@confluent.com
#
# Created: 940422 - wpt
# Modified: 940526 - jag : handle colors, add preference configuration
# Modified: 940802 - jag : unapply X resource color settings
# Modified: 940907 - wpt : system, OS, DISPLAY checks
# Modified: 940917 - jag : put configuration at the beginning
# Modified: 950104 - jag : move to share/bin, use cmd_init, add printcmd
#                          configuration, protect multi-word arguments
# Modified: 950302 - jag : use foreach($argv:q), add font configuration
# Modified: 950317 - jag : set LC_NUMERIC=C
# Modified: 950421 - jag : discard limit command output in case of error (HP-UX)
# Modified: 950620 - jag : remove set of LC_NUMERIC
# Modified: 950801 - jag : add openwindows preference
# Modified: 950808 - jag : check for available color cells with chkcolor
# Modified: 950823 - jag : add required bin directories to PATH
# Modified: 950911 - jag : open Z_INITIAL.vthought if .vgalaxy.1.vr missing
# Modified: 951015 - jag : check for available home directory
# Modified: 970304 - jag : remove open of Z_INITIAL.vthought and warning
#                          regarding colormaps with unequal sides

# Preference configuration.
# Set the following variables to 1/0 to enable/disable addition of command
# line preferences left unspecified by the user:
#
#   set_laf  : look-and-feel (default: motif)
#   set_bg   : background color (default: none)
#              This overrides the X resource vthoughtx.background
#   set_fg   : foreground color (default: none)
#              This overrides the X resource vthoughtx.foreground
#   set_pr   : print command (default: lp)
#              This is only set if lpr is not found in the search path
#   set_font : font (default: helvetica)
set set_laf = 1
set set_bg = 1
set set_fg = 1
set set_pr = 1
set set_font = 1

# Set the product installation directory.
# This line is configured during installation.
set top_dir = "vt-1.4"

# Get the operating system head of directory paths and check for existence
# of vthoughtx.
set cmd_exec = "vthoughtx"
source $top_dir/share/bin/cmd_init

# Quit if errors occurred.
if ($cmd_status) exit ($cmd_status)

# Determine if the home directory is available.
# If not, warn the user.
set home_ok = 1
if ("$home" == "" || ! -d "$home") then
  set home_ok = 0

  echo ""
  echo "Warning: Your HOME environment variable is not set to a valid directory."
  echo "Until this is corrected, your preferences (e.g., default colors in"
  echo "the color chooser) may not be available."
  echo ""
endif

# If the home directory is available and the user does not have a preference
# resource file, copy one in.
if ($home_ok) then
  set prefs = .vgalaxy.1.vr
  if (! -e $home/$prefs) then
    cp $lib_dir/$prefs $home
  endif
endif

# See if any recognized arguments were passed in.
# Disable setting of any preferences already specified by the user.
foreach word ($argv:q)
  if ("$word" == "-laf") set set_laf = 0
  if (("$word" == "-bg") || ("$word" == "-background")) set set_bg = 0
  if (("$word" == "-fg") || ("$word" == "-foreground")) set set_fg = 0
  if ("$word" == "-printcmd") set set_pr = 0
  if ("$word" == "-font") set set_font = 0
end

# Set the laf preference to motif if left unspecified by the user.
set laf = ""
if ($set_laf) set laf = "-laf motif"

# Set the background preference to none if left unspecified by the user.
# This unapplies a background color set by the X resource.
set bg = ""
if ($set_bg) set bg = "-background none -bg none"

# Set the foreground preference to none if left unspecified by the user.
# This unapplies a foreground color set by the X resource.
set fg = ""
if ($set_fg) set fg = "-foreground none -fg none"

# Set the printcmd preference to lp if lpr is not in the search path.
set pr = ""
if ($set_pr) then
  set found = 0
  foreach dir ($path)
    if (-x $dir/lpr) set found = 1
  end
  if (! $found) set pr = "-printcmd lp"
endif

# Set the font preference to helvetica if left unspecified by the user.
set font = ""
if ($set_font) set font = "-font helvetica"

# Set the openwindows preference if xrdb sees any OpenWindows properties.
set ow = ""
if (`xrdb -query | grep OpenWindows | wc -l` > 0) set ow = "-openwindows"

# Add the bin directories for all required products to the command search path.
set ci_exit = 0
source $top_dir/share/config/product.dat
foreach id ($ci_required)
  set dir = $top_dir:h/$id
  set path = ($path $dir/share/bin $dir/$cmd_oshead/bin)
end

# Set the coredumpsize to zero.
# Ignore error messages if the command does not exist.
limit coredumpsize 0 >& /dev/null

# Warn if the DISPLAY environment variable is not set.
if (! $?DISPLAY) then
  echo ""
  echo "Warning: Your DISPLAY environment variable is not set."
  echo "Visual Thought will still run, but various X-based tools,"
  echo "such as the screen grabber (xgrabsc), will not."
  echo ""
endif

# Check for available color cells.
# Add the command arguments in case "-display" was specified.
# Specify the "-help" argument to obtain usage information.
chkcolor $argv:q
set cstatus = $status
set cfix = ""

# If insufficient color cells are available, issue a warning and add the
# sharedcolormax preference.
if ($cstatus == "3") then
  echo ""
  echo "Warning: There is no standard colormap and insufficient color cells"
  echo "are available to create one.  A private colormap will be used,"
  echo "causing color flashing when the window focus is changed.  For more"
  echo "information and instructions to fix this problem, see the file"
  echo "doc/note_colormap.txt in the Visual Thought installation directory."
  echo ""
  set cfix = "-sharedcolormax 0"
endif

# Exec the program.  Can't background it because then we lose stderr.
exec $bin_dir/$cmd_exec $laf $bg $fg $pr $font $ow $argv:q $cfix
