#!/bin/sh

###############################################################
# wxinstall -- wxWindows installation script.                 #
# Julian Smart, April 1994                                    #
###############################################################

########### Edit these variables for your system. #############

# X include directories
export XINCLUDE
XINCLUDE='-I/aiai/packages/motif1.2.1/motif/include -I/aiai/packages/X.V11R5/include'

# X library directories
export XLIB
XLIB='-L/aiai/packages/motif1.2.1/motif/sun4/lib -L/aiai/packages/X.V11R5/lib'

# wxWindows directory to install to
#WXDIR='/disk/home/project/4/gryphon/jacs/wx'
WXDIR='/disk/home/project/4/gryphon/jacs/wx/tests/install/wx'

# C++ compiler
CC=gcc-2.1

# Target (motif, xview, hp)
GUITARGET=motif

# Archive prefix, dependent on version number
ARCHIVE=wx150i

# Current directory
CURRENTDIR=`pwd`

GUISUFFIX=_motif

if [ "$GUITARGET" = "motif" ]
then
  GUISUFFIX=_motif
fi

if [ "$GUITARGET" = "xview" ]
then
  GUISUFFIX=_ol
fi

if [ "$GUITARGET" = "hp" ]
then
  GUISUFFIX=_hp
fi

install_from_tar()
{
  cd $CURRENTDIR
  if [ -f $ARCHIVE"_1.tar" -a -f $ARCHIVE"_2.tar" ]
  then
    if [ ! -d $WXDIR ]
    then
      echo Making directory $WXDIR
      mkdir $WXDIR
    fi
    if [ ! -d $WXDIR ]
    then
      echo $WXDIR cannot be created!
    else
       echo Untarring $ARCHIVE"_1.tar, " $ARCHIVE"_2.tar."
      echo Please be patient...
      cd $WXDIR
      tar xf $CURRENTDIR/$ARCHIVE"_1.tar"
      tar xf $CURRENTDIR/$ARCHIVE"_2.tar"
      echo If you saw no errors, untarring was successful.
    fi
  else
    echo $ARCHIVE"_*.tar" not found.
  fi
}

install_from_z()
{
  cd $CURRENTDIR
  if [ -f $ARCHIVE"_1.tar.Z" -a -f $ARCHIVE"_2.tar.Z" ]
  then
    echo Uncompressing $ARCHIVE"_1.tar.Z, " $ARCHIVE"_2.tar.Z"
    uncompress $ARCHIVE"_1.tar.Z"
    uncompress $ARCHIVE"_2.tar.Z"
    install_from_tar
  else
    echo $ARCHIVE"_*.tar.Z" not found. Please place in the current directory.
  fi
}

install_from_gzip()
{
  cd $CURRENTDIR
  if [ -f $ARCHIVE"_1.tgz" -a -f $ARCHIVE"_2.tgz" ]
  then
    echo Uncompressing $ARCHIVE"_1.tgz, " $ARCHIVE"_2.tgz"
    gzip -d $ARCHIVE"_1.tgz"
    gzip -d $ARCHIVE"_2.tgz"
    install_from_tar
  else
    echo $ARCHIVE"_*.tgz" not found. Please place in the current directory.
  fi
}

install_from_zip()
{
  cd $CURRENTDIR
  echo "to be done"
}

install_from_archive()
{
  echo
  echo "Unarchiving Options"
  echo "-------------------"
  echo "1) Install from " $ARCHIVE"_1.tar.Z, " $ARCHIVE"_2.tar.Z"
  echo "2) Install from " $ARCHIVE"_1.tgz, " $ARCHIVE"_2.tgz"
  echo "3) Install from " $ARCHIVE"_1.zip, " $ARCHIVE"_2.zip"
  echo "4) Install from " $ARCHIVE"_1.tar, " $ARCHIVE"_2.tar"
  echo "0) Quit."

  read archans

  if [ "$archans" = "1" ]
  then
    install_from_z
  fi
  if [ "$archans" = "2" ]
  then
    install_from_gzip
  fi
  if [ "$archans" = "3" ]
  then
    install_from_zip
  fi
  if [ "$archans" = "4" ]
  then
    install_from_tar
  fi
  cd $CURRENTDIR
}

