#!/bin/sh
#   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 1, 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, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# "frontend" for SABRE flight simulator
# dialog based version
# Author: Antti Barck 9/2/97
#         proff@iki.fi
# strings attached
# DH Version 0.2.3
# Hey Antti ... cool script!
# 12/19/97 Now using GDEV and "window"
# for high-res SABRE
# 04/10/98 Version 0.2.3 patch
banner="$(src/sabre --print-banner)"
DLG=dialogsrc/dialog
FOO=.tmp
MODE="-mode 0"
SABRE="-mode $MODE" #default flight parameters
OPTS=
DEMO=
WINDOW="-window 320 200"

# fatal( context, error, source )
# display errorbox
function fatal ()
{
  errname="Unknown error"
  case $2 in
   "EINV" ) errname="Invalid argument" ;;
   "ENOENT" ) errname="No such file or directory" ;;
   "EACCES" ) errname="Permission denied" ;;
  esac 
  
  $DLG \
  --backtitle "$banner" \
  --title "Error during $1" \
  --msgbox \
  "$3: $errname" 5 55
}

# view_file( file )
# display file on textbox
function view_file()
{
 if [ -r $1 ]
  then 
   $DLG \
   --backtitle "$banner" \
   --title "$1" \
   --textbox \
   $1 21 76
  else
   if [ -e $1 ]
    then
     fatal viewing EACCES $1
    else
     fatal viewing ENOENT $1
   fi
 fi
}

# load_scenario( file, type )
# load mission or demo
function load_scenario()
{
 path=scenarios/$1
 echo "$1 $2 = $path $MODE $OPTS $WINDOW" >> loadlog
 
 if [ -x $path ]
  then
   $DLG \
   --backtitle "$banner" \
   --title "Loading $1 $2" \
   --infobox  "Scenario coming up: Please standby!" \
   3 44
   export SABRE
   $path $DEMO $MODE $OPTS $WINDOW > sabre.log
   $DLG \
   --backtitle "$banner" \
   --title "Flight session over!" \
   --yesno "Do you want to see the log?" 5 44
   if [ $? = 0 ]
    then
     view_file sabre.log
   fi
  else
   if [ -e $1 ]
    then
     fatal loading EACCES $1
    else
     fatal loading ENOENT $1
   fi
 fi  
}

# sabre_documentation()
# browse documentation
function sabre_documentation() {
 looping=1
 until [ "$looping" = "0" ];
 do
  $DLG \
  --backtitle "$banner" \
  --title "Documentation" \
  --menu  "Miscellaneous SABRE papers" \
  21 68 15 \
  "WHATSNEW"        "news on latest" \
  "intro"          "introduction to the game" \
  "keys"           "keys used" \
  "takeoff"        "takeoff mission" \
  "shoot"          "shoot missions" \
  "gru"            "NEW! ground attack missions" \
  "COPYING"        "GNU General Public Licence" \
  "CONTRIBUTORS"   "people behind this game" \
  "README"         "general background information" \
  "REQUIREMENTS"   "the gear needed" \
  "INSTALL"        "building SABRE up" \
  "TODO"           "future plans and fixes" \
  "TROUBLE_SHOOTING" "some noted problems and fixes" \
  "JOYSTICK.README" "info on using a joystick" \
   "HISTORY"       "the SABRE saga" \
   2> $FOO
   
  choice=$(cat $FOO)	       
  case "$choice" in
   "intro" | \
   "keys" | \
   "takeoff" | \
   "gru" | \
   "shoot") view_file doc/$choice.txt ;;
   "HISTORY"      | \
   "README"       | \
   "CONTRIBUTORS" | \
   "COPYING"      | \
   "INSTALL" | \
   "README" | \
   "REQUIREMENTS" | \
   "WHATSNEW" | \
   "JOYSTICK.README" | \
   "TROUBLE_SHOOTING" | \
   "TODO" ) view_file $choice ;;
   "User Interface" ) view_file dialogsrc/README ;;
   "")  looping=0;;
  esac
 done 
}

# sabre_missions()
# browse missions
function sabre_missions() {
 DEMO=
 looping=1
 until [ "$looping" = "0" ];
 do
  $DLG \
  --backtitle "$banner" \
  --title "Missions" \
  --menu  "Various flight missions" \
  21 68 15 \
  "fly"           "Just fly around (no crash)" \
  "takeoff"       "Takeoff from runway" \
  "shoot1"        "Just squeeze the trigger" \
  "shoot2"        "(another angle to previous)" \
  "shoot3"        "(yet another )" \
  "dogfight"      "Dance with viscious MIG-15" \
  "furball"       "Bumper-to-Bumper Air Combat" \
  "melee"         "Five on five!" \
  "migjump"       "Ride with Commander Jing" \
  "yakattak"      "Mustang vs Yak race" \
  "thunder"       "F84 Thunderjets vs MiG-15s" \
  "pistons"       "The Last of the Piston-Engines" \
  "gru"           "F84 Ground Attack Mission" \
  "gru2"          "F86 Ground Attack Mission" \
  "gru3"          "F86 Ground Attack Mission" \
  2> $FOO
  choice=$(cat $FOO)	       
  #
  # is it so? forget the case control and just call 
  # load_scenario $choice mission
  #
  case "$choice" in
  "fly" | \
  "takeoff" | \
  "shoot1" | \
  "shoot2" | \
  "shoot3" | \
  "dogfight" | \
  "furball" | \
  "melee" | \
  "migjump" | \
  "thunder" | \
  "yakattak" | \
  "gru" | \
  "gru2" | \
  "gru3" | \
  "pistons") load_scenario $choice mission; continue ;;
  "")  return ;;
  esac
  fatal missions EINV $choice
 done 
}

