#!/bin/ksh

# Warn that only superuser should setup InterDesk.

if test $(id -u) -ne 0
then
  echo "\\aWARNING!!!"
  echo "It is recommended that only the superuser (root) setup InterDesk."
  echo "If possible, have your system administrator install InterDesk for you."
  echo
  echo "Press Enter to continue...\\c"

  read input
fi

# screen.cfg-file setup function.

setup_screen_cfg ()
{
  echo "Processing file $1 ..."
  fgrep InterDesk/shell $1 > /dev/null
  export result="$?"
  if test $result -eq 1
  then
    echo "\\tAdding InterDesk Graphical Shell to file..."
    echo start /usr/lib/windows/apps/InterDesk/shell >> $file
  elif test $result -eq 0
  then
    echo "\\tFile already contains the InterDesk Graphical Shell!"
  else
    echo "\\tFile doesn't exist!"
  fi
}

# Clear screen.

echo \\f\\c

if test ! -d /usr/lib/windows/apps/InterDesk
  echo "Directory /usr/lib/windows/apps/InterDesk does not exist!  Please install"
  echo "the InterDesk archive to this location before setting up InterDesk."
  echo

  exit
fi

# Main menu loop.

while true
do
  echo
  echo
  echo "InterDesk Version 1.00.  Copyright (C) 1996 by Scott C. Moonen."
  echo "All Rights Reserved."
  echo
  echo "InterDesk Setup Menu:"
  echo
  echo "1)  Setup InterDesk's Symbolic Links"
  echo "2)  Configure User for InterDesk"
  echo "3)  Put Shell in Specific screen.cfg Files"
  echo "Q)  Exit"
  echo
  echo "Enter a selection: \\c"

  read input

  case $input in
    1)
        echo Creating links for InterDesk...
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.altermode.idsh \
          /usr/lib/windows/apps
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.clipboard.idsh \
          /usr/lib/windows/apps
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.iedit.idsh     \
          /usr/lib/windows/apps/Iedit
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.loadmon.idsh   \
          /usr/lib/windows/apps
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.mail.idsh      \
          /usr/bin
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.maildog.idsh   \
          /usr/lib/windows/apps
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.maildog.idsh   \
          /usr/lib/windows/apps32
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.mandelq.idsh   \
          /usr/lib/windows/apps/Mandel
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.puzzle.idsh    \
          /usr/lib/windows/apps
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.puzzle.idsh    \
          /usr/lib/windows/apps32
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.qwcalc.idsh    \
          /usr/lib/windows/apps/Calc
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.qwcalc.idsh    \
          /usr/lib/windows/apps32/Calc
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.ruler.idsh     \
          /usr/lib/windows/apps
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.ruler.idsh     \
          /usr/lib/windows/apps32
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.sedit.idsh     \
          /usr/lib/windows/apps/Sedit
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.sedit.idsh     \
          /usr/lib/windows/apps32/Sedit
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.slideshow.idsh \
          /usr/lib/windows/apps
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.slideshow.idsh \
          /usr/lib/windows/apps32
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.solitaire.idsh \
          /usr/lib/windows/apps/Solitaire
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.vpoker.idsh    \
          /usr/lib/windows/apps/Vpoker
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.wclock.idsh    \
          /usr/lib/windows/apps
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.wclock.idsh    \
          /usr/lib/windows/apps32
        ln -fs /usr/lib/windows/apps/InterDesk/shadow_files/.wterm.idsh     \
          /usr/lib/windows/bin
        ln -fs /usr/lib/windows/apps/InterDesk/lib/*.lib                    \
          /usr/lib

        echo
        echo "All system screen.cfg files in /windows/config (including node-specific ones)"
        echo "will now be updated to include the InterDesk Graphical Shell in them."
        echo
        echo "Press Enter to continue, or type 'q' to cancel: \\c"

        read cont
        if test "$cont" = "q"
        then
          continue
        elif test "$cont" = "Q"
        then
          continue
        fi

        for file in /windows/config/screen.cfg*
        do
          setup_screen_cfg $file
        done

        ;;
    2)
        echo
        echo "Initialization files will now be installed for the user.  If some already"
        echo "exist, the new ones will be given a version-specific suffix."
        echo
        echo "Enter the user's home dir: \\c"
        read dir

        if test "$dir" = ""
        then
          continue
        fi

        export cwd="/usr/lib/windows/apps/InterDesk/doc"

        if test -d $dir
        then
          cd $dir
        else
          echo
          echo "\\aUser's home directory does not exist!"
          continue
        fi

        export cmd="fcat $cwd/InterDesk.ini.tar.F | pax -rlv"
        if test -f $dir/windows/InterDesk.ini
        then
          export cmd="$cmd -s ,InterDesk.ini,InterDesk100.ini,g"
        fi
        if test -f $dir/windows/IDskFm.ini
        then
          export cmd="$cmd -s ,IDskFm.ini,IDskFm100.ini,g"
        fi
        if test -f $dir/windows/IDskHelp.ini
        then
          export cmd="$cmd -s ,IDskHelp.ini,IDskHelp100.ini,g"
        fi

        if test ! -d windows
        then
          echo "Creating \"windows\" subdirectory..."
          mkdir windows
        fi

        cd windows

        export dir="$dir/windows"

        echo
        echo $cmd
        eval $cmd

        echo
        echo "All screen.cfg files in the user's windows configuration dir (including"
        echo "node-specific ones) will now be updated to include the InterDesk Graphical"
        echo "Shell in them."
        echo
        echo "Press Enter to continue, or type 'q' to cancel: \\c"

        read cont
        if test "$cont" = "q"
        then
          continue
        elif test "$cont" = "Q"
        then
          continue
        fi

        for file in $dir/screen.cfg*
        do
          setup_screen_cfg $file
        done
        ;;
    3)
        echo
        echo "All screen.cfg files in the given dir (including node-specific ones) will"
        echo "now be updated to include the InterDesk Graphical Shell in them."
        echo
        echo "Enter the directory: \\c"
        read dir

        if test "$dir" = ""
        then
          continue
        fi

        for file in $dir/screen.cfg*
        do
          setup_screen_cfg $file
        done
        ;;
    x*|X*|q*|Q*)
        exit
        ;;
  esac
done
