testsAndMisc/scripts/utils/setup_media_organizer.sh
Copilot faff8ba349 Fix shell script formatting and add PR workflow validation (#3)
* Initial plan

* fix: format shell scripts with shfmt (convert tabs to 2 spaces)

Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>

* feat: enhance shell-check workflow for PR pre-merge validation

- Add pull_request_target trigger to check PRs from forks
- Add explicit failure message with instructions
- Create BRANCH_PROTECTION.md with setup guide
- Ensure workflow runs on all PRs targeting main/master

Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>

* refactor: improve workflow security and remove redundant exit code

- Remove pull_request_target to avoid executing untrusted fork code
- Remove redundant exit 1 from failure step
- Update documentation to reflect changes
- Standard pull_request trigger handles forks securely

Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>
2026-01-07 22:52:20 +01:00

73 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# Setup script to configure media organizer to run on startup
# Creates systemd service for automatic media file organization
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ORGANIZE_SCRIPT="$SCRIPT_DIR/organize_downloads.sh"
SERVICE_NAME="media-organizer"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
# Get the actual user (not root when running with sudo)
if [[ -n ${SUDO_USER:-} ]]; then
USER_NAME="$SUDO_USER"
else
USER_NAME="$(whoami)"
fi
# Function to log messages
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
# Check if organize script exists
if [[ ! -f $ORGANIZE_SCRIPT ]]; then
log "ERROR: organize_downloads.sh not found at $ORGANIZE_SCRIPT"
exit 1
fi
# Check if running as root for systemd service creation
if [[ $EUID -ne 0 ]]; then
log "This script needs to be run as root to create systemd service."
log "Re-executing with sudo..."
exec sudo "$0" "$@"
fi
log "Setting up media organizer startup service..."
# Create systemd service file
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=Media File Organizer
After=graphical-session.target
Wants=graphical-session.target
[Service]
Type=oneshot
User=$USER_NAME
Group=$USER_NAME
ExecStart=$ORGANIZE_SCRIPT
StandardOutput=journal
StandardError=journal
RemainAfterExit=no
[Install]
WantedBy=multi-user.target
EOF
log "Created systemd service file: $SERVICE_FILE"
# Reload systemd and enable the service
systemctl daemon-reload
systemctl enable "$SERVICE_NAME.service"
log "Service enabled successfully!"
log "The media organizer will now run on every system startup."
log ""
log "To manually run the service: sudo systemctl start $SERVICE_NAME"
log "To check service status: sudo systemctl status $SERVICE_NAME"
log "To view service logs: sudo journalctl -u $SERVICE_NAME"
log "To disable the service: sudo systemctl disable $SERVICE_NAME"