#!/bin/sh
# $Id: vnc,v 1.5 2019/06/19 22:09:53 friedman Exp $

xgeom()
{
    xrdb -symbols -screen |
        sort |
        sed -e /-DHEIGHT=/h \
            -e /-DWIDTH=/H  \
            -e '$!d'        \
            -e x            \
            -e 's/^.*=\(.*\)\n.*=\(.*\)/\2x\1/'
}

run_vnc()
{
    exec >> $HOME/.xinitrc.log 2>&1 < /dev/null
    case ${passwd-notset} in
        notset ) exec with -s run-misc.sh vncviewer "$@" ;;
    esac

    echo -n "$passwd" |
        vncpasswd -f  |
        with -s run-misc.sh vncviewer "$@"
}

tigervnc()
{
    case ${passwd+isset} in
        isset ) set : -PasswordFile=/dev/stdin "$@"; shift ;;
    esac
    run_vnc -Shared -FullscreenSystemKeys=0 "$@"
}

realvnc()
{
    run_vnc -AlwaysShared -Shared "$@"
}

tightvnc()
{
    encodings='copyrect tight hextile zlib corre rre'
    run_vnc -encodings "$encodings" "$@"
}

vnc_client()
{
    case $fs in
        t ) set : -FullScreen "$@"
            shift ;;
    esac

    case $mac in
        t ) set : -FullColor -PreferredEncoding hextile "$@"
            shift ;;
    esac

    case $vncversion in
        *TigerVNC*      ) tigervnc "$@" ;;
        *"RealVNC Ltd"* ) realvnc  "$@" ;;
        *TightVNC*      ) tightvnc "$@" ;;
    esac
}

main()
{
    vncversion=`vncviewer --version 2>&1`

    while :; do
        case $1 in
            -pw  ) passwd=$2; shift ; shift ;;
            -fs  ) fs=t;  shift ;;
            -mac ) mac=t; shift ;;
            *    ) break ;;
        esac
    done

    vnc_client "$@"
}

main "$@"

# eof
