1
0
Fork 0
forked from ahurac/dotfiles
ahuarc-dotfiles/bin/vncctl
2023-05-12 15:18:32 +02:00

91 lines
1.4 KiB
Bash
Executable file

#!/bin/sh
# cd to the local VNC directory, exit if it fails
cd "${HOME}/.vnc" || exit 5
# Variables
file_base="./$(hostname):1."
verbs="start|status|stop|restart|help"
echol() {
col="$1"
shift
printf '\033['"${col}"'m%s\033[0m\n' "$*"
}
error() {
>&2 printf '\033[1;31m%s\033[0m %s\n' "ERROR:" "$1"
shift
exit "$1"
}
help() {
name="$(basename "$0")"
cat << EOF
${name} - Start a VNC server
Usage:
${name} ${verbs}
EOF
}
usage() {
>&2 help
error "Invalid usage: ${1}" 1
}
is_running() {
vncserver -list | grep -q '^:1'
}
start() {
if ! is_running; then
set -e
vncserver > /dev/null
echo 'export DISPLAY=:1;'
else
error "The VNC server is already running!" 4
fi
}
status() {
log_f="${file_base}log"
if is_running; then
echol '1;32' "The VNC server is running."
tail "$log_f"
else
echol '1;31' "The VNC server is not running."
fi
}
stop() {
if is_running; then
vncserver -kill :1
else
error "The VNC server is not running!" 3
fi
}
restart() {
stop
start
}
# Argument parsing
set -e; trap 'set +e; error "$error" "$?"' EXIT
error="You must give an argument"
[ -n "$1" ]; arg="$1"; shift
unset error
set +e; trap - EXIT
# Main case statement
case "$arg" in
status|start|stop|restatr|help)
"$arg"
;;
*)
usage "Invalid argument \"$arg\""
;;
esac