#!/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
# 01/01/99 Version 0.2.4
banner="$(src/sabre --print-banner)"
DLG=./dialogsrc/dialog
FOO=.tmp
MODE="-mode 0"
RZ_STEPS=
SABRE="-mode $MODE" #default flight parameters
DEMO=
if [ -f "rs.sh" ]
  then
    . rs.sh ;
else
    YOKE="-ms"
    RDDR="-kyr"
    THRT="-kyt"
    WINDOW="-window 320 200"
fi

# 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 $WINDOW" >> loadlog
 
 if [ -x $path ]
  then
   $DLG \
   --backtitle "$banner" \
   --title "Loading $1 $2" \
   --infobox  "Scenario coming up: Please standby!" \
   3 44
   export SABRE
   export SABRE_BIN=./src/sabre
   $path $DEMO $MODE $WINDOW $YOKE $RDDR $THRT > sabre.log
    stty sane
   $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 
}


function sabre_controls() {
 until [ "foo" = "bar" ];
 do
  $DLG \
  --backtitle "$banner" \
  --title "Control Setup" \
  --menu  "Select Control To Setup" \
  14 56 5 \
  "Yoke"  "Yoke Control" \
  "Throttle"  "Throttle Control" \
  "Rudder"  "Rudder Control" \
  "Calibrate 0" "Calibrate Joystick 0" \
  "Calibrate 1" "Calibrate Joystick 1" \
  2> $FOO
  choice=$(cat $FOO)
  case "$choice" in
   "Yoke") sabre_yoke;;
   "Throttle") sabre_throttle;;
   "Rudder") sabre_rudder;;
   "Calibrate 0") WHICHJOY=0;sabre_calibrate;;
   "Calibrate 1") WHICHJOY=1;sabre_calibrate;;
   "")  return;;
  esac
  done
}

function sabre_calibrate
{
    cp ./lib/joy$WHICHJOY.def ./lib/joy$WHICHJOY,sav
    cd joystick
    rm joy$WHICHJOY.def
    sabrejoycal $WHICHJOY
    cp joy$WHICHJOY.def ../lib/.
    cd ..
}

function sabre_yoke() {
 #clear all flags
 ST_JOY0=off
 ST_MS=off
 ST_KY=off
  # find which are on
  for param in $YOKE;
  do
   case $param in
    "-j0" ) ST_JOY0=on ;;
    "-kys" ) ST_KY=on ;;
    "-ms" ) ST_MS=on ;;
   esac
  done
  
  $DLG \
  --backtitle "$banner" \
  --title "Control Setup" \
  --radiolist  "Select Yoke Control Device" \
  10 66 3 \
  "-j0"       "Joystick" "$ST_JOY0" \
  "-ms"       "Mouse" "$ST_MS" \
  "-ks"       "Keyboard" "$ST_KY" \
  2> $FOO
  if [ $? = 0 ]
   then
    YOKE=$(sed 's/\"//g' < $FOO)
  fi
}

# sabre_setup() 
# default flight parameters
function sabre_rudder() {
 #clear all flags
 ST_JOY2=off
 ST_MR=off
 ST_KYR=off
  # find which are on
  for param in $RDDR;
  do
   case $param in
    "-j2" ) ST_JOY2=on ;;
    "-kyr" ) ST_KYR=on ;;
    "-mr" ) ST_MR=on ;;
   esac
  done
  
  $DLG \
  --backtitle "$banner" \
  --title "Control Setup" \
  --radiolist  "Select Rudder Control Device" \
  10 66 3 \
  "-j2"       "Rudder Pedals" "$ST_JOY2" \
  "-kyr"      "Keyboard" "$ST_KYR" \
  "-mr"       "Mouse Rudder" "$ST_MR" \
  2> $FOO
  if [ $? = 0 ]
   then
    RDDR=$(sed 's/\"//g' < $FOO)
  fi
}

# sabre_setup() 
# default flight parameters
function sabre_throttle() {
 #clear all flags
 ST_JOY1=off
 ST_MT=off
 ST_KYT=off
  # find which are on
  for param in $THRT;
  do
   case $param in
    "-j1" ) ST_JOY1=on ;;
    "-mt" ) ST_MT=on ;;
    "-kyt" ) ST_KYT=on ;;
   esac
  done
  
  $DLG \
  --backtitle "$banner" \
  --title "Control Setup" \
  --radiolist  "Select Throttle Control Device" \
  10 66 3 \
  "-j1"       "Throttle Wheel" "$ST_JOY1" \
  "-mt"       "Mouse Throttle" "$ST_MT" \
  "-kyt"       "Keyboard" "$ST_KYT" \
  2> $FOO
  if [ $? = 0 ]
   then
    THRT=$(sed 's/\"//g' < $FOO)
  fi
}

# write out saved stuff
function runsabre_save() 
{
    echo YOKE=\"$YOKE\" > rs.sh  ;
    echo RDDR=\"$RDDR\" >> rs.sh ;
    echo THRT=\"$THRT\" >> rs.sh ;
    echo WINDOW=\"$WINDOW\" >> rs.sh;
    chmod a+x rs.sh;
    rm -f $FOO; 
    exit;
}

# 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" \
 "controls"        "Select Flight Controls" \
 "quit"            "EJECT! EJECT! EJECT!" \
 2> $FOO
 choice=$(cat $FOO)	       
 case "$choice" in
  "missions")      sabre_missions;;
  "demos")         sabre_demos;;
  "controls")      sabre_controls;;
  "svgamode")      sabre_vgamodes;;
  "window")        sabre_vgawindow;;
  "documentation") sabre_documentation;;
  "quit" | "")     runsabre_save;;
 esac
done
# the end








