#!/bin/bash

if [ "x$1" = "x" ] || [ "x$2" = "x" ]
then
    echo "Usage:   $0 username resolution"
    echo "Example: $0 joe 1024x768"
    exit 1
fi

VNCUSER=$1
RESOLUTION=$2

getent passwd $VNCUSER > /dev/null 2>&1
if [ $? != 0 ]
then
    echo "User '$VNCUSER' does not exist"
    exit 1
fi

rpm -q tigervnc-server > /dev/null 2>&1 || yum -y install tigervnc-server
sed -i -e "s,^\([^#].*\)$,#\1," /etc/sysconfig/vncservers
echo "VNCSERVERS=\"1:$VNCUSER\"" >> /etc/sysconfig/vncservers
echo "VNCSERVERARGS[1]=\"-geometry $RESOLUTION\"" >> /etc/sysconfig/vncservers

su - "$VNCUSER" -c vncpasswd

chkconfig vncserver on
service vncserver stop > /dev/null 2>&1
service vncserver start

iptables -I INPUT 1 -p tcp -m state --state NEW --dport 5901 -j ACCEPT
grep 5901 /etc/sysconfig/iptables > /dev/null 2>&1 || service iptables save