make_main_library()
{
  echo
  echo Making the wxWindows main library. It will go in
  echo $WXDIR/lib.
  echo
  echo Please press RETURN to continue, and then make yourself a coffee.
  echo 
  read ans
  cd $WXDIR/src/x
  make -e -f makefile.unx $GUITARGET | tee $CURRENTDIR/make.log
  cd $CURRENTDIR
  if [ -f make.log ]
  then
    more make.log
  fi
  echo
  echo "If you saw no errors, wxWindows was compiled. See the file make.log"
  echo "for errors and warnings."
  echo "Remember, if you compile without using this script, the"
  echo "options in the makefiles have not been permanently changed."
  echo "Use the -e make option to override options with current environment"
  echo "variable values."
}

###############################################################
# Make peripheral components                                  #
###############################################################

make_toolbar()
{
  echo
  echo Making the toolbar library. It will go in
  echo $WXDIR/utils/toolbar/lib.
  echo
  cd $WXDIR/utils/toolbar/src
  make -e -f makefile.unx $GUITARGET | tee $CURRENTDIR/make.log
  cd $CURRENTDIR
  echo
  echo "If you saw no errors, the toolbar library was compiled. See the file make.log"
  echo "for errors and warnings."
}

make_prologio()
{
  if [ ! -d $WXDIR/utils/prologio ]
  then
    echo PROLOGIO directory does not exist.
  else
    echo
    echo Making the PROLOGIO library. It will go in
    echo $WXDIR/utils/prologio/lib.
    echo
    cd $WXDIR/utils/prologio/src
    make -e -f makefile.unx $GUITARGET | tee $CURRENTDIR/make.log
    cd $CURRENTDIR
    echo
    echo "If you saw no errors, the PROLOGIO library was compiled. See the file make.log"
    echo "for errors and warnings."
  fi
}

make_hytext()
{
  if [ ! -d $WXDIR/utils/hytext ]
  then
    echo hyText directory does not exist.
  else
    echo
    echo Making the hyText library. It will go in
    echo $WXDIR/utils/hytext/lib.
    echo
    cd $WXDIR/utils/hytext/src
    make -e -f makefile.unx $GUITARGET | tee $CURRENTDIR/make.log
    cd $CURRENTDIR
    echo
    echo "If you saw no errors, the hyText library was compiled. See the file make.log"
    echo "for errors and warnings."
  fi
}

make_wxtree()
{
  if [ ! -d $WXDIR/utils/wxtree ]
  then
    echo wxTree directory does not exist.
  else
    echo
    echo Making the wxTree library. It will go in
    echo $WXDIR/utils/wxtree/lib.
    echo
    cd $WXDIR/utils/wxtree/src
    make -e -f makefile.unx $GUITARGET | tee $CURRENTDIR/make.log
    cd $CURRENTDIR
    echo
    echo "If you saw no errors, the wxTree library was compiled. See the file make.log"
    echo "for errors and warnings."
  fi
}

make_wxgraph()
{
  if [ ! -d $WXDIR/utils/wxgraph ]
  then
    echo wxGraph directory does not exist.
  else
    echo
    echo Making the wxGraph library. It will go in
    echo $WXDIR/utils/wxgraph/lib.
    echo
    cd $WXDIR/utils/wxgraph/src
    make -e -f makefile.unx $GUITARGET | tee $CURRENTDIR/make.log
    cd $CURRENTDIR
    echo
    echo "If you saw no errors, the wxGraph library was compiled. See the file make.log"
    echo "for errors and warnings."
  fi
}

make_wxhelp()
{
  if [ ! -d $WXDIR/utils/wxhelp ]
  then
    echo wxHelp directory does not exist.
  else
    echo
    echo Making the wxHelp program. It will go in
    echo $WXDIR/utils/wxhelp/src.
    echo
    cd $WXDIR/utils/wxhelp/src
    make -e -f makefile.unx $GUITARGET | tee $CURRENTDIR/make.log
    cd $CURRENTDIR
    echo
    echo "If you saw no errors, the wxHelp program was compiled. See the file make.log"
    echo "for errors and warnings."
  fi
}

