Files

64 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BIN_DIR="${BIN_DIR:-$HOME/.local/bin}"
LOG_FILE="${LOG_FILE:-${XDG_STATE_HOME:-$HOME/.local/state}/skills-autoupdate.log}"
SCHEDULE="${SCHEDULE:-17 * * * *}"
SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TARGET="$BIN_DIR/skills-autoupdate"
MARKER="# skills-autoupdate"
usage() {
cat <<EOF
Usage: install.sh [--no-cron] [--uninstall]
--no-cron install the script and config files without a crontab entry
--uninstall remove the script and the crontab entry (config files are kept)
Environment:
BIN_DIR install directory (default ~/.local/bin)
SCHEDULE cron schedule (default "17 * * * *")
LOG_FILE cron log file (default ~/.local/state/skills-autoupdate.log)
EOF
}
current_crontab() {
crontab -l 2>/dev/null | grep -vF "$MARKER" || true
}
cron=1
case "${1:-}" in
--no-cron) cron=0 ;;
--uninstall)
rm -f "$TARGET"
if command -v crontab >/dev/null; then
kept="$(current_crontab)"
printf '%s\n' "$kept" | sed '/^$/d' | crontab -
fi
echo "removed $TARGET and the crontab entry"
exit 0
;;
-h | --help) usage; exit 0 ;;
"") ;;
*) usage >&2; exit 2 ;;
esac
install -Dm755 "$SRC_DIR/skills-autoupdate" "$TARGET"
echo "installed $TARGET"
mkdir -p "$HOME/.config"
for f in skills harnesses; do
if [ ! -e "$HOME/.config/$f" ]; then
install -m644 "$SRC_DIR/examples/$f" "$HOME/.config/$f"
echo "created ~/.config/$f"
fi
done
if [ "$cron" -eq 1 ]; then
command -v crontab >/dev/null || { echo "crontab not found, install cron or use --no-cron" >&2; exit 1; }
mkdir -p "$(dirname "$LOG_FILE")"
kept="$(current_crontab)"
printf '%s\n%s\n' "$kept" "$SCHEDULE \"$TARGET\" >>\"$LOG_FILE\" 2>&1 $MARKER" | sed '/^$/d' | crontab -
echo "crontab entry set: $SCHEDULE, log in $LOG_FILE"
fi