#!/bin/sh
echo loading MICS installer, please wait...

# change to the working dir
DIR="$( cd "$( dirname "$0" )" && pwd )"
cd "$DIR"

# this can be done as root only
if [ "$EUID" -eq 0 ] ; then
  # store current se mode
  se="$(getenforce)"

  # tempo switch to permissive
  setenforce 0
fi



# do the actual installation
# 1) try MICS installer jre first
# 2) try to use system jre as fallback
# 3) try to use topspin jre as last resort
if (./jre/bin/java -Xmx384M -Dsun.java2d.xrender=false -Dawt.useSystemAAFontSettings=lcd -splash:install_splash.png -jar mics.jar -install $*) ; then
  echo "using MICS installer jre..."
elif (java -Xms32M -Xmx200M -XX:PermSize=20M -Dsun.java2d.xrender=false -Dawt.useSystemAAFontSettings=lcd -splash:install_splash.png -jar mics.jar -install $*) ; then
  echo "MICS installer jre failed, using system jre..."
else
  echo "both MICS installer and system jre failed, trying topspin jre..."
 
  if (d="$(topspin -p)") ; then
    tsp="$(topspin -p)"
    "$tsp/jre/bin/java" -Xms32M -Xmx200M -XX:PermSize=20M -Dsun.java2d.xrender=false -Dawt.useSystemAAFontSettings=lcd -splash:install_splash.png -jar mics.jar -install $*
  else
    echo "****************************************************"
    echo "* ERROR: no running jre found"
    echo "* try starting '$DIR/install' as root ('su -')"
    echo "* or disable selinux during the update ('setenforce 0')."
    echo "****************************************************"
  fi

fi


# this can be done as root only
if [ "$EUID" -eq 0 ] ; then
  # restore original se mode
  setenforce ${se}
fi
