dotfiles/bin/wolf

54 lines
861 B
Text
Raw Normal View History

2023-04-13 00:04:20 +02:00
#!/bin/bash
# Arguments
hostname="$1"; shift
2023-04-13 00:04:20 +02:00
# Variables
declare mac
declare -a wol_com options
name="$(basename "$0")"
wolf_d="${HOME}/.wolf"
wolf_host="${wolf_d}/${hostname}"
# Functions
# Throw an error
function error {
>&2 echo -e "${name}: \033[1;31mERROR:\033[0m ${1:-No further details}"; shift
exit "${1:-1}"
}
# Parse variable in options file
function getvar {
grep '^'"$1"' ' "$wolf_host" | cut -d ' ' -f 2
}
# Parsing
[[ -f $wolf_host ]] || error "\"${hostname}\": No such host"
# Assign the options
mac="$(getvar Mac)"
ip="$(getvar IP)"
port="$(getvar Port)"
# Add options if necessary
[[ -n $ip ]] && options+=(-i "$ip" )
[[ -n $port ]] && options+=(-p "$port")
# Commands building
# wol
wol_com=(
wol
"$@"
"${options[@]}"
"$mac"
)
# Debug
#echo "${wol_com[@]}"
#exit 0
# Execution
set -xe
"${wol_com[@]}"