make_tex2rtf()
{
  if [ ! -d $WXDIR/utils/tex2rtf ]
  then
    echo Tex2RTF directory does not exist.
  else
    echo
    echo Making the Tex2RTF utility. It will go in
    echo $WXDIR/utils/tex2rtf/src.
    echo
    cd $WXDIR/utils/tex2rtf/src
    make -e -f makefile.unx $GUITARGET | tee $CURRENTDIR/make.log
    cd $CURRENTDIR
    echo
    echo "If you saw no errors, the Tex2RTF program was compiled. See the file make.log"
    echo "for errors and warnings."
  fi
}

###############################################################
# Make demos                                                  #
###############################################################

make_hello()
{
  echo
  echo Making the Hello demo. It will be made as
  echo $WXDIR/samples/hello/hello$GUISUFFIX
  echo
  cd $WXDIR/samples/hello
  make -e -f makefile.unx $GUITARGET 2>&1 | tee $CURRENTDIR/make.log
  cd $CURRENTDIR
  echo
  echo "If you saw no errors, the Hello demo was compiled. See the file make.log"
  echo "for errors and warnings."
}

make_minimal()
{
  echo
  echo Making the Minimal demo. It will be made as
  echo $WXDIR/samples/minimal/minimal$GUISUFFIX
  echo
  cd $WXDIR/samples/minimal
  make -e -f makefile.unx $GUITARGET 2>&1 | tee $CURRENTDIR/make.log
  cd $CURRENTDIR
  echo
  echo "If you saw no errors, the Minimal demo was compiled. See the file make.log"
  echo "for errors and warnings."
}

make_mdi()
{
  echo
  echo Making the Mdi demo. It will be made as
  echo $WXDIR/samples/mdi/mdi$GUISUFFIX
  echo
  cd $WXDIR/samples/mdi
  make -e -f makefile.unx $GUITARGET 2>&1 | tee $CURRENTDIR/make.log
  cd $CURRENTDIR
  echo
  echo "If you saw no errors, the MDI demo was compiled. See the file make.log"
  echo "for errors and warnings."
}

make_form()
{
  echo
  echo Making the Form demo. It will be made as
  echo $WXDIR/samples/form/form$GUISUFFIX
  echo
  cd $WXDIR/samples/form
  make -e -f makefile.unx $GUITARGET 2>&1 | tee $CURRENTDIR/make.log
  cd $CURRENTDIR
  echo
  echo "If you saw no errors, the Form demo was compiled. See the file make.log"
  echo "for errors and warnings."
}

make_ipc()
{
  echo
  echo Making the IPC demo. It will be made as
  echo $WXDIR/samples/ipc/server$GUISUFFIX
  echo $WXDIR/samples/ipc/client$GUISUFFIX
  echo
  cd $WXDIR/samples/ipc
  make -e -f makefile.unx $GUITARGET 2>&1 | tee $CURRENTDIR/make.log
  cd $CURRENTDIR
  echo
  echo "If you saw no errors, the IPC demo was compiled. See the file make.log"
  echo "for errors and warnings."
}

make_all_periph_components()
{
  make_toolbar
  make_prologio
  make_tree
  make_graph
  make_hytext
  make_wxhelp
  make_tex2rtf
}

make_peripheral_components()
{
  compoption='none'
  while [ "$compoption" != "0" ]
  do
    echo
    echo "Peripheral Component Options"
    echo "----------------------------"
    echo "1) Make wxToolBar library"
    echo "2) Make PROLOGIO library"
    echo "3) Make wxTree layout library"
    echo "4) Make wxGraph layout library"
    echo "5) Make hyText library"
    echo "6) Make wxHelp system"
    echo "7) Make Tex2RTF RTF/HTML/wxHelp/WinHelp converter"
    echo "8) Make all the above"
    echo "----------------------------"
    echo "0) Quit."

    read compoption

    if [ "$compoption" = "1" ]
    then
      make_toolbar
    fi
    if [ "$compoption" = "2" ]
    then
      make_prologio
    fi
    if [ "$compoption" = "3" ]
    then
      make_tree
    fi
    if [ "$compoption" = "4" ]
    then
      make_graph
    fi
    if [ "$compoption" = "5" ]
    then
      make_hytext
    fi
    if [ "$compoption" = "6" ]
    then
      make_wxhelp
    fi
    if [ "$compoption" = "7" ]
    then
      make_tex2rtf
    fi
    if [ "$compoption" = "8" ]
    then
      make_all_periph_components
    fi
  done
}

