#!/bin/bash # Utilities module for Transmission RSS Manager Installation # Function to log a message with timestamp function log() { local level=$1 local message=$2 local timestamp=$(date '+%Y-%m-%d %H:%M:%S') case $level in "INFO") echo -e "${timestamp} ${GREEN}[INFO]${NC} $message" ;; "WARN") echo -e "${timestamp} ${YELLOW}[WARN]${NC} $message" ;; "ERROR") echo -e "${timestamp} ${RED}[ERROR]${NC} $message" ;; *) echo -e "${timestamp} [LOG] $message" ;; esac } # Function to check if a command exists function command_exists() { command -v "$1" &> /dev/null } # Function to backup a file before modifying it function backup_file() { local file=$1 if [ -f "$file" ]; then local backup="${file}.bak.$(date +%Y%m%d%H%M%S)" cp "$file" "$backup" log "INFO" "Created backup of $file at $backup" fi } # Function to create a directory if it doesn't exist function create_dir_if_not_exists() { local dir=$1 local owner=$2 if [ ! -d "$dir" ]; then mkdir -p "$dir" log "INFO" "Created directory: $dir" if [ -n "$owner" ]; then chown -R "$owner" "$dir" log "INFO" "Set ownership of $dir to $owner" fi fi } # Function to finalize the setup (permissions, etc.) function finalize_setup() { log "INFO" "Setting up final permissions and configurations..." # Set proper ownership for the installation directory chown -R $USER:$USER $INSTALL_DIR # Create media directories with correct permissions create_dir_if_not_exists "$MEDIA_DIR/movies" "$USER:$USER" create_dir_if_not_exists "$MEDIA_DIR/tvshows" "$USER:$USER" create_dir_if_not_exists "$MEDIA_DIR/music" "$USER:$USER" create_dir_if_not_exists "$MEDIA_DIR/software" "$USER:$USER" # Create book/magazine directories if enabled if [ "$ENABLE_BOOK_SORTING" = true ]; then create_dir_if_not_exists "$MEDIA_DIR/books" "$USER:$USER" create_dir_if_not_exists "$MEDIA_DIR/magazines" "$USER:$USER" fi # Install NPM packages log "INFO" "Installing NPM packages..." cd $INSTALL_DIR && npm install # Start the service log "INFO" "Starting the service..." systemctl daemon-reload systemctl enable $SERVICE_NAME systemctl start $SERVICE_NAME # Check if service started successfully sleep 2 if systemctl is-active --quiet $SERVICE_NAME; then log "INFO" "Service started successfully!" else log "ERROR" "Service failed to start. Check logs with: journalctl -u $SERVICE_NAME" fi # Create default configuration if it doesn't exist if [ ! -f "$INSTALL_DIR/config.json" ]; then log "INFO" "Creating default configuration file..." cat > $INSTALL_DIR/config.json << EOF { "transmissionConfig": { "host": "${TRANSMISSION_HOST}", "port": ${TRANSMISSION_PORT}, "username": "${TRANSMISSION_USER}", "password": "${TRANSMISSION_PASS}", "path": "${TRANSMISSION_RPC_PATH}" }, "remoteConfig": { "isRemote": ${TRANSMISSION_REMOTE}, "directoryMapping": ${TRANSMISSION_DIR_MAPPING} }, "destinationPaths": { "movies": "${MEDIA_DIR}/movies", "tvShows": "${MEDIA_DIR}/tvshows", "music": "${MEDIA_DIR}/music", "books": "${MEDIA_DIR}/books", "magazines": "${MEDIA_DIR}/magazines", "software": "${MEDIA_DIR}/software" }, "seedingRequirements": { "minRatio": 1.0, "minTimeMinutes": 60, "checkIntervalSeconds": 300 }, "processingOptions": { "enableBookSorting": ${ENABLE_BOOK_SORTING}, "extractArchives": true, "deleteArchives": true, "createCategoryFolders": true, "ignoreSample": true, "ignoreExtras": true, "renameFiles": true, "autoReplaceUpgrades": true, "removeDuplicates": true, "keepOnlyBestVersion": true }, "rssFeeds": [], "rssUpdateIntervalMinutes": 60, "autoProcessing": false } EOF chown $USER:$USER $INSTALL_DIR/config.json fi log "INFO" "Setup finalized!" }