#!/bin/sh
#
# @package      hubzero-mw-session
# @file         startxvnc
# @author       Rick Kennell <kennell@purdue.edu>
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @author       Pascal Meunier  <pmeunier@purdue.edu>
# @copyright    Copyright (c) 2005-2014 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2005-2014 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.

set -e

PATH=/usr/bin:/bin:/usr/sbin:/sbin

if [ $# != 3 ]
then
  echo Syntax: $0 '<hostname> <dispnum> <geometry>'
  exit 1
fi

count=1
while [ "`/sbin/runlevel`" = "unknown" ]; do
        echo "Waiting for container to finish booting."
        sleep 1
		count=$(($count+1))
        if [ "$count" -gt 99 ]; then
                echo "Container failed to boot"
                exit 1
        fi
done

hostname=$1
dispnum=$2
geometry=$3
cookie=$(mcookie)

# Create runtime Xvnc directories and drop to the 'nobody' user.

if [ $(id -u) = 0 ]
then
    mkdir -p /var/log/Xvnc
    mkdir -p /var/run/Xvnc
    chown nobody:root /var/log/Xvnc
    chown nobody:root /var/run/Xvnc
    chmod 755 /var/log/Xvnc
    chmod 755 /var/run/Xvnc
    echo $0: Becoming nobody.
    unset DISPLAY
    su -c "$0 $*" nobody
    exit $?
fi

umask 022

if [ $(id -u) != $(id -u nobody) ]
then
  mkdir -p ~/.vnc
  chmod go-rwx ~/.vnc
  logdir=~/.vnc
  rundir=~/.vnc
else
  logdir=/var/log/Xvnc
  rundir=/var/run/Xvnc
fi

cd ${rundir}

#
# Get the VNC password from standard input.
#
echo $0: Waiting for 8-byte vncpasswd and EOF.
dd count=1 bs=8 of=passwd-${hostname}:${dispnum}
echo "Got the vncpasswd"
chmod go-r passwd-${hostname}:${dispnum}

#
# Build an Xauthority entry for the display.
#
echo Adding auth for ${hostname}:${dispnum} and ${hostname}/unix:${dispnum}
echo add ${hostname}:${dispnum} . ${cookie} > authlist-${hostname}:${dispnum}
echo add ${hostname}/unix:${dispnum} . ${cookie} >> authlist-${hostname}:${dispnum}
cat authlist-${hostname}:${dispnum} | xauth -f Xauthority-${hostname}:${dispnum} source -

portnum=$((5000 + $dispnum))

#
# When a log file is created, make sure it is not user-readable.
#
umask 077

fontpath=""
for i in /usr/share/fonts/X11/misc \
  /usr/share/fonts/X11/100dpi \
  /usr/share/fonts/X11/75dpi \
  /usr/share/fonts/X11/Type1 \
  /usr/share/fonts/X11/cyrillic \
  /usr/share/fonts/truetype/freefont \
  /usr/share/fonts/truetype/msttcorefonts \
  /usr/share/fonts/truetype/ttf-bitstream-vera \
  /usr/share/fonts/truetype/ttf-dejavu \
  /usr/share/fonts/truetype/ttf-liberation \
  /usr/share/fonts/truetype/unifont \
  /var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType \
 ; do
  if test -d "$i"; then 
      if test ! -f "$i/fonts.scale"; then
	echo "$i/fonts.scale missing"
      fi

      if test -f "$i/fonts.dir"; then
      	if test "x${fontpath}" = "x"; then 
	  fontpath="$i"
      	else 
	  fontpath="$fontpath,$i"
      	fi
      else
	echo "$i/fonts.dir missing, not adding to font path"
      fi
  else
	echo "$i missing, not adding to font path"
  fi
 
done

#
# Start the display.
#

if [ -f /usr/bin/Xtigervnc ]; then
XVNC=/usr/bin/Xtigervnc
elif [ -f /usr/bin/Xvnc4 ]; then
XVNC=/usr/bin/Xvnc4
elif [ -f /usr/bin/Xtightvnc ]; then
XVNC=/usr/bin/Xtightvnc
elif [ -f /usr/bin/Xvnc ]; then
XVNC=/usr/bin/Xvnc
else
XVNC=/usr/bin/Xvnc
fi

${XVNC} :${dispnum} -desktop hub -geometry $geometry -auth Xauthority-${hostname}:${dispnum} \
	-depth 24  -rfbwait 30000 -rfbauth passwd-${hostname}:${dispnum} \
	-rfbport ${portnum} -pn -fp ${fontpath} > ${logdir}/Xvnc-${hostname}:${dispnum}.log 2>&1 &

#
# Record the pid of the process just started.
#
pid=$!
echo ${pid} > ${rundir}/Xvnc-${hostname}:${dispnum}.pid

