#!/bin/sh
##
## Multipurpose script to use VNC.
##
#
# Main variables.
#
TEMPORARY=`tempfile`
touch $TEMPORARY
SELECTION=""
#
GEOMETRY=""
DEPTH=""
#
VNC_REMOTE_SERVER=`cat /etc/nlnx/all/VNC_REMOTE_SERVER 2> /dev/null`
VNC_REMOTE_SERVERS=`cat /etc/nlnx/all/VNC_REMOTE_SERVERS 2> /dev/null`
#
# Encripted default password.
# bla bla bla: zxdftipe
#
ENCRIPTED_PASSWORD="zxdftipe"   
PASSWORD_FILE="$HOME/.vnc/passwd"
#
# X fonts.
#
VNC_FONTS=\
/usr/lib/X11/fonts/misc/,\
/usr/lib/X11/fonts/75dpi/:unscaled,\
/usr/lib/X11/fonts/100dpi/:unscaled,\
/usr/lib/X11/fonts/75dpi/,\
/usr/lib/X11/fonts/100dpi/,\
/usr/lib/X11/fonts/Type1/,\
/usr/lib/X11/fonts/Speedo/
#
# Command line arguments.
#
PROGRAM_NAME=`basename $0`
ARGUMENTS="$@"
#
# Main menu.
#
# srvr  = server
# cclnt = controlling client
# vclnt = viewing client
#
main_menu () {
    if dialog   \
        --clear \
        --title "VNC run commands" \
        --menu "VNC server works at \":1\" and X works at \":0\"" \
        0 0 0 \
        "vncs"            "srvr             + new password" \
        "vncsc"           "srvr + cclnt + X + new password" \
        "vnccx"           "       cclnt + X + password \"$PASSWORD_FILE\"" \
        "vncvx"           "       vclnt + X + password \"$PASSWORD_FILE\"" \
        "vncc"            "       cclnt + X + password \"$PASSWORD_FILE\"" \
        "vncv"            "       vclnt + X + password \"$PASSWORD_FILE\"" \
        "sharedx"         "srvr + cclnt + X + default password" \
        "takesharedx"     "       cclnt + X + default password" \
        "viewsharedx"     "       vclnt + X + default password" \
        2> $TEMPORARY
    then
        SELECTION=`cat $TEMPORARY`
        echo "" > $TEMPORARY
        #
        # Test if the command can be done
        #
        if false
        then
            #
            # There are troubles... (to be done).
            #
            dialog --msgbox "There are unknown troubles..." 0 0
        else
            #
            # Continue as run with this command name (arguments included).
            #
            PROGRAM_NAME="$SELECTION"
        fi
    fi
}
#
# sharedx.
#
start_shared_x () {
    #
    # Quit old VNC servers and reset personal configuration.
    #
    killall Xvnc 2> /dev/null
    killall Xrealvnc 2> /dev/null
    killall Xtightvnc 2> /dev/null
    rm -f    $PASSWORD_FILE
    echo "$ENCRIPTED_PASSWORD" > $PASSWORD_FILE
    chmod 0600 $PASSWORD_FILE
    #
    # Start VNC server at screen :1, using ~/.vnc/log for log file.
    #
    Xvnc :1 -auth ~/.Xauthority -geometry $GEOMETRY -depth $DEPTH \
        -rfbwait 120000 -rfbauth $PASSWORD_FILE -rfbport 5901 \
        -fp $VNC_FONTS -co /etc/X11/rgb \
        -alwaysshared nologo -dpi 100 2> `dirname $PASSWORD_FILE`/log &
    #
    # If it does not work, start it with vncserver:
    #
    # vncserver :1 -auth ~/.Xauthority -geometry $GEOMETRY -depth $DEPTH \
    #    -rfbwait 120000 -rfbauth $PASSWORD_FILE -rfbport 5901 \
    #    -fp $VNC_FONTS -co /etc/X11/rgb \
    #    -alwaysshared nologo -dpi 100 \
    #    -startup true 2> `dirname $PASSWORD_FILE`/log &
    #
    # Window manager.
    #
    start_vnc_window_manager &
    #
    # Start xinit with xvncviewer as a client
    #
    xinit /usr/bin/xvncviewer -fullscreen -passwd $PASSWORD_FILE localhost:1
}
#
# vncs.
#
start_vncs () {
    #
    # Quit old VNC servers and reset personal configuration.
    #
    killall Xvnc 2> /dev/null
    killall Xrealvnc 2> /dev/null
    killall Xtightvnc 2> /dev/null
    rm -f    $PASSWORD_FILE
    #
    if ! vncpasswd $PASSWORD_FILE
    then
        #
        dialog --msgbox "The password is not valid, sorry." 0 0
        #
        exit
        #
    fi
    #
    # Start VNC server at screen :1, using ~/.vnc/log for log file.
    #
    Xvnc :1 -auth ~/.Xauthority -geometry $GEOMETRY -depth $DEPTH \
        -rfbwait 120000 -rfbauth $PASSWORD_FILE -rfbport 5901 \
        -fp $VNC_FONTS -co /etc/X11/rgb \
        -alwaysshared nologo -dpi 100 2> `dirname $PASSWORD_FILE`/log &
    #
    # If it does not work, start it with vncserver:
    #
    # vncserver :1 -auth ~/.Xauthority -geometry $GEOMETRY -depth $DEPTH \
    #    -rfbwait 120000 -rfbauth $PASSWORD_FILE -rfbport 5901 \
    #    -fp $VNC_FONTS -co /etc/X11/rgb \
    #    -alwaysshared nologo -dpi 100 \
    #    -startup true 2> `dirname $PASSWORD_FILE`/log &
    #
    # Window manager.
    #
    start_vnc_window_manager &
}
#
# vncwm.
#
start_vnc_window_manager () {
    #
    xsetroot -display :1 -solid NavyBlue
    #
    if [ -f ~/.backgroundcolor ]
    then
        xsetroot -display :1 -solid `cat ~/.backgroundcolor`
    fi
    #
    if [ -f ~/.wallpaper ]
    then
        display -display :1 -window root -backdrop ~/.wallpaper
    fi
    #
    # Start fvwm and kill VNC when it dies.
    #
    fvwm2 -display :1  2> /dev/null ; killall Xvnc 2> /dev/null ; \
                                      killall Xrealvnc 2> /dev/null ; \
                                      killall Xtightvnc 2> /dev/null
    #
}
#
# Common viewer procedure
#
vnc_viewer_procedure () {
    #
    # The default password file will be used if available.
    #
    if [ -f $PASSWORD_FILE ]
    then
        #
        dialog --msgbox "There is already a file $PASSWORD_FILE. \
If authentication fails, try to remove it." 0 0
        #
    else
        if ! vncpasswd $PASSWORD_FILE
        then
            #
            dialog --msgbox "The password is not valid, \
I will try with a default one." 0 0
            #
            # Try with default password.
            #
            echo "$ENCRIPTED_PASSWORD" > $PASSWORD_FILE
        fi
    fi
    #
    # Check for arguments.
    #
    if [ "$ARGUMENTS" = "" ]
    then
        #
        # Give address.
        #
        if dialog  \
            --title "remote VNC server address" \
            --inputbox \
                "Please insert the remote VNC server address. \
The connection will be tried at screen \":1\"." \
            0 0 \
            "$VNC_REMOTE_SERVER" \
            2> $TEMPORARY
        then
            ARGUMENTS=`cat $TEMPORARY`
            echo "" > $TEMPORARY
        fi
    fi
    #
    # Save data.
    #
    VNC_REMOTE_SERVER="$ARGUMENTS"
    rm -f /etc/nlnx/all/VNC_REMOTE_SERVER
    echo "$VNC_REMOTE_SERVER" > /etc/nlnx/all/VNC_REMOTE_SERVER
}
#
# Take/view shared x procedure
#
vnc_viewer_shared_x_procedure () {
    #
    # Default password
    #
    rm -f    $PASSWORD_FILE
    echo "$ENCRIPTED_PASSWORD" > $PASSWORD_FILE
    chmod 0600 $PASSWORD_FILE
    #
    # Check for arguments.
    #
    if [ "$ARGUMENTS" = "" ]
    then
        #
        # Give address.
        #
        if dialog  \
            --title "remote VNC server addresses" \
            --inputbox \
                "Please insert the remote VNC server addresses. \
The connection will be tried at screen \":1\"." \
            0 0 \
            "$VNC_REMOTE_SERVERS" \
            2> $TEMPORARY
        then
            ARGUMENTS=`cat $TEMPORARY`
            echo "" > $TEMPORARY
        fi
    fi
    #
    # Save data.
    #
    VNC_REMOTE_SERVERS="$ARGUMENTS"
    rm -f /etc/nlnx/all/VNC_REMOTE_SERVERS
    echo "$VNC_REMOTE_SERVERS" > /etc/nlnx/all/VNC_REMOTE_SERVERS
}
#
# Geometry selection.
#
select_geometry () {
    if dialog   \
        --clear \
        --title "geometry" \
        --menu "Select VNC server geometry:" \
        0 0 0 \
        "1024x768"              "default" \
        "1440x1080"             "" \
        "1408x1056"             "" \
        "1376x1032"             "" \
        "1344x1008"             "" \
        "1312x984"              "" \
        "1280x960"              "" \
        "1248x936"              "" \
        "1216x912"              "" \
        "1184x888"              "" \
        "1152x864"              "" \
        "1120x840"              "" \
        "1088x816"              "" \
        "1056x792"              "" \
        "1024x768"              "usual resolution" \
        "992x744"               "" \
        "960x720"               "" \
        "928x696"               "" \
        "896x672"               "" \
        "864x648"               "" \
        "832x624"               "" \
        "800x600"               "old SVGA resolution" \
        "768x576"               "" \
        "736x552"               "" \
        "704x528"               "" \
        "672x504"               "" \
        "640x480"               "old VGA resolution" \
        "608x456"               "" \
        "576x432"               "" \
        "544x408"               "" \
        "512x384"               "" \
        "480x360"               "" \
        2> $TEMPORARY
    then
        SELECTION=`cat $TEMPORARY`
        echo "" > $TEMPORARY
        GEOMETRY="$SELECTION"
    else
        #
        # Set to a default
        #
        GEOMETRY="1024x768"
    fi
}
#
# Write to log.
#
log_vnc_view () {
    logger "User $USER trying to view VNC server $1"
}
log_vnc_control () {
    logger "User $USER trying to control VNC server $1"
}
#
# Start the command selection
#
if [ "$PROGRAM_NAME" = "vncrc" ]
then
    main_menu
