2023-06-11 12:46:46 +02:00
|
|
|
#!/bin/bash
|
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
|
|
|
|
2023-06-11 12:53:27 +02:00
|
|
|
# Variable
|
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:53:27 +02:00
|
|
|
# echo bold text
|
2023-06-11 12:46:46 +02:00
|
|
|
echobf() {
|
|
|
|
printf '\033[1m%s\033[0m\n' "$*"
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:53:27 +02:00
|
|
|
# Print an error message
|
2023-06-11 12:46:46 +02:00
|
|
|
print_error() {
|
2023-06-11 13:10:26 +02:00
|
|
|
(
|
|
|
|
printf '\033[1;31m%s\033[0m ' "ERROR:"
|
|
|
|
echobf "$*"
|
|
|
|
) > /dev/stderr
|
2023-05-07 22:08:11 +02:00
|
|
|
}
|
|
|
|
|
2023-06-11 12:53:27 +02:00
|
|
|
# Print an error and exit
|
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:53:27 +02:00
|
|
|
# Check if the VNC server is running
|
2023-06-11 12:46:46 +02:00
|
|
|
is_running() {
|
|
|
|
vncserver -list | grep -q '^:1'
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:53:27 +02:00
|
|
|
# Show a help message
|
2023-06-11 12:46:46 +02:00
|
|
|
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 13:10:26 +02:00
|
|
|
${name} ${verbs// /|}
|
2023-05-01 00:11:01 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:53:27 +02:00
|
|
|
# Show the same help with an error
|
2023-06-11 12:46:46 +02:00
|
|
|
error_help() {
|
|
|
|
print_error "Invalid usage"
|
2023-06-11 12:53:27 +02:00
|
|
|
>&2 public_help
|
2023-06-11 12:46:46 +02:00
|
|
|
return 1
|
2023-05-01 00:11:01 +02:00
|
|
|
}
|
|
|
|
|
2023-06-11 12:53:27 +02:00
|
|
|
# Start the VNC server
|
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:53:27 +02:00
|
|
|
# Check if the server is running
|
2023-06-11 12:46:46 +02:00
|
|
|
public_status() {
|
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."
|
2023-06-11 12:53:27 +02:00
|
|
|
tail "./$(hostname):1.log" | 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:53:27 +02:00
|
|
|
# Stop the VNC server
|
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:53:27 +02:00
|
|
|
# Restart the VNC server
|
2023-06-11 12:46:46 +02:00
|
|
|
public_restart() {
|
2023-05-12 14:59:13 +02:00
|
|
|
stop
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:53:27 +02:00
|
|
|
# Parse argument
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
arg="$1"
|
|
|
|
else
|
|
|
|
arg=status
|
|
|
|
fi
|
2023-05-01 00:11:01 +02:00
|
|
|
|
2023-06-11 12:53:27 +02:00
|
|
|
# Main switch
|
2023-06-11 12:46:46 +02:00
|
|
|
if echo "$verbs" | grep -q "$arg"; then
|
|
|
|
"public_${arg}"
|
|
|
|
else
|
2023-06-11 13:10:26 +02:00
|
|
|
error_help
|
2023-06-11 12:46:46 +02:00
|
|
|
fi
|
2023-04-13 00:04:20 +02:00
|
|
|
|