1
0
Fork 0
forked from ahurac/dotfiles
ahuarc-dotfiles/bin/headlessvnc
2023-06-11 13:10:26 +02:00

105 lines
1.7 KiB
Bash
Executable file

#!/bin/bash
# cd to the local VNC directory, exit if it fails
cd "${HOME}/.vnc" || exit 5
# Variable
verbs="start status stop restart help"
# echo bold text
echobf() {
printf '\033[1m%s\033[0m\n' "$*"
}
# Print an error message
print_error() {
(
printf '\033[1;31m%s\033[0m ' "ERROR:"
echobf "$*"
) > /dev/stderr
}
# Print an error and exit
error() {
print_error "$1"
shift
exit "$1"
}
# Check if the VNC server is running
is_running() {
vncserver -list | grep -q '^:1'
}
# Show a help message
public_help() {
local name
name="$(basename "$0")"
cat << EOF
${name} - Start a VNC server
Usage:
${name} ${verbs// /|}
EOF
}
# Show the same help with an error
error_help() {
print_error "Invalid usage"
>&2 public_help
return 1
}
# Start the VNC server
public_start() {
if ! is_running; then
set -e
vncserver \
-xstartup ./xstartup \
-localhost \
-alwaysshared \
-securitytypes none
else
error "The VNC server is already running!" 4
fi
}
# Check if the server is running
public_status() {
if is_running; then
echobf "The VNC server is running."
tail "./$(hostname):1.log" | sed 's/^/\t/g'
else
echobf "The VNC server is not running."
fi
}
# Stop the VNC server
public_stop() {
if is_running; then
vncserver -kill :1
else
error "The VNC server is not running!" 3
fi
}
# Restart the VNC server
public_restart() {
stop
start
}
# Parse argument
if [ -n "$1" ]; then
arg="$1"
else
arg=status
fi
# Main switch
if echo "$verbs" | grep -q "$arg"; then
"public_${arg}"
else
error_help
fi