2023-06-11 12:46:46 +02:00
|
|
|
#!/bin/bash
|
2023-05-14 16:02:54 +02:00
|
|
|
# shellcheck disable=SC2317
|
2023-05-01 00:11:01 +02:00
|
|
|
|
|
|
|
# cd to the local VNC directory, exit if it fails
|
2023-05-01 00:17:12 +02:00
|
|
|
cd "${HOME}/.vnc" || exit 5
|
2023-04-13 00:04:20 +02:00
|
|
|
|
|
|
|
# Variables
|
2023-05-01 00:11:01 +02:00
|
|
|
file_base="./$(hostname):1."
|
2023-06-11 12:46:46 +02:00
|
|
|
verbs="start status stop restart help"
|
2023-05-01 00:11:01 +02:00
|
|
|
|
2023-06-11 12:46:46 +02:00
|
|
|
echobf() {
|
|
|
|
printf '\033[1m%s\033[0m\n' "$*"
|
|
|
|
}
|
|
|
|
|
|
|
|
print_error() {
|
|
|
|
>&2 (
|
|
|
|
printf '\033[1;31m%s\033[0m ' "ERROR:"
|
|
|
|
echobf "$*"
|
|
|
|
)
|
2023-05-07 22:08:11 +02:00
|
|
|
}
|
|
|
|
|
2023-05-01 00:11:01 +02:00
|
|
|
error() {
|
2023-06-11 12:46:46 +02:00
|
|
|
print_error "$1"
|
2023-05-01 00:11:01 +02:00
|
|
|
shift
|
|
|
|
exit "$1"
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:46:46 +02:00
|
|
|
is_running() {
|
|
|
|
vncserver -list | grep -q '^:1'
|
|
|
|
}
|
|
|
|
|
|
|
|
public_help() {
|
|
|
|
local name
|
2023-05-01 00:11:01 +02:00
|
|
|
name="$(basename "$0")"
|
|
|
|
cat << EOF
|
|
|
|
${name} - Start a VNC server
|
|
|
|
|
|
|
|
Usage:
|
2023-06-11 12:46:46 +02:00
|
|
|
${name} $(echo "$verbs" | sed 's/ /|/g')
|
2023-05-01 00:11:01 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:46:46 +02:00
|
|
|
error_help() {
|
|
|
|
print_error "Invalid usage"
|
|
|
|
public_help
|
|
|
|
return 1
|
2023-05-01 00:11:01 +02:00
|
|
|
}
|
|
|
|
|
2023-06-11 12:46:46 +02:00
|
|
|
public_start() {
|
2023-05-01 00:11:01 +02:00
|
|
|
if ! is_running; then
|
2023-05-07 21:59:23 +02:00
|
|
|
set -e
|
2023-05-20 10:42:13 +02:00
|
|
|
vncserver \
|
2023-05-20 23:27:42 +02:00
|
|
|
-xstartup ./xstartup \
|
2023-05-20 10:42:13 +02:00
|
|
|
-localhost \
|
|
|
|
-alwaysshared \
|
|
|
|
-securitytypes none
|
2023-05-01 00:11:01 +02:00
|
|
|
else
|
2023-05-01 00:17:12 +02:00
|
|
|
error "The VNC server is already running!" 4
|
2023-05-01 00:11:01 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:46:46 +02:00
|
|
|
public_status() {
|
|
|
|
local log_f="${file_base}log"
|
2023-05-01 00:11:01 +02:00
|
|
|
if is_running; then
|
2023-06-11 12:46:46 +02:00
|
|
|
echobf "The VNC server is running."
|
|
|
|
tail "$log_f" | sed 's/^/\t/g'
|
2023-05-01 00:11:01 +02:00
|
|
|
else
|
2023-06-11 12:46:46 +02:00
|
|
|
echobf "The VNC server is not running."
|
2023-05-01 00:11:01 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:46:46 +02:00
|
|
|
public_stop() {
|
2023-05-01 00:11:01 +02:00
|
|
|
if is_running; then
|
|
|
|
vncserver -kill :1
|
|
|
|
else
|
|
|
|
error "The VNC server is not running!" 3
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:46:46 +02:00
|
|
|
public_restart() {
|
2023-05-12 14:59:13 +02:00
|
|
|
stop
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:11:01 +02:00
|
|
|
# Argument parsing
|
|
|
|
set -e; trap 'set +e; error "$error" "$?"' EXIT
|
2023-06-11 12:46:46 +02:00
|
|
|
error="You must give an argument"; [ -n "$1" ]; arg="$1"; shift; unset error
|
2023-05-01 00:11:01 +02:00
|
|
|
set +e; trap - EXIT
|
|
|
|
|
|
|
|
# Main case statement
|
2023-06-11 12:46:46 +02:00
|
|
|
if echo "$verbs" | grep -q "$arg"; then
|
|
|
|
"public_${arg}"
|
|
|
|
else
|
|
|
|
error_usage
|
|
|
|
fi
|
2023-04-13 00:04:20 +02:00
|
|
|
|