fi
#
# Now the program name might be changed.
#
if   [ "$PROGRAM_NAME" = "sharedx" ]
then
    select_geometry
    DEPTH="16"
    start_shared_x
elif [ "$PROGRAM_NAME" = "vncs" ]
then
    select_geometry
    DEPTH="16"
    start_vncs
elif [ "$PROGRAM_NAME" = "vnccx" ]
then
    #
    vnc_viewer_procedure
    #
    # Please note that you cannot put it in background!
    #
    xinit /usr/bin/xvncviewer -fullscreen -passwd $PASSWORD_FILE \
        $VNC_REMOTE_SERVER:1
    #
elif [ "$PROGRAM_NAME" = "vncvx" ]
then
    #
    vnc_viewer_procedure
    #
    log_vnc_view $VNC_REMOTE_SERVER:1
    #
    # Please note that you cannot put it in background!
    #
    xinit /usr/bin/xvncviewer -viewonly -fullscreen -passwd $PASSWORD_FILE \
        $VNC_REMOTE_SERVER:1
    #
elif [ "$PROGRAM_NAME" = "vncc" ]
then
    #
    vnc_viewer_procedure
    #
    log_vnc_control $VNC_REMOTE_SERVER:1
    #
    /usr/bin/xvncviewer -passwd $PASSWORD_FILE $VNC_REMOTE_SERVER:1 &
    #
