#!/usr/bin/env bash
#
# VMware Installer Service command line interface.
#

set -e

setsi="no"
username="${VMWARE_GKSU_USER}"

if [ -z "${username}" ] && [ -n "${SUDO_USER}" ]; then
   username=${SUDO_USER}
fi

exit_install() {
   if [ "${setsi}" = "yes" ] && [ -n "${username}" ]; then
      # When the setsi is "yes", we must be root/sudo account
      # and set the "SI:localuser:root" by us.
      su ${username} -c "xhost -SI:localuser:root" >/dev/null 2>&1
   fi
}

# Used for canceling installation, rollback possible xhost setting.
trap "exit_install" USR1 EXIT INT QUIT TERM

ETCDIR=/etc/vmware-installer

MIN_GLIBC_VERSION=2.5


# Check to make sure glibc version >= MIN_GLIBC_VERSION, meet the minimum
# glibc version requirement to launch vmis-launcher. Function returns with
# no side effect if version check succeeds, exits with 1 if version check
# fails, but only emits a warning if the version cannot be determined.
validate_glibc_version() {
   # Example ldd output:
   #   ubuntu:
   #      ldd (Ubuntu EGLIBC 2.12.1-0ubuntu10.2) 2.12.1
   #      ...
   #   other linux distributions:
   #      ldd (GNU libc) 2.12
   #      ...
   read glibcMajor glibcMinor <<<`ldd --version | awk '/^ldd/ { split($NF,v,/\./); print v[1],v[2]; }'`
   if [ "$glibcMinor" == "" ]; then
      echo "Warning: VMware Installer could not determine this system's glibc version."
      return
   fi

   # Make sure glibc version >= MIN_GLIBC_VERSION
   minVersion=$(expr `echo $MIN_GLIBC_VERSION | awk -F. '{print $1*65536+$2}'`)
   glibcVersion=$(expr $glibcMajor \* 65536 + $glibcMinor)

   if [ $glibcVersion -lt $minVersion ]; then
      echo "Error: This system's glibc (version $glibcMajor.$glibcMinor) is too old. This product requires glibc version $MIN_GLIBC_VERSION or later."
      exit 1
   fi
}

# VMWARE_BOOTSTRAP is overriden in the bootstrapper so that it can be
# sourced from a temporary location during installation.
if [ -e "$VMWARE_BOOTSTRAP" ]; then
   . "$VMWARE_BOOTSTRAP"
else
   . $ETCDIR/bootstrap
fi

. "$VMWARE_INSTALLER"/python/init.sh

# vmis-launcher requires glibc versioin >= MIN_GLIBC_VERSION
validate_glibc_version

# set "set +e" to make sure "| grep" can work in our scenario.
set +e
if [ -n "${DISPLAY}" ]; then
   ps -e | grep -q -i "Xwayland" >/dev/null 2>&1
   if [ "$?" = "0" ]; then
      # we only do this for Xwayland session
      account=`id -unr`
      if [ "${account}" = "root" ] && [ -n "${username}" ] && \
         [ "${username}" != "${account}" ]; then
         # we only do this when the user realname is not "root"
         # and we can only switch to realname user to do the xhost setting
         # in wayland session
         su ${username} -c "xhost" | grep -q 'SI:localuser:root' >/dev/null 2>&1
         if [ "$?" != "0" ]; then
            # we only do this for "root" entry is not enabled.
            su ${username} -c "xhost SI:localuser:root" >/dev/null 2>&1
            if [ "$?" = "0" ]; then
               setsi="yes"
            fi
         fi
      fi
   fi
fi
# rollback to "set -e" environment.
set -e

VMWARE_INSTALLER="$VMWARE_INSTALLER" VMISPYVERSION="$VMISPYVERSION" "$VMWARE_INSTALLER"/vmis-launcher "$VMWARE_INSTALLER"/vmware-installer.py "$@"

# clean the xhost setting by us
exit_install
