#!/usr/bin/env bash

set -e
set -o pipefail

help_main()
{
    echo "\
Usage: vnc COMMANDS ARGS...
Run COMMANDS with ARGS

COMMAND must be one of the sub-commands listed below:

   mjru
   vm
   server

Report bugs to: go.wigust@gmail.com."
}

help_server()
{
    echo "\
Usage: vnc server [OPTION] PORT...
Run vnc server at PORT.

  -h, --help             display this help and exit

Report bugs to: go.wigust@gmail.com."
}

case "$1" in
    --help)
        help_main
        ;;
    client)
        case "$2" in
            vm1*)
                # NOTE: Could append X509Plain and VncAuth separated by command to SecurityTypes.
                ssh vm1.wugi.info -- "pgrep -fa 'Xvnc :1'" \
                    || ssh vm1.wugi.info -- 'vncserver -X509Key ~/.vnc/key.pem -X509Cert ~/.vnc/x509_ca.pem -SecurityTypes X509Vnc :1'
                vncviewer -passwd ~/.vnc/passwd X509CA=/home/oleg/.vnc/x509_ca.pem vm1.wugi.info:5901
                ;;
            mjru)
                ssh majordomo.intr -- "pgrep -fa 'Xvnc :3'" \
                    || ssh majordomo.intr -- 'vncserver -xstartup "$HOME/.vnc/xstartup" :3'
                ss -o state listening '( dport = 59588 or sport = 59588 )' | grep -q :59588 \
                    || ssh -fNL 59588:localhost:5903 majordomo.intr
                vncviewer -PasswordFile ~/.vnc/passwd -NoJpeg localhost:59588
                ;;
            vm)
                ss -o state listening '( dport = 5911 or sport = 5911 )' | grep -q :5911 \
                    || ssh -fNL 5911:localhost:5901 guix.vm.wugi.info
                vncviewer -AutoSelect=0 -PreferredEncoding=Raw -FullColor=1 -NoJPEG=1 -CompressLevel=0 -passwd ~/.vnc/passwd 127.0.0.1:5911
                ;;
            novnc)
                docker run --restart unless-stopped --detach --name novnc --network=host geek1011/easy-novnc:latest --addr 127.0.0.1:5911 --port 5910 --novnc-params "resize=remote"
                ;;
            guix)
                vnc_directory="$(mktemp -d -t "vnc.XXXXXXXXXX")"
                trap 'rm -rf "$vnc_directory"' EXIT
                (
                    cd "$vnc_directory" || exit 1
                    pass show localhost/vncserver \
                        | guix shell tigervnc-server -- vncpasswd -f > passwd
                    if [[ $HOSTNAME == "guixsd" ]]
                    then
                        X509CA="${HOME}/.vnc/x509_ca.pem" vncviewer -passwd passwd -Shared "192.168.0.144:${3:-5902}"
                    fi
                )
                ;;
            windows)
                vnc_directory="$(mktemp -d -t "vnc.XXXXXXXXXX")"
                trap 'rm -rf "$vnc_directory"' EXIT
                (
                    cd "$vnc_directory" || exit 1
                    pass show windows.local/oleg \
                        | guix shell tigervnc-server -- vncpasswd -f > passwd
                    vncviewer -passwd passwd "windows.local"
                )
                ;;
            *)
                vncviewer -AutoSelect=0 -PreferredEncoding=Raw -FullColor=1 -NoJPEG=1 -CompressLevel=0 -passwd "$HOME/.vnc/passwd" 127.0.0.1:"$2"
                ;;
        esac
        ;;
    server)
        case "$2" in
            --help)
                help_server
                ;;
            5901|firefox)
                vncserver -xstartup "$HOME/.vnc/xstartup-firefox" :1
                ;;
            5902|workstation)
                vncserver -geometry 1920x1080 -xstartup "$HOME/.vnc/xstartup-stumpwm" :2
                ;;
            5903|small)
                vncserver -geometry 1366x768 -xstartup "$HOME/.vnc/xstartup-stumpwm" :3
                ;;
            5910|quassel)
                vncserver -xstartup "$HOME/.vnc/xstartup-quassel" :10
                ;;
            *)
                help_server
                ;;
        esac
        ;;
    ssl)
        case "$2" in
            vm1*)
                # [[https://zalinux.ru/?p=3905][Как настроить и использовать сервер TigerVNC в Linux - ZaLinux.ru]]
                openssl req -x509 -newkey rsa -days 365 -nodes -keyout key.pem -out x509_ca.pem -subj '/CN=vm1.wugi.info'
                ;;
            guix.wugi.info)
                # [[https://zalinux.ru/?p=3905][Как настроить и использовать сервер TigerVNC в Linux - ZaLinux.ru]]
                openssl req -x509 -newkey rsa -days 365 -nodes -keyout key.pem -out x509_ca.pem -subj '/CN=guix.wugi.info'
                install --backup x509_ca.pem "$HOME"/.vnc/x509_ca.pem
                install --backup key.pem "$HOME"/.vnc/key.pem
                ;;
        esac
        ;;
    password)
        # vnc password PASSWORD_TEXT kvm12.intr:12345
        vnc_directory="$(mktemp -d -t "vnc.XXXXXXXXXX")"
        (
            set -x
            trap 'rm -rf "$vnc_directory"' EXIT
            cd "$vnc_directory"
            echo "$2" | guix shell tigervnc-server -- vncpasswd -f > passwd
            command vncviewer -passwd passwd "$3"
        )
        ;;
    *)
        help_main
        ;;
esac