make_all_demos()
{
  make_hello
  make_minimal
  make_mdi
  make_ipc
  make_form
}

make_demos()
{
  compoption='none'
  while [ "$compoption" != "0" ]
  do
    echo
    echo "Demo compilation options"
    echo "------------------------"
    echo "1) Make Hello demo (the main wxWindows demo)"
    echo "2) Make Minimal demo"
    echo "3) Make MDI (Multiple document interface) demo"
    echo "4) Make IPC demo"
    echo "5) Make wxForm demo"
    echo "6) Make all the above"
    echo "------------------------"
    echo "0) Quit."

    read compoption

    if [ "$compoption" = "1" ]
    then
      make_hello
    fi
    if [ "$compoption" = "2" ]
    then
      make_minimal
    fi
    if [ "$compoption" = "3" ]
    then
      make_mdi
    fi
    if [ "$compoption" = "4" ]
    then
      make_ipc
    fi
    if [ "$compoption" = "5" ]
    then
      make_form
    fi
    if [ "$compoption" = "6" ]
    then
      make_all_demos
    fi
  done
}

###############################################################
# Run demos                                                   #
###############################################################

run_hello()
{
  if [ ! -f $WXDIR"/samples/hello/hello"$GUISUFFIX ]
  then
    echo The Hello sample has not been compiled yet.
  else
    echo
    echo "Running "$WXDIR"/samples/hello/hello"$GUISUFFIX
    echo
    cd $WXDIR"/samples/hello"
    "hello"$GUISUFFIX
    cd $CURRENTDIR
  fi
}

run_minimal()
{
  if [ ! -f $WXDIR"/samples/minimal/minimal"$GUISUFFIX ]
  then
    echo The Minimal sample has not been compiled yet.
  else
    echo
    echo "Running "$WXDIR"/samples/minimal/minimal"$GUISUFFIX
    echo
    cd $WXDIR"/samples/minimal"
    "minimal"$GUISUFFIX
    cd $CURRENTDIR
  fi
}

run_mdi()
{
  if [ ! -f $WXDIR"/samples/mdi/mdi"$GUISUFFIX ]
  then
    echo The MDI sample has not been compiled yet.
  else
    echo
    echo "Running "$WXDIR"/samples/mdi/mdi"$GUISUFFIX
    echo
    cd $WXDIR"/samples/mdi"
    "mdi"$GUISUFFIX
    cd $CURRENTDIR
  fi
}

run_form()
{
  if [ ! -f $WXDIR"/samples/form/form"$GUISUFFIX ]
  then
    echo The Form sample has not been compiled yet.
  else
    echo
    echo "Running "$WXDIR"/samples/form/form"$GUISUFFIX
    echo
    cd $WXDIR"/samples/form"
    "form"$GUISUFFIX
    cd $CURRENTDIR
  fi
}

run_ipc()
{
  if [ ! -f $WXDIR"/samples/ipc/client"$GUISUFFIX ]
  then
    echo The IPC samples have not been compiled yet.
  else
    echo
    echo "Running "$WXDIR"/samples/ipc/client"$GUISUFFIX,
    echo "        "$WXDIR"/samples/ipc/server"$GUISUFFIX
    echo
    cd $WXDIR"/samples/ipc"
    echo "Starting server..."
    "server"$GUISUFFIX &
    echo "Waiting 15 seconds..."
    sleep 15
    echo "Starting client..."
    "client"$GUISUFFIX
    cd $CURRENTDIR
  fi
}

