#!/bin/sh

# NOTE: If your system doesn't support type -path then this script will
#       only work properly if it's run from the current directory

this_file=`type -path $0`
this_dir=`expr $this_file : '\(.*\)/' '|' .`

comm_or_demo=`expr $this_file : '.*\(demo\)' '|' commercial`

# warn the user if we're not root

if [ x`whoami` != xroot ]; then

  echo You are not running as super user.
  echo If you continue with the install, executor-svga will not be
  echo made setuid root, and it should be.
  echo ' '

  first_time_in_loop=1
  response=unknown
  while [ x$response != x -a x$response != xy -a x$response != xn  ]; do
    if [ $first_time_in_loop -ne 1 ]; then
      echo Unknown response $response
    fi
    first_time_in_loop=0
    echo -n "Continue anyway [n/y]? "
    read response
  done
  
  if [ x$response != xy ]; then
    exit 1
  fi
fi

# Choose default executable format

default=aout
alternate=elf
test_file=/bin/cat
if [ -r $test_file ]; then
  if file $test_file | grep -si elf; then
    default=elf
    alternate=aout
  fi
fi

# ask what executable format

first_time_in_loop=1
response=unknown
while [ x$response != x -a x$response != xaout -a x$response != xelf  ]; do
  if [ $first_time_in_loop -ne 1 ]; then
    echo Unknown response $response
  fi
  first_time_in_loop=0
  echo -n "Which executable type should be used [$default/$alternate]? "
  read response
done

if [ x$response = x ]; then
  response=$default
fi

# untar the common files

echo Installing the files common to all Executor distributions

tar_file=$this_dir/executor-common.tar.gz

if tar xpfzv $tar_file -C /; then
  lack_of_nul_command=true
else
  echo ' '
  echo Some sort of problem seems to have occurred untarring $tar_file
  echo Installation FAILED.
  exit 1
fi

# untar the appropriate file

echo Installing $response format files

tar_file=$this_dir/executor-$response-$comm_or_demo.tar.gz

if tar xpfzv $tar_file -C /usr/local/bin; then
  echo Done
  exit 0
else
  echo ' '
  echo Some sort of problem seems to have occurred untarring $tar_file
  echo Installation FAILED.
  exit 1
fi
