configs/bin/dgl

109 lines
2.1 KiB
Bash
Executable File

#! /bin/bash
#
# General launcher script for dgamelaunch actions. The idea is to symlink
# this (one) script into $HOME/bin and use it to run the others.
SELF="$0"
SCRIPT_NAME=$(basename $0)
usage() {
echo -e "Usage: $SCRIPT_NAME $@"
}
cat-error() {
cat >&2
}
report-usage() {
printf "Usage: $SCRIPT_NAME " >&2
cat-error
exit 1
}
canonical-name() {
local name=$1
if [[ -d "$name" ]]; then
printf "%s\n" "$(cd $name && echo $PWD)"
else
local dir=$(cd $(dirname $name) && echo $PWD)
printf "%s\n" "$dir/$(basename $name)"
fi
}
test-set-dgl-home() {
if [[ -n "$DGL_CONF_HOME" ]]; then
return 0
fi
local dir=$1
if [[ -d "$dir" && -d "$dir/bin" && -f "$dir/dgamelaunch.conf" \
&& -d "$dir/chroot" ]]; then
export DGL_CONF_HOME="$(canonical-name $dir)"
export DGL_CONF_LOGS="$dir/logs"
mkdir -p "$DGL_CONF_LOGS"
fi
}
find-dgl-home() {
test-set-dgl-home "$HOME/conf-dgamelaunch"
test-set-dgl-home "$(dirname $SELF)/.."
local link_target="$(readlink $0)"
if [[ -n "$link_target" ]]; then
test-set-dgl-home "$(dirname $(readlink $SELF))/.."
fi
if [[ -z "$DGL_CONF_HOME" ]]; then
echo -e "Cannot find dgamelaunch config base directory"
return 1
fi
}
resolve-action() {
local action="$1"
local script="$DGL_CONF_HOME/bin/$action.sh"
if [[ ! -f "$script" ]]; then
cat-error <<EOF
Can't find dgamelaunch action '$action'
Available actions are:
$(list-all-actions)
EOF
exit 1
fi
echo "$script"
}
find-dgl-home || exit 1
ACTION=$1
shift
find-all-actions() {
local -a actions
actions=($DGL_CONF_HOME/bin/*.sh)
actions=(${actions[@]%%.sh})
actions=(${actions[@]##$DGL_CONF_HOME/bin/})
echo "${actions[@]}"
}
list-all-actions() {
find-all-actions | fold -s
}
if [[ -z "$ACTION" ]]; then
usage "<action>"
echo -e "Where <action> is one of:"
list-all-actions >&2
exit 1
fi
ACTION_SCRIPT="$(resolve-action $ACTION)"
[[ -z "$ACTION_SCRIPT" ]] && exit 1
cd "$DGL_CONF_HOME"
source ./sh-utils
SCRIPT_NAME="$SCRIPT_NAME $ACTION"
source "$ACTION_SCRIPT"