#!/bin/sh

# LSB TET installation script
# This script handles the installation of the TET3 test
# harness 
#
DEFTET_ROOT=/opt/lsb-tet3-lite

######################################################################
# Useful functions

######################################################################
# Add a group. Try to be tolerant of different groupadd arguments
add_single_group()
{
  if [ -n "$GROUP_ID" ]; then
    GROUP_ID=$(($GROUP_ID+1))
    GROUP_FLAGS="-g $GROUP_ID"
  fi
  set -x
  groupadd $GROUP_FLAGS $1
  GROUP_ADD=$?
  set +x
  if [ $GROUP_ADD -ne 0 -a -z "$GROUP_ID" ]; then
    grep $1 /etc/group
    if [ $? -ne 0 ]; then
      # work out a group id we can use
      # Serious dodgy algorithm but avoids the problem of 
      # nogroup often being set to the last valid gid
      echo
      echo Possibly detected different group add implementation
      echo Trying to workaround by supplying -G flag
      echo
      GROUP_ID=`cut -f3 -d: /etc/group | sort -n | tail -n 2 | head -n 1`;
      GROUP_ID=$(($GROUP_ID+10))
      GROUP_FLAGS="-g $GROUP_ID"
      set -x
      groupadd $GROUP_FLAGS $1
      set +x
    fi
  fi
}

uidsetup() 
{
  # add the maximum number of groups for a user
  echo "Adding the group tet"
  add_single_group  tet
  
  echo "Adding the user tet"
  
  set -x
  # you may prefer the ksh to sh
  useradd -g tet -d $TET_ROOT -s /bin/sh -c "TET harness login" tet
  set +x
  echo "Note you do not routinely have to login as user tet"
  echo "so you may prefer to lock the password."
  echo "Enter the password for the tet user"
  passwd tet
}

# Sanity configuration check
SanityChecks()
{
:
}

######################################################################

# Check we're running as root
if  [ $EUID -ne 0 ]; then
  echo "This installation script must be run as root"
  exit 1
fi

PATH=$PATH:/usr/sbin
export PATH

echo "TET Harness installation"
echo "---------------------------"
echo 
echo "IMPORTANT NOTE: TET3 will be installed into $DEFTET_ROOT"
echo "you can optionally select whether you want to assign file"
echo "ownership to a new user tet, or to have the files default"
echo "to be owned by bin."
echo
echo "If you are installing a new version of the test harness or the last"
echo "installation failed for any reason it is recommended that you first"
echo "remove any the old installation."
echo

# Do sanity checks on installation to save problems later
SanityChecks

# Find out where to install the test suite
printf "Enter root directory of TET harness installation: [$DEFTET_ROOT] "
read cmd
if [ "$cmd" = "" ]; then
  cmd=$DEFTET_ROOT
fi
if [ ! -d $cmd ]; then
  mkdir $cmd
  if [ $? -ne 0 ]; then
    echo "Unable to create installation directory: $cmd"
    exit 1
  fi
fi
TET_ROOT=$cmd
export TET_ROOT

# We do not need a user tet, but if we have one we install the files
# under that user
grep "^tet:" /etc/passwd 2>&1 >/dev/null
if [ $? -ne 0 ]; then
  printf "The optional user \"tet\" and group \"tet\" are not setup\n"
  printf "Do you wish to install them ..?[n]"
  read cmd
  if [ "$cmd" = "Y" -o "$cmd" = "y" ] ; then
      uidsetup
  else
    echo "Default ownerships will be applied to the files installed"
  fi
else
  usermod -d $TET_ROOT tet
fi

# Install the tet and vsxgen package
# Default to latest version of tarball available
tet_package=`ls -1 lsb_tet* 2> /dev/null | tail -n 1`
tet_package=${tet_package:-""}
if [ $tet_package ]; then
  tet_package="$PWD/$tet_package"
fi
echo "Installing the TET test harness"
printf "TET package to install [$tet_package]: "
read cmd
if [ "$cmd" != "" ]; then
  tet_package=$cmd
fi
if [ ! -f "$tet_package" ]; then
  echo "Could not find file \"$tet_package\""
  echo Aborting
  exit 1
fi


(cd $TET_ROOT && tar xfz $tet_package)
if [ $? -ne 0 ]; then
  echo "Unable to unpack TET"
  exit 1
fi


# Configure setup script and profiles
sed -e "s@^echo Unconfigured@TET_ROOT=$TET_ROOT@" $TET_ROOT/setup.sh.skeleton \
  > $TET_ROOT/setup.sh && rm $TET_ROOT/setup.sh.skeleton
sed -e "s@^echo Unconfigured@TET_ROOT=$TET_ROOT@" $TET_ROOT/profile.skeleton \
  > $TET_ROOT/profile && rm $TET_ROOT/profile.skeleton

# Set ownership correctly
echo Setting up ownership and permissions correctly
grep "^tet:" /etc/passwd 2>&1 >/dev/null
if [ $? -ne 0 ]; then
	chown -R bin.bin $TET_ROOT
else
	chown -R tet.tet $TET_ROOT
fi
chmod u+x $TET_ROOT/setup.sh

# Display licence information
echo ----------------------------------------------------------------------
echo Licencing Conditions for test harness
echo 

sh $TET_ROOT/Licence.brief
printf "Press Enter to continue. "
read cmd


grep "^tet:" /etc/passwd 2>&1 >/dev/null
if [ $? -ne 0 ]; then
	printf "\n\nYou should now login as the privileged user and\nrun $TET_ROOT/setup.sh to complete tet installation\n"
else
	printf "\n\nYou should now login as the tet user and\nrun $TET_ROOT/setup.sh to complete tet installation\n"
fi



