#!/bin/bash
#
# Copyright 2017 VMware, Inc.  All rights reserved.
#
# This script manages the VMware Horizon Client
#

libpath="/usr/lib/vmware"
binPath="/usr/lib/vmware/view"
scriptPath="$binPath/virtualPrinting/scripts"
vdp_servicePath="/usr/lib/pcoip/vchan_plugins"
libtsdrClient="$libpath/view/vdpService/libtsdrClient.so"
# Get the executed user
user=
if [[ $USER = "" ]]; then
   if [ "`type -t 'whomai' 2>/dev/null`" = 'file' ]; then
      user=`whoami`
   fi
else
   user=$USER
fi

# Get the home path
homePath=
if [[ $HOME = "" ]]; then
   homePath="/home/$user"
else
   homePath=$HOME
fi

# Input argument from stdin
stdin_arg=

# Check the openssl libraires path we installed in /usr/lib/vmware
vm_ssl_path() {
   local sslPath
   if [ -d "$libpath" ]; then
      sslPath="$libpath"
   else
      sslPath=""
   fi
   echo "$sslPath"
}

# Add path to LD_LIBRARY_PATH
vm_append_to_library_path() {
   local ldBasePath
   if [ ! -z "$LD_LIBRARY_PATH" ]; then
      ldBasePath="$LD_LIBRARY_PATH"
   fi
   export LD_LIBRARY_PATH="$1:""$ldBasePath"
}

# Function to export environment variable
vm_append_export() {
   local key=$1
   local val=$2

   export $key"=""$val"
}

# Export environment variable if libtsdrClient.so exists.
vm_folder_redirection_config() {
   if [ -x $libtsdrClient ]; then
      vm_append_export "ENABLE_FOLDER_REDIRECTION" "TRUE"
   fi
}

# Create config file for virtual printing
vm_virtual_printing_config() {
   local confPath=$homePath

   if [ -d "$confPath" ]; then
      if [ ! -d "$confPath/.thnuclnt" ]; then
         # If $scriptPath/thnuclnt.conf means that user choice not to start virtual printing service
         if [ -e "$scriptPath/thnuclnt.conf" ]; then
            mkdir -p "$confPath/.thnuclnt"
         fi
      fi
      if [ ! -e "$confPath/.thnuclnt/thnuclnt.conf" ]; then
         # If $scriptPath/thnuclnt.conf means that user choice not to start virtual printing service
         if [ -e "$scriptPath/thnuclnt.conf" ]; then
            cp "$scriptPath/thnuclnt.conf" "$confPath/.thnuclnt/thnuclnt.conf"
            # Replace /home/<user> as $homePath
            sed -e "s,/home/<user>,$confPath,g" -i "$confPath/.thnuclnt/thnuclnt.conf"
         fi
      fi
   fi
}


vm_start_virtual_printing() {
   if [ -d "$scriptPath" ]; then
      # If script file not existed means that user choice not to start virtual printing service
      if [ -e "$scriptPath/initscript-virtual-printing.sh" ]; then
         sh "$scriptPath/initscript-virtual-printing.sh" start & >/dev/null 2>&1
         # Export needed environment variables before laucn view client
         vm_append_export "TPCLIENTADDR" "$homePath/.thnuclnt/svc"
         vm_append_export "THNURDPIMG" "/usr/bin/thnurdp"
      fi
   fi
}

vm_stop_virtual_printing() {
   if [ -e "$scriptPath" ]; then
      # If script file not existed means that user choice not to run virtual printing service
      if [ -e "$scriptPath/initscript-virtual-printing.sh" ]; then
         sh "$scriptPath/initscript-virtual-printing.sh" stop >/dev/null 2>&1
      fi
   fi
}


vm_run() {
   # Export openssl libraries path
   vm_append_to_library_path "`vm_ssl_path`"
   vm_append_to_library_path $vdp_servicePath
   # Config tsdr feature
   vm_folder_redirection_config
   # Stop virtual printing service process before we create virtual printing config file
   vm_stop_virtual_printing
   vm_virtual_printing_config
   if [ -e "$binPath/bin/vmware-view" ]; then
      vm_start_virtual_printing
      if [ ! $stdin_arg ]; then
         "$binPath/bin/vmware-view" "$@"
      else
         echo $stdin_arg | "$binPath/bin/vmware-view" "$@"
      fi
      vm_stop_virtual_printing
   else
      echo "File not exists:/usr/lib/vmware/view/bin/vmware-view"
      exit 1
   fi

   return "$?"
}

# Read stdin if it exists and there is "-p -" in command parameters
if [ ! -t 0 ] && [[ "$@" =~ "-p -" ]]; then
   read -t 1 stdin_arg
fi

vm_run "$@"
