#!/bin/bash
#
# Copyright 2015 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"
# 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

# 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"
}

# 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`"
   # 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
      "$binPath/bin/vmware-view" "$@"
      vm_stop_virtual_printing
   else
      echo "File not exists:/usr/lib/vmware/view/bin/vmware-view"
      exit 1
   fi

   return "$?"
}

vm_run "$@"