run_demos()
{
  compoption='none'
  while [ "$compoption" != "0" ]
  do
    echo
    echo "Run a wxWindows demo"
    echo "--------------------"
    echo "1) Run Hello demo (the main wxWindows demo)"
    echo "2) Run Minimal demo"
    echo "3) Run MDI (Multiple document interface) demo"
    echo "4) Run IPC demo"
    echo "5) Run wxForm demo"
    echo "--------------------"
    echo "0) Quit."

    read compoption

    if [ "$compoption" = "1" ]
    then
      run_hello
    fi
    if [ "$compoption" = "2" ]
    then
      run_minimal
    fi
    if [ "$compoption" = "3" ]
    then
      run_mdi
    fi
    if [ "$compoption" = "4" ]
    then
      run_ipc
    fi
    if [ "$compoption" = "5" ]
    then
      run_form
    fi
  done
}

make_all()
{
  make_main_library
  make_all_periph_components
  make_all_demos
}

remove_component()
{
  if [ -d $COMPONENT ]
  then
    echo "Remove "$COMPONENT": are you sure? (y/n) "
    read ans
    if [ "$ans" = "y" ]
    then
      rm -f -r $COMPONENT
    fi
  fi
}

remove_components()
{
  compoption='none'
  while [ "$compoption" != "0" ]
  do
    if [ -f $WXDIR/docs/manual.ps ]
    then
      PSMANUALSOK="*"
    else
      PSMANUALSOK=" "
    fi
  
    if [ -f $WXDIR/docs/wx.hlp ]
    then
      HLPMANUALSOK="*"
    else
      HLPMANUALSOK=" "
    fi
  
    if [ -d $WXDIR/utils/prologio ]
    then
      PROLOGIOOK="*"
    else
      PROLOGIOOK=" "
    fi
  
    if [ -d $WXDIR/utils/toolbar ]
    then
      TOOLBAROK="*"
    else
      TOOLBAR=" "
    fi
  
    if [ -d $WXDIR/utils/wxtree ]
    then
      WXTREEOK="*"
    else
      WXTREEOK=" "
    fi
  
    if [ -d $WXDIR/utils/wxgraph ]
    then
      WXGRAPHOK="*"
    else
      WXGRAPHOK=" "
    fi

    if [ -d $WXDIR/utils/wxhelp ]
    then
      WXHELPOK="*"
    else
      WXHELPOK=" "
    fi

    if [ -d $WXDIR/utils/tex2rtf ]
    then
      TEX2RTFOK="*"
    else
      TEX2RTFOK=" "
    fi

    if [ -d $WXDIR/utils/hytext ]
    then
      HYTEXTOK="*"
    else
      HYTEXTOK=" "
    fi

    if [ -d $WXDIR/utils/wxclips ]
    then
      WXCLIPSOK="*"
    else
      WXCLIPSOK=" "
    fi

    echo
    echo "Remove a wxWindows component (* means it's installed)"
    echo "-----------------------------------------------------"
    echo "wxWindows currently takes up this many KB:"
    du -s $WXDIR
    echo "-----------------------------------------------------"
    echo " 1) "$PSMANUALSOK"Remove PostScript manuals"
    echo " 2) "$HLPMANUALSOK"Remove Windows Help manuals"
    echo " 3) "$PROLOGIOOK"Remove PROLOGIO library"
    echo " 4) "$HYTEXTOK"Remove hyText library"
    echo " 5) "$TOOLBAROK"Remove wxToolBar library"
    echo " 6) "$WXTREEOK"Remove wxTree library"
    echo " 7) "$WXGRAPHOK"Remove wxGraph library"
    echo " 8) "$WXHELPOK"Remove wxHelp system"
    echo " 9) "$TEX2RTFOK"Remove Tex2RTF/HTML/WinHelp/wxHelp converter"
    echo "10) "$WXCLIPSOK"Remove wxCLIPS (requires NASA's CLIPS to compile)"
    echo "------------------------------------------------------"
    echo "0) Quit."

    read compoption

    if [ "$compoption" = "1" ]
    then
      rm -f $WXDIR/docs/*.ps
      rm -f $WXDIR/utils/*/docs/*.ps
    fi
    if [ "$compoption" = "2" ]
    then
      rm -f $WXDIR/docs/*.hlp
      rm -f $WXDIR/utils/*/docs/*.hlp
    fi
    if [ "$compoption" = "3" ]
    then
      COMPONENT=$WXDIR/utils/prologio
      remove_component
    fi
    if [ "$compoption" = "4" ]
    then
      COMPONENT=$WXDIR/utils/hytext
      remove_component
    fi
    if [ "$compoption" = "5" ]
    then
      COMPONENT=$WXDIR/utils/toolbar
      remove_component
    fi
    if [ "$compoption" = "6" ]
    then
      COMPONENT=$WXDIR/utils/wxtree
      remove_component
    fi
    if [ "$compoption" = "7" ]
    then
      COMPONENT=$WXDIR/utils/wxgraph
      remove_component
    fi
    if [ "$compoption" = "8" ]
    then
      COMPONENT=$WXDIR/utils/wxhelp
      remove_component
    fi
    if [ "$compoption" = "9" ]
    then
      COMPONENT=$WXDIR/utils/tex2rtf
      remove_component
    fi
    if [ "$compoption" = "10" ]
    then
      COMPONENT=$WXDIR/utils/wxclips
      remove_component
    fi
  done
}

### View manual ###

view_manual()
{
  if [ ! -d $WXDIR ]
  then
    echo "Sorry -- you must unarchive the distribution before you can"
    echo "look at the manual."
  else
    if [ ! -f $WXDIR"/utils/wxhelp/src/wxhelp"$GUISUFFIX ]
    then
      echo "Please compile wxHelp first, or place wxhelp"$GUISUFFIX" in"
      echo $WXDIR"/wxhelp/src."
      echo "Make it now? (y/n)"
      read ans
      if [ "$ans" = "y" ]
      then
        make_wxhelp
      fi
    fi
    if [ -f $WXDIR"/utils/wxhelp/src/wxhelp"$GUISUFFIX ]
    then
      $WXDIR"/utils/wxhelp/src/wxhelp"$GUISUFFIX -f $WXDIR"/docs/wx.xlp"
    fi
  fi
}

echo
echo "--------------------------------------"
echo "Welcome to the wxWindows installation."
echo "--------------------------------------"
echo
echo The X include directories are:
echo $XINCLUDE
echo
echo The X library directories are:
echo $XLIB
echo
echo The compiler is $CC
echo The GUI target is $GUITARGET out of motif, xview, hp.
echo
echo The directory to install to is:
echo $WXDIR
echo
echo ">>> Please check these settings carefully."
echo ">>> Are they correct for your system? (y/n)"

read ans

if [ "$ans" != "y" ]
then 
  echo Please edit the variables in this file, wxinstall.
  exit
else
  useroption='none'
  while [ "$useroption" != "0" ]
  do
    echo
    echo "wxWindows Installation Main Options"
    echo "-----------------------------------"
    echo
    echo "1) Install from archive."
    echo "2) Make main wxWindows library."
    echo "3) Make a peripheral component."
    echo "4) Make a demo."
    echo "5) Make absolutely everything."
    echo "6) Run a demo."
    echo "7) View manual using wxHelp"
    echo "8) Remove wxWindows components."
    echo "-----------------------------------"
    echo "0) Exit from installation."

    read useroption

    if [ "$useroption" = "1" ]
    then
      install_from_archive
    fi
    if [ "$useroption" = "2" ]
    then
      make_main_library
    fi
    if [ "$useroption" = "3" ]
    then
      make_peripheral_components
    fi
    if [ "$useroption" = "4" ]
    then
      make_demos
    fi
    if [ "$useroption" = "5" ]
    then
      make_all
    fi
    if [ "$useroption" = "6" ]
    then
      run_demos
    fi
    if [ "$useroption" = "7" ]
    then
      view_manual
    fi
    if [ "$useroption" = "8" ]
    then
      remove_components
    fi
  done
fi

