Files
camera-gallery/install
2025-10-29 21:17:31 +00:00

222 lines
5.8 KiB
Bash

#!/bin/bash
set -e
# --- Setup ---
BASEDIR="$(cd "$(dirname "$0")" && pwd)"
SRCDIR="$BASEDIR/.src"
SCRIPTDIR="$SRCDIR/scripts"
BACKUPDIR="/tmp/shutterlinksort-backup"
# --- Functions ---
usage() {
cat <<EOF
Usage: sudo $0 [OPTIONS] [WEBROOT]
Options:
--help Show this help message
--dry-run Preview actions without changing any files
--rollback Restore the last backup
--uninstall Remove installed files and web content
Arguments:
WEBROOT Required: destination for web content
Examples:
Install normally (default web root):
sudo $0
Install to custom web root:
sudo $0 /srv/http/shutterlinksort
Dry-run install (preview actions):
./install.sh --dry-run /srv/http/shutterlinksort
Rollback from last backup:
sudo $0 --rollback
Uninstall installed files:
sudo $0 --uninstall
EOF
exit 0
}
backup_if_exists() {
local target="$1"
if [ -e "$target" ]; then
local target_dir
target_dir="$(dirname "$target")"
if [ "$DRYRUN" = true ]; then
echo " [DRY-RUN] Would back up existing $(basename "$target") to $BACKUPDIR/$target_dir"
return
fi
mkdir -p "$BACKUPDIR/$target_dir"
cp -a "$target" "$BACKUPDIR/$target_dir/"
echo " • Backed up existing $(basename "$target")"
fi
}
rollback() {
if [ ! -d "$BACKUPDIR" ]; then
echo "No backup found at $BACKUPDIR. Nothing to restore."
exit 1
fi
read -pr "Are you sure you want to rollback all files from backup? [y/N]: " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo "=== Rolling back from backup ==="
if [ "$DRYRUN" = true ]; then
echo " [DRY-RUN] Would restore backup from $BACKUPDIR to /"
else
cp -a "$BACKUPDIR/"* /
echo "Rollback complete ✅"
fi
else
echo "Rollback cancelled."
fi
exit 0
}
uninstall() {
read -pr "Are you sure you want to uninstall all shutterlinksort files and web content? [y/N]: " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo "=== Uninstalling shutterlinksort ==="
for srcfile in "$SCRIPTDIR"/shutterlinksort-*; do
filename="$(basename "$srcfile")"
progname="${filename%%-*}"
pathpart="${filename#*-}"
targetpath="${pathpart//-//}"
target="/$targetpath/$progname"
if [ "$DRYRUN" = true ] && [ -e "$target" ]; then
echo " [DRY-RUN] Would remove $target"
continue
fi
if [ -e "$target" ]; then
rm -f "$target"
echo " • Removed $target"
fi
done
if [ -d "$WEBROOT" ]; then
if [ "$DRYRUN" = true ]; then
echo " [DRY-RUN] Would remove web content at $WEBROOT"
else
rm -rf "$WEBROOT"
echo " • Removed web content at $WEBROOT"
fi
fi
echo "Uninstall complete ✅"
else
echo "Uninstall cancelled."
fi
exit 0
}
# --- Parse arguments ---
DRYRUN=false
ACTION="install"
# Show usage if no arguments were provided
if [ $# -eq 0 ]; then
usage
fi
while [ $# -gt 0 ]; do
case "$1" in
--help)
usage
;;
--dry-run)
DRYRUN=true
shift
;;
--rollback|--uninstall)
ACTION="$1"
shift
;;
*)
WEBROOT="$1"
shift
;;
esac
done
# --- Main ---
case "$ACTION" in
--rollback)
rollback
;;
--uninstall)
uninstall
;;
install)
echo "=== Installing shutterlinksort components ==="
echo "Using web root: $WEBROOT"
echo "Backup directory: $BACKUPDIR"
if [ "$DRYRUN" = true ]; then
echo "[DRY-RUN mode: no files will be changed]"
fi
echo
# Clear previous backup
if [ "$DRYRUN" = true ]; then
echo "[DRY-RUN] Would remove existing $BACKUPDIR"
else
rm -rf "$BACKUPDIR"
fi
if [ "$DRYRUN" = true ]; then
echo "[DRY-RUN] Would create $BACKUPDIR"
else
mkdir -p "$BACKUPDIR"
fi
# Install scripts
for srcfile in "$SCRIPTDIR"/shutterlinksort-*; do
[ -e "$srcfile" ] || continue
filename="$(basename "$srcfile")"
progname="${filename%%-*}"
pathpart="${filename#*-}"
targetpath="${pathpart//-//}"
target="/$targetpath/$progname"
echo "→ Installing $progname$target"
mkdir -p "/$targetpath" 2>/dev/null || true
backup_if_exists "$target"
if [ "$DRYRUN" = true ]; then
echo " [DRY-RUN] Would install $srcfile$target"
else
install -m 755 "$srcfile" "$target"
fi
done
# Install web content
echo
echo "=== Installing web content ==="
if [ "$DRYRUN" = true ]; then
echo " [DRY-RUN] Would create $WEBROOT"
else
mkdir -p "$WEBROOT"
fi
if [ -d "$WEBROOT" ]; then
if [ "$DRYRUN" = true ]; then
echo " [DRY-RUN] Would back up existing web content to $BACKUPDIR/$(basename "$WEBROOT")"
else
cp -a "$WEBROOT" "$BACKUPDIR/"
fi
fi
if [ "$DRYRUN" = true ]; then
echo " [DRY-RUN] Would copy $SRCDIR/web/ → $WEBROOT"
else
cp -r "$SRCDIR/web/"* "$WEBROOT/"
fi
echo
echo "Web content installed to: $WEBROOT"
echo "All backups stored in: $BACKUPDIR"
echo "Installation complete ✅"
echo "To rollback, run: sudo $0 --rollback"
echo "To uninstall, run: sudo $0 --uninstall"
;;
esac