# sabre_demos() 
# browse demos
function sabre_demos() {
 DEMO=-demo
 until [ "foo" = "bar" ];
 do
  $DLG \
  --backtitle "$banner" \
  --title "Demos" \
  --menu  "Some flight demos" \
  14 56 6 \
  "furball"  "Bumper-to-Bumper Air Combat" \
  "melee"    "Five on five!" \
  "migjump"  "Ride with Commander Jing" \
  "yakattak" "Mustang for Yak race" \
  "thunder"  "F84 Thunderjets vs MiG-15s" \
  "pistons"  "The Last of the Piston-Engines" \
  2> $FOO
  choice=$(cat $FOO)
  #
  # is it so? forget the case control and just call 
  # load_scenario $choice demo
  #
  case "$choice" in
  "foo" | \
  "furball" | \
  "melee" | \
  "migjump" | \
  "pistons" | \
  "thunder" | \
  "yakattak") load_scenario $choice demo; continue  ;;
   "")  return;;
  esac
  fatal missions EINV $choice
 done 
}

# sabre_vgamodes() 
# Allow choosing of the vga mode
function sabre_vgamodes() 
{
  $DLG \
  --backtitle "$banner" \
  --title "VGA Modes" \
  --menu  "SVGALIB Modes" \
  12 56 4 \
  "G320X200X256"  "" \
  "G640X480X256"  "" \
  "G800X600X256"  "" \
  "G1024X768X256" "" \
  2> $FOO
  choice=$(cat $FOO)
  case "$choice" in
   "G320X200X256") MODE=0 ;;
   "G640X480X256") MODE=1 ;; 
   "G800X600X256") MODE=2 ;; 
   "G1024X768X256") MODE=3 ;;
   "")  return;;
  esac
  MODE="-mode $MODE"
}

# sabre_window 
# Allow choosing of the "window"
# Enter your own preferred
# window sizes, if you so
# desire ... multiples of
# 16 for the width are
# recommended. It's also best
# to keep a 3:2 width:height 
# proportion
function sabre_vgawindow() {
# until [ "foo" = "bar" ];
# do
  $DLG \
  --backtitle "$banner" \
  --title "Window Size" \
  --menu  "Window Size" \
  16 56 8 \
  "160X100"  "" \
  "320X200"  "" \
  "352X220"  "" \
  "384X240"  "" \
  "400X250"  "" \
  "512X320"  "" \
  "608X380"  "" \
  "Full Screen"     "" \
  2> $FOO
  choice=$(cat $FOO)
  case "$choice" in
   "160X100") WINDOW="-window 160 100" ;;
   "320X200") WINDOW="-window 320 200" ;;
   "352X220") WINDOW="-window 352 220" ;;
   "384X240") WINDOW="-window 384 240" ;;
   "400X250") WINDOW="-window 400 250" ;;
   "512X320") WINDOW="-window 512 320" ;;
   "608X380") WINDOW="-window 608 380" ;;
   "Full Screen")    WINDOW= ;;
   "")  return;;
  esac
# done 
}

# sabre_setup() 
# default flight parameters
function sabre_joy() {
 #clear all flags
 ST_JOY0=off
 ST_JOY1=off
  # find which are on
  for param in $OPTS;
  do
   case $param in
    "-j0" ) ST_JOY0=on ;;
    "-j1" ) ST_JOY1=on ;;
   esac
  done
  
  $DLG \
  --backtitle "$banner" \
  --title "Setup" \
  --checklist  "Joystick Setup -- See JOYSTICK.README" \
  9 66 2 \
  "-j0"       "Joystick 0 (Yoke)" "$ST_JOY0" \
  "-j1"       "Joystick 1 (Rudder)" "$ST_JOY1" \
  2> $FOO
  if [ $? = 0 ]
   then
    OPTS=$(sed 's/\"//g' < $FOO)
  fi
}

# the main loop
if [ ! -f $DLG ];
then
    echo "$DLG not found ... trying <dialog>"
    DLG=`which dialog`
fi

if [ ! -f $DLG ];
then
    echo "$DLG not found"
    exit 1;
fi

until [ "foo" = "bar" ];
do
 $DLG \
 --backtitle "$banner" \
 --title "Main" \
 --menu  "Welcome to SABRE - enjoy your flight!" \
 14 50 7 \
 "missions"        "Various Flight Scenarios" \
 "demos"           "Some Flight Demos" \
 "svgamode"        "SVGALIB Video Mode Selection" \
 "window"          "Select Window Size" \
 "documentation"   "Miscellaneous SABRE papers" \
 "joystick"        "Activate Joystick" \
 "quit"            "EJECT! EJECT! EJECT!" \
 2> $FOO
 choice=$(cat $FOO)	       
 case "$choice" in
  "missions")      sabre_missions;;
  "demos")         sabre_demos;;
  "joystick")      sabre_joy;;
  "svgamode")      sabre_vgamodes;;
  "window")        sabre_vgawindow;;
  "documentation") sabre_documentation;;
  "quit" | "") rm -f $FOO; exit;;
 esac
done
# the end








