headlessvnc : ré-écriture complète

This commit is contained in:
Ahurac 2024-04-04 15:26:55 +02:00
parent cb3fc72d2b
commit 58ee6d737a

View file

@ -1,107 +1,59 @@
#!/bin/bash #!/usr/bin/env sh
perror() {
# cd to the local VNC directory, exit if it fails >&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
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 error_usage() {
print_error() { perror "invalid usage"
( >&2 usage
printf '\033[1;31m%s\033[0m ' "ERROR:"
echobf "$*" exit 1
) > /dev/stderr
} }
# Print an error and exit
error() {
print_error "$1"
shift
exit "$1"
}
# Check if the VNC server is running
is_running() { is_running() {
vncserver -list | grep -q '^:1' vncserver -list | grep -q '^:1'
} }
# Show a help message start() {
public_help() { if ! is_running; then
local name set -- vncserver \
name="$(basename "$0")" -localhost -alwaysshared \
cat << EOF -securitytypes none
${name} - Start a VNC server [ -x ~/.vnc/xstartup ] && set -- "$@" -xstartup ~/.vnc/xstartup
Usage: "$@"
${name} ${verbs// /|} else
EOF perror "server already started"
exit 2
fi
} }
# Show the same help with an error stop() {
error_help() { if is_running; then
print_error "Invalid usage" vncserver -kill :1
>&2 public_help else
return 1 perror "server is not running"
exit 2
fi
} }
# Start the VNC server status() {
public_start() { if is_running; then
if ! is_running; then printf '\033[1;32m*\033[0m \033[1m%s\n' "the server is running"
set -e else
vncserver \ printf '\033[1;31m*\033[0m \033[1m%s\n' "the server is not running"
-xstartup ./xstartup \ fi
-localhost \
-alwaysshared \
-securitytypes none \
-nocursor \
-geometry 1600x900
else
error "The VNC server is already running!" 4
fi
} }
# Check if the server is running case "$1" in
public_status() { ""|status) status ;;
if is_running; then start) start ;;
echobf "The VNC server is running." stop) stop ;;
tail "./$(cat /proc/sys/kernel/hostname):1.log" | sed 's/^/\t/g' restart)
else stop
echobf "The VNC server is not running." start
fi ;;
} *)
error_usage
# Stop the VNC server ;;
public_stop() { esac
if is_running; then
vncserver -kill :1
else
error "The VNC server is not running!" 3
fi
}
# Restart the VNC server
public_restart() {
public_stop
public_start
}
# Parse argument
if [ -n "$1" ]; then
arg="$1"
else
arg=status
fi
# Main switch
if echo "$verbs" | tr ' ' '\n' | grep -q "$arg"; then
"public_${arg}"
else
error_help
fi