#!/bin/bash
#
# vim:tabstop=3:expandtab:shiftwidth=3
#
# $LastChangedDate: 2026-06-03 13:45:37 +0200 (Wed, 03 Jun 2026) $
# $Revision: 3248 $
#
###############################################################################
#Usage:# The script adds drive letter A: to citrix sessions
#Usage:#
#Usage:# Use /opt/Citrix/ICAClient/util/configmgr for alternatives
#Usage:#
###############################################################################

_help_show_args="[OFF/UIT/REMOVE/VERWIJDER]"

for D in . $(echo $PATH | tr ':' ' ')
do
   if [ -f $D/_option_processor ]
   then
      . $D/_option_processor
      break
   fi
done

ICAROOT=/opt/Citrix/ICAClient
WfClientTemplate="$ICAROOT/config/wfclient.template"
WfClientIni="$HOME/.ICAClient/wfclient.ini"

# $ICAROOT/config/wfclient.template
# $HOME/.ICAClient/.eula_accepted

# DrivePathA=/home/Hans
# DriveEnabledA=True

if ! rpm -q ICAClient &> /dev/null
then
   echo "! [ERROR] ICAClient not installed" >&2
   exit 1
fi

if [ ! -d "$(dirname $WfClientIni)" ] && \
   [ -f $ICAROOT/config/wfclient.template ]
then
   echo "# Creating $WfClientIni" | sed 's|'"$HOME"'|~|'
   mkdir "$(dirname $WfClientIni)"
   touch $(dirname $WfClientIni)/.eula_accepted
   cat $ICAROOT/config/wfclient.template > $WfClientIni
fi

if [ ! -w ${WfClientIni} ] 
then
   (
      if [ ! -r ${WfClientIni} ]
      then
         echo "! [ERROR] ${WfClientIni} not found, citrix installed?"
      else
         echo "! [ERROR] ${WfClientIni} write protected"
      fi
   ) | sed 's|'"$HOME"'|~|' >&2
   exit 1
fi

UitData="$(cat ~/.ICAClient/wfclient.ini | \
           tr '[\n' '\n~' | \
           sed -e 's|~DrivePathA=[^~]\+~|~|g' \
               -e 's|~DriveEnabledA=[^~]\+~|~|g' \
               -e '/^[[]$/d')"

if grep -qiE "$(sed -e 's,[]],$,' -e 's,/,$|^,g' -e 's,[[],^,' \
                  <<< "$_help_show_args")" <<< "$1"
then
   Action="OFF"
   NewData="$(echo -n "$UitData" | tr '\n~' '[\n')"
else
   Action="ON"
   NewData="$(echo -n "$UitData" | sed \
    -e 's|^\(WFClient[]].*[^~]\)\(~*\)$|\1~DrivePathA='"$HOME"'~DriveEnabledA=True\2|' | \
   tr '\n~' '[\n' | grep -v '^[[]$')"
fi

CurMd5="$(md5sum $WfClientIni | awk '{print $1}')"
NewMd5="$(echo -n "$NewData" | md5sum | awk '{print $1}')"

if [ "$NewMd5" = "$CurMd5" ]
then
   echo "# Citrix drive A: stays $Action"
else
   echo -n "$NewData" > ${WfClientIni}
   echo "# Citrix drive A: switched to $Action"
fi
