#!/bin/sh
#
#   Platform-independent setup script.
#
#   Selects the correct data file, unpacks it and starts the installer.
#

echo "SEM Agent for UNIX"
echo "------------------"
echo ""

data=""
spec=""
install="SEMInstaller"
platform=`uname -s`
tmp_dir="`pwd`/tmp"
tmp_file="sysgem_data_tmp.tar"

echo "[8] Platform is ${platform}"

#
#   The data file is platform-specific.
#
case $platform in

    AIX )
    spec="1"
    ;;

    HP-UX )
    processor=`uname -m`
    case $processor in

        ia64 )
            spec="7"
            ;;

        * )
            spec="2"
            ;;
    esac
    ;;

    IRIX )
    spec="3"
    ;;

    Linux )
    processor=`uname -m`
    case $processor in

        ia64 )
            spec="9"
            ;;

        s390x )
            spec="10"
            ;;

        x86_64 )
            spec="11"
            ;;

        ppc64 )
            spec="12"
            ;;

        * )
            spec="4"
            ;;
    esac
    ;;

    OSF1 )
    spec="5"
    ;;

    SunOS )
    hardware=`uname -m`
    case $hardware in

        i86pc )
            spec="8"
            ;;

        * )
            spec="6"
            ;;
    esac
    ;;

    * )
    echo "Unknown platform ${platform}, please contact Sysgem AG"
    exit 0

esac

data="_kit.${spec}"

#
#   Show the file.
#
echo "[7] Data file is ${data}"

#
#   Create dir if it doesn't exist.
#
echo "[6] Creating ${tmp_dir}"

if test ! -d ${tmp_dir}; then
    mkdir ${tmp_dir}
fi

#
#   Clear out the directory.
#
rm -f ${tmp_dir}/*

#
#   Copy to a temporary file, then decompress and unpack.
#
echo "[5] Copying ${data} to ${tmp_dir}"
cp ${data} ${tmp_dir}/${data}

echo "[4] Changing to ${tmp_dir}"
cd ${tmp_dir}

echo "[3] Unpacking ${data}"
tar -xf ${data}

#
#   64-bit HP-UX.
#
# case $platform in
# 
#     HP-UX )
#     OSVER=`uname -r | sed 's/[A-Z.]//g'`
#     OSBITS=64
# 
#     if [ "$OSVER" -ge 1100 ]; then
#         OSBITS=`getconf KERNEL_BITS`;
#     fi
# 
#     if [ ${OSBITS} -eq 64 ];
#     then
#         echo ""
#         echo "Using 64-bit executables...";
#         rm ssm-perf
#         cp ssm-perf-64 ssm-perf
#     fi
#     ;;
# 
#     Linux )
#     hard=`arch`
#     
#     if [ ${hard} = "x86_64" ];
#     then
#         echo ""
#         echo "Using 64-bit executables...";
#         pwd
#         rm ssm-commander
#         cp ssm-commander-64 ssm-commander
#     fi
#     ;;
# 
# esac

echo "[2] Removing temporary files"
rm -f ${data}

echo "[1] Launching ${install}"

./${install} "$@"