elif [ "$PROGRAM_NAME" = "vncv" ]
then
    #
    vnc_viewer_procedure
    #
    log_vnc_view $VNC_REMOTE_SERVER:1
    #
    /usr/bin/xvncviewer -viewonly -passwd $PASSWORD_FILE $VNC_REMOTE_SERVER:1 &
    #
elif [ "$PROGRAM_NAME" = "vncsc" ]
then
    select_geometry
    DEPTH="16"
    start_vncs
    xinit /usr/bin/xvncviewer -fullscreen -passwd $PASSWORD_FILE localhost:1
    #
elif [ "$PROGRAM_NAME" = "takesharedx" ]
then
    #
    vnc_viewer_shared_x_procedure
    #
    for a in $ARGUMENTS
    do
        #
        log_vnc_control $a:1
        #
        /usr/bin/xvncviewer -passwd $PASSWORD_FILE "$a:1" -title "$a:1" &
    done
    #
elif [ "$PROGRAM_NAME" = "viewsharedx" ]
then
    #
    vnc_viewer_shared_x_procedure
    #
    for a in $ARGUMENTS
    do
        #
        log_vnc_view $a:1
        #
        /usr/bin/xvncviewer -viewonly -passwd $PASSWORD_FILE "$a:1" \
            -title "$a:1" &
    done
    #
fi
