docmachine-cli/old/docmachine
2023-01-18 16:29:34 +01:00

135 lines
3 KiB
Bash
Executable file

#!/bin/sh
# vim: set ft=sh:
# Set defaults
BASEDIR="$(pwd)"
BASEHASH="$(echo "$BASEDIR" |sha256sum |head -c7)"
ACTION="watch"
VERBOSITY="0"
DOCKER_IMAGE=glenux/docmachine:latest
DOCKER_OPT_TTY="-t"
gxt_usage() {
echo "Usage: $0 [options]"
echo ""
echo "-d, --data-dir DIR Content directory"
echo "-a, --action ACTION Action (watch, build, shell, etc.)"
echo "-t, --tty Enable TTY mode (needed for shell)"
echo "-v, --verbose Enable verbosity"
echo "-h, --help Show this help"
}
# Parse arguments
while [ $# -gt 0 ]; do
ARG="${1:-}"
OPT="${2:-}"
shift
# echo "DEBUG: ARG=$ARG OPT=$OPT"
case "$ARG" in
-a|--action)
shift # argument requires a parameter
if [ -z "$OPT" ]; then
>&2 echo "ERROR: missing argument for --action"
exit 1
fi
ACTION="$OPT"
;;
-d|--data-dir)
shift # argument requires a parameter
if [ -z "$OPT" ]; then
>&2 echo "ERROR: missing argument for --data-dir"
exit 1
fi
if [ ! -d "$OPT" ]; then
>&2 echo "ERROR: argument for --data-dir must be a directory"
exit 1
fi
BASEDIR="$(cd "$OPT" && pwd)"
;;
-h|--help)
>&2 gxt_usage
exit 0
;;
-T|--no-tty)
DOCKER_OPT_TTY=""
;;
-v|--verbosity)
VERBOSITY="$((VERBOSITY+1))"
;;
*)
>&2 gxt_usage
>&2 echo "ERROR: unknown option '$ARG'"
exit 1
;;
esac
done
echo "basedir = $BASEDIR"
echo "docker_image = $DOCKER_IMAGE"
echo "action = $ACTION"
## Detect Marp SCSS
if [ -f "$BASEDIR/.marp/theme.scss" ]; then
eval DOCKER_OPT_MARP_THEME="-v \"$BASEDIR/.marp:/app/.marp\""
echo "Theme: detected Marp files. Adding option to command line ($DOCKER_OPT_MARP_THEME)"
else
echo "Theme: no theme detected. Using default files"
fi
## Detect Mkdocs configuration - old format (full)
if [ -f "$BASEDIR/mkdocs.yml" ]; then
>&2 echo "Mkdocs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml"
exit 1
fi
## Detect Mkdocs configuration - new format (patch)
if [ -f "$BASEDIR/mkdocs-patch.yml" ]; then
eval DOCKER_OPT_MKDOCS_CONFIG="-v \"$BASEDIR/mkdocs-patch.yml:/app/mkdocs-patch.yml\""
echo "Mkdocs: detected mkdocs-patch.yml file. Adding option to command line ($DOCKER_OPT_MKDOCS_CONFIG)"
else
echo "Mkdocs: no mkdocs-patch.yml detected. Using default files"
fi
## Detect slides
if [ -d "$BASEDIR/slides" ]; then
DOCKER_OPT_MARP_PORT="-p 5200:5200"
fi
## Detect docs
if [ -d "$BASEDIR/docs" ]; then
DOCKER_OPT_MKDOCS_PORT="-p 5100:5100"
fi
if [ "$VERBOSITY" -gt 0 ]; then
set -x
fi
DOCKER_NAME="docmachine-$BASEHASH"
DOCKER_CID="$(docker ps -f "name=$DOCKER_NAME" -q)"
if [ -n "$DOCKER_CID" ]; then
docker kill "$DOCKER_NAME"
fi
docker run -i $DOCKER_OPT_TTY \
--name "$DOCKER_NAME" \
--rm \
--shm-size=1gb \
-e "EXT_UID=$(id -u)" \
-e "EXT_GID=$(id -g)" \
-v "$BASEDIR/docs:/app/docs" \
-v "$BASEDIR/slides:/app/slides" \
-v "$BASEDIR/images:/app/images" \
-v "$BASEDIR/_build:/app/_build" \
$DOCKER_OPT_MKDOCS_CONFIG \
$DOCKER_OPT_MARP_THEME \
$DOCKER_OPT_MKDOCS_PORT \
$DOCKER_OPT_MARP_PORT \
"$DOCKER_IMAGE" "$ACTION"
if [ "$VERBOSITY" -gt 0 ]; then
set +x
fi