#!/bin/sh
# iTalc client launcher using avahi
# Written by Stéphane Graber <stgraber@ubuntu.com>

find_free_port() {
# $1 = requested port
# $2 = sorted list of ports in use
# Basic logic: if the distance between two used ports A and B is greater than 1,
# then port A+1 is free.
    req_port=$1
    used_ports="$2 65536"
    candidate=$req_port
    for p in $used_ports
    do
        if [ $p -lt $req_port ]
        then # Pass the ports lower than the requested one
            continue
        elif [ $p -gt $candidate ]
        then
            echo $candidate
            return 0
        else
            candidate=$(($p+1))
        fi
    done
    return 1
}

ICA_LTSP=`xprop -root ica_ltsp | sed 's/^.* //' 2> /dev/zero`
if [ "$ICA_LTSP" = "1" ]
then
    echo "ICA already running on the thin client."
    exit 0
fi

# Provide a configuration file to enable switching off client autodetection
# and/or specifying user-defined ports.
if [ -f /etc/italc/italc.conf ]
then
    . /etc/italc/italc.conf
fi

PUBLISH_CLIENT=${PUBLISH_CLIENT:-True}
ISDPORT=${ISDPORT:-5800}
IVSPORT=${IVSPORT:-5900}
HOSTNAME=`hostname`
MD5_3=`md5sum /etc/italc/keys/public/teacher/key | awk '{print \$1}'`
MD5_2=`md5sum /etc/italc/keys/public/admin/key | awk '{print \$1}'`
MD5_1=`md5sum /etc/italc/keys/public/supporter/key | awk '{print \$1}'`

if [ -n "$1" ]; then
    ROLE=$1
else
    ROLE="other"
fi

if [ -n "$LTSP_CLIENT" ]
then
    echo LTSP environement detected
    PORT=`echo $LTSP_CLIENT | awk -F . '{print \$4}'`
# Not much point in using the ports from /etc/italc/italc.conf for thin clients
    ISDPORT=$((11000 + $PORT))
    IVSPORT=$((10000 + $PORT))
    HOSTNAME=${LTSP_CLIENT_HOSTNAME:-$HOSTNAME}
fi

USED_PORTS=$(netstat -nap 2>/dev/null | grep -v TIME_WAIT | sed -n 's/unix[^0-9]*[0-9]*[^0-9]*\([0-9]*\).*/\1/p;s/tcp[^:]*:\([0-9]*\).*/\1/p;s/udp[^:]*:\([0-9]*\).*/\1/p' | sort -n)
ISDPORT=`find_free_port $ISDPORT "$USED_PORTS"`
USED_PORTS=$(echo "$USED_PORTS
$ISDPORT" | sort -n)
IVSPORT=`find_free_port $IVSPORT "$USED_PORTS"`

RUNNING_ICA=`ps -C ica -o pid,command | sed -n 's/^ *\([0-9]*\) *ica.*/\1/p'`
if [ -n "$RUNNING_ICA" ]
then
    for processus in $RUNNING_ICA
    do
        kill $processus
    done
    echo ICA already running, killing it.
    sleep 1s
fi

if [ "$PUBLISH_CLIENT" = "True" ] && [ -f /usr/bin/avahi-publish-service ] && [ ! -f /etc/ltsp/getltscfg-cluster.conf ]
then
    echo Announce the service on avahi
    avahi-publish-service "italc $USER" _italc._tcp $IVSPORT $MD5_1 $MD5_2 $MD5_3 "$HOSTNAME" > /dev/zero &
fi

echo Starting ICA
# Store the port in an xprop, for italc-launcher to find it
xprop -root -f ICA_PORT 16c -set ICA_PORT $ISDPORT
ica -noshm -isdport $ISDPORT -ivsport $IVSPORT -role $ROLE 2> /dev/zero
xprop -root -remove ICA_PORT

if [ "$PUBLISH_CLIENT" = "True" ] && [ -f /usr/bin/avahi-publish-service ] && [ ! -f /etc/ltsp/getltscfg-cluster.conf ]
then
    echo Stopping avahi
    kill `ps -C "avahi-publish-service italc $USER _italc._tcp $IVSPORT" -o pid=`
fi
