First step into separated scripts

This commit is contained in:
2026-01-03 10:47:27 +01:00
parent ced8ac8dc0
commit 3bdd26ed81
7 changed files with 228 additions and 224 deletions
+77 -224
View File
@@ -2,274 +2,127 @@
set -e
### 🔧 Default Configuration (can be overridden by .env file)
SERVER_USER="${SERVER_USER:-$USER}" # Default: current user
SERVER_SSH_PORT="${SERVER_SSH_PORT:-22}" # Default: standard SSH port
SERVER_IP="${SERVER_IP:-192.168.1.100}" # Default: common local network
### Default Configuration (can be overridden by .env file)
SERVER_USER="${SERVER_USER:-$USER}" # Default: current user
SERVER_SSH_PORT="${SERVER_SSH_PORT:-22}" # Default: standard SSH port
SERVER_IP="${SERVER_IP:-192.168.1.100}" # Default: common local network
SERVER_DB_DIR="${SERVER_DB_DIR:-/volume1/Darktable/darktable_db}"
SERVER_PHOTO_DIR="${SERVER_PHOTO_DIR:-/volume1/Darktable/photo_library}"
LOCAL_PHOTO_DIR="${PHOTO_DIR:-$HOME/Pictures/raw}"
LOCAL_DARKTABLE_DB_DIR="${DARKTABLE_DB_DIR:-$HOME/.config/darktable}"
BIN_DIR="${BIN_DIR:-$HOME/.local/bin}"
APPLICATIONS_DIR="$HOME/.local/share/applications"
SYNC_SCRIPT="$BIN_DIR/darktable_sync.sh"
WRAPPER_SCRIPT="$BIN_DIR/darktable_wrapper.sh"
DESKTOP_SHORTCUT="$APPLICATIONS_DIR/darktable-with-sync.desktop"
SYNC_ONLY_SHORTCUT="$APPLICATIONS_DIR/darktable-sync-only.desktop"
### Prepare folders
### 📁 Prepare folders
mkdir -p "$BIN_DIR"
mkdir -p "$HOME/.config/systemd/user"
mkdir -p "$APPLICATIONS_DIR"
### Load .env if present (overrides defaults)
### 🔄 Load .env if present (overrides defaults)
ENV_FILE=".env"
if [[ -f "$ENV_FILE" ]]; then
echo "📥 Loading configuration from .env file..."
set -a # automatically export all variables
source "$ENV_FILE"
set +a
echo "Loading configuration from .env file..."
set -a
# shellcheck source=/dev/null
. "$ENV_FILE"
set +a
fi
### 📝 Show effective configuration
echo "Using configuration:"
echo "SERVER_USER: $SERVER_USER"
echo "SERVER_IP: $SERVER_IP"
echo "SERVER_SSH_PORT: $SERVER_SSH_PORT"
echo "SERVER_DB_DIR: $SERVER_DB_DIR"
echo "SERVER_PHOTO_DIR: $SERVER_PHOTO_DIR"
echo "PHOTO_DIR: $LOCAL_PHOTO_DIR"
echo "DARKTABLE_DB_DIR:$LOCAL_DARKTABLE_DB_DIR"
echo "BIN_DIR: $BIN_DIR"
### Show effective configuration
### 🔍 Check dependencies
echo "🔍 Checking requirements..."
echo "Using configuration:"
echo "SERVER_USER: $SERVER_USER"
echo "SERVER_IP: $SERVER_IP"
echo "SERVER_SSH_PORT: $SERVER_SSH_PORT"
echo "SERVER_DB_DIR: $SERVER_DB_DIR"
echo "SERVER_PHOTO_DIR: $SERVER_PHOTO_DIR"
echo "PHOTO_DIR: $LOCAL_PHOTO_DIR"
echo "DARKTABLE_DB_DIR: $LOCAL_DARKTABLE_DB_DIR"
echo "BIN_DIR: $BIN_DIR"
### Check dependencies
echo "Checking requirements..."
REQUIRED_CMDS=("rsync" "notify-send" "ping" "darktable" "systemctl" "xdg-user-dir")
for cmd in "${REQUIRED_CMDS[@]}"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: '$cmd' is not installed."
echo "👉 You can install it with: sudo apt install $cmd"
exit 1
fi
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: '$cmd' is not installed."
echo "Install it with: sudo apt install $cmd"
exit 1
fi
done
# Check folder presence
if [ ! -d "$LOCAL_PHOTO_DIR" ]; then
echo "Local photo folder does not exist: $LOCAL_PHOTO_DIR"
echo "👉 Please create it using: mkdir -p \"$LOCAL_PHOTO_DIR\""
exit 1
fi
if [ ! -d "$LOCAL_DARKTABLE_DB_DIR" ]; then
echo "❌ Darktable database path does not exist: $LOCAL_DARKTABLE_DB_DIR"
echo "👉 Start Darktable once or create the directory manually."
exit 1
fi
# Check if Server is reachable
if ping -c 1 "$SERVER_IP" &>/dev/null; then
echo "✅ Server is reachable: $SERVER_IP"
if ! ssh -p "$SERVER_SSH_PORT" "$SERVER_USER@$SERVER_IP" "[ -d '$SERVER_DB_DIR' ]"; then
echo "❌ Remote directory missing on Server: $SERVER_DB_DIR"
echo "👉 Please create it or adjust the path."
exit 1
fi
if ! ssh -p "$SERVER_SSH_PORT" "$SERVER_USER@$SERVER_IP" "[ -d '$SERVER_PHOTO_DIR' ]"; then
echo "❌ Remote directory missing on Server: $SERVER_PHOTO_DIR"
echo "👉 Please create it or adjust the path."
exit 1
fi
else
echo "⚠️ Server not reachable: $SERVER_IP"
echo "➡️ Sync will fail until Server is online."
fi
### ✅ Create sync script
cat > "$SYNC_SCRIPT" <<EOF
#!/bin/bash
log() {
echo "\$1"
}
count_synced_files() {
LOG="\$1"
DIRECTION="\$2"
case "\$DIRECTION" in
"up")
COUNT=\$(grep -E '^(<f|cd)' "\$LOG" | wc -l)
;;
"down")
COUNT=\$(grep -E '^(>f|cd)' "\$LOG" | wc -l)
;;
*)
COUNT=0
;;
esac
echo "\$COUNT"
}
SCRIPT_NAME=\$(basename \$0)
LOCKFILE="/tmp/\${SCRIPT_NAME}.lock"
if [ -e "\$LOCKFILE" ]; then
echo "Scrip is already running or delete \$LOCKFILE"
echo "Local photo folder does not exist: $LOCAL_PHOTO_DIR"
echo "Create it using: mkdir -p \"$LOCAL_PHOTO_DIR\""
exit 1
fi
touch "\$LOCKFILE"
trap "rm -f '\$LOCKFILE'" EXIT
SHOW_NOTIFY_START_STOP=false
if [[ "\$1" == "--with-notify-start-stop" ]]; then
SHOW_NOTIFY_START_STOP=true
if [ ! -d "$LOCAL_DARKTABLE_DB_DIR" ]; then
echo "Darktable database path does not exist: $LOCAL_DARKTABLE_DB_DIR"
echo "Start Darktable once or create the directory manually."
exit 1
fi
if ping -c 1 $SERVER_IP &>/dev/null; then
export DISPLAY=:0
SYNC_LOG=\$(mktemp)
log "🔃 Server is reachable starting sync..."
log "Log file: \$SYNC_LOG"
# Check if server is reachable and remote dirs exist
if [ "\$SHOW_NOTIFY_START_STOP" = true ]; then
notify-send "Darktable Sync" "🔄 Sync started..." -t 3000
fi
if ping -c 1 "$SERVER_IP" &>/dev/null; then
echo "Server is reachable: $SERVER_IP"
log "⬆️ Uploading Darktable DB to Server..."
UPLOAD_LOG1=\$(mktemp)
rsync -uavh --itemize-changes -e "ssh -p $SERVER_SSH_PORT" "$LOCAL_DARKTABLE_DB_DIR/" "$SERVER_USER@$SERVER_IP:$SERVER_DB_DIR/" 2>&1 | tee -a "\$SYNC_LOG" "\$UPLOAD_LOG1"
SENT1=\$(count_synced_files "\$UPLOAD_LOG1" "up")
rm "\$UPLOAD_LOG1"
if ! ssh -p "$SERVER_SSH_PORT" "$SERVER_USER@$SERVER_IP" "[ -d '$SERVER_DB_DIR' ]"; then
echo "Remote directory missing on server: $SERVER_DB_DIR"
echo "Create it or adjust the path."
exit 1
fi
log "⬆️ Uploading photos to Server..."
UPLOAD_LOG2=\$(mktemp)
rsync -uavh --itemize-changes -e "ssh -p $SERVER_SSH_PORT" "$LOCAL_PHOTO_DIR/" "$SERVER_USER@$SERVER_IP:$SERVER_PHOTO_DIR/" 2>&1 | tee -a "\$SYNC_LOG" "\$UPLOAD_LOG2"
SENT2=\$(count_synced_files "\$UPLOAD_LOG2" "up")
rm "\$UPLOAD_LOG2"
log "⬇️ Downloading DB back from Server..."
DOWNLOAD_LOG1=\$(mktemp)
rsync -uavh --itemize-changes -e "ssh -p $SERVER_SSH_PORT" "$SERVER_USER@$SERVER_IP:$SERVER_DB_DIR/" "$LOCAL_DARKTABLE_DB_DIR/" 2>&1 | tee -a "\$SYNC_LOG" "\$DOWNLOAD_LOG1"
RECEIVED1=\$(count_synced_files "\$DOWNLOAD_LOG1" "down")
rm "\$DOWNLOAD_LOG1"
log "⬇️ Downloading photos from Server..."
DOWNLOAD_LOG2=\$(mktemp)
rsync -uavh --itemize-changes -e "ssh -p $SERVER_SSH_PORT" "$SERVER_USER@$SERVER_IP:$SERVER_PHOTO_DIR/" "$LOCAL_PHOTO_DIR/" 2>&1 | tee -a "\$SYNC_LOG" "\$DOWNLOAD_LOG2"
RECEIVED2=\$(count_synced_files "\$DOWNLOAD_LOG2" "down")
rm "\$DOWNLOAD_LOG2"
if [ "\$SHOW_NOTIFY_START_STOP" = true ]; then
notify-send "Darktable Sync" "✅ Sync finished." -t 3000
fi
TOTAL_SENT=\$((SENT1 + SENT2))
TOTAL_RECEIVED=\$((RECEIVED1 + RECEIVED2))
if [ "\$TOTAL_SENT" -gt 0 ] || [ "\$TOTAL_RECEIVED" -gt 0 ]; then
log "✅ Uploaded: \$TOTAL_SENT files"
log "✅ Downloaded: \$TOTAL_RECEIVED files"
notify-send "Darktable Sync" "⬆️ \$TOTAL_SENT uploaded | ⬇️ \$TOTAL_RECEIVED downloaded" -t 10000
else
log "️ No changes detected."
fi
rm -f "\$SYNC_LOG"
if ! ssh -p "$SERVER_SSH_PORT" "$SERVER_USER@$SERVER_IP" "[ -d '$SERVER_PHOTO_DIR' ]"; then
echo "Remote directory missing on server: $SERVER_PHOTO_DIR"
echo "Create it or adjust the path."
exit 1
fi
else
log "❌ Server not reachable skipping sync."
echo "Server not reachable: $SERVER_IP"
echo "Sync will fail until server is online."
fi
EOF
chmod +x "$SYNC_SCRIPT"
### Install sync and wrapper scripts
### ✅ Create wrapper script with sync + notify
cat > "$WRAPPER_SCRIPT" <<EOF
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
log() {
echo "\$1"
}
cp "$SCRIPT_DIR/scripts/darktable_sync.sh" "$SYNC_SCRIPT"
cp "$SCRIPT_DIR/scripts/darktable_wrapper.sh" "$WRAPPER_SCRIPT"
chmod +x "$SYNC_SCRIPT" "$WRAPPER_SCRIPT"
log "🔄 Sync starting before Darktable..."
"$SYNC_SCRIPT" --with-notify-start-stop
### Install systemd user service and timer
log "🚀 Launching Darktable..."
darktable
log "🛑 Darktable closed."
cp "$SCRIPT_DIR/systemd/darktable-sync.service" "$HOME/.config/systemd/user/darktable-sync.service"
cp "$SCRIPT_DIR/systemd/darktable-sync.timer" "$HOME/.config/systemd/user/darktable-sync.timer"
log "🔄 Sync starting after Darktable..."
"$SYNC_SCRIPT" --with-notify-start-stop
log "Darktable Sync" "✅ Final sync completed." -t 3000
EOF
systemctl --user daemon-reload
systemctl --user enable darktable-sync.timer
systemctl --user start darktable-sync.timer
chmod +x "$WRAPPER_SCRIPT"
### Install desktop shortcuts
cp "$SCRIPT_DIR/desktop/darktable-with-sync.desktop" "$DESKTOP_SHORTCUT"
cp "$SCRIPT_DIR/desktop/darktable-sync-only.desktop" "$SYNC_ONLY_SHORTCUT"
### ✅ Create systemd service and timer
# cat > "$HOME/.config/systemd/user/darktable-sync.service" <<EOF
# [Unit]
# Description=Darktable Sync
update-desktop-database "$APPLICATIONS_DIR" 2>/dev/null || true
# [Service]
# Type=oneshot
# ExecStart=$SYNC_SCRIPT
# EOF
# cat > "$HOME/.config/systemd/user/darktable-sync.timer" <<EOF
# [Unit]
# Description=Run Darktable sync every 5 minutes
# [Timer]
# OnBootSec=2min
# OnUnitActiveSec=5min
# Unit=darktable-sync.service
# [Install]
# WantedBy=default.target
# EOF
### ✅ Create desktop shortcut
cat > "$DESKTOP_SHORTCUT" <<EOF
[Desktop Entry]
Name=Darktable with Sync
Exec=$WRAPPER_SCRIPT
Icon=darktable
Type=Application
Terminal=false
Categories=Graphics;
StartupNotify=true
Keywords=Darktable;Photo;Sync;Server;Rsync;
EOF
chmod +x "$DESKTOP_SHORTCUT"
### ✅ Create additional desktop shortcut (sync only)
cat > "$SYNC_ONLY_SHORTCUT" <<EOF
[Desktop Entry]
Name=Darktable Sync Only
Exec=$SYNC_SCRIPT --with-notify-start-stop
Icon=darktable
Type=Application
Terminal=false
Categories=Utility;
StartupNotify=true
Keywords=Darktable;Photo;Sync;Server;Schedule;
EOF
chmod +x "$SYNC_ONLY_SHORTCUT"
### ▶️ Enable systemd timer
# systemctl --user daemon-reexec
# systemctl --user enable --now darktable-sync.timer
### ✅ Summary
echo ""
echo "✅ Setup complete."
echo ""
echo "🖱 You can start Darktable via 'Darktable with Sync' in your application menu."
# echo "🕒 Sync will run every 5 minutes automatically when the Server is online."
echo "Installation finished."