transmission-rss-manager/install-script.sh
2025-03-04 22:28:11 +00:00

330 lines
9.9 KiB
Bash
Executable File

#!/bin/bash
# Transmission RSS Manager Installer Script
# Main entry point for the installation
# Text formatting
BOLD='\033[1m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Print header
echo -e "${BOLD}==================================================${NC}"
echo -e "${BOLD} Transmission RSS Manager Installer ${NC}"
echo -e "${BOLD} Version 1.2.0 - Enhanced Edition ${NC}"
echo -e "${BOLD}==================================================${NC}"
echo
# Check if script is run with sudo
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Please run as root (use sudo)${NC}"
exit 1
fi
# Get current directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Create modules directory if it doesn't exist
mkdir -p "${SCRIPT_DIR}/modules"
# Check for installation type
IS_UPDATE=false
if [ -f "${SCRIPT_DIR}/config.json" ]; then
IS_UPDATE=true
echo -e "${YELLOW}Existing installation detected. Running in update mode.${NC}"
echo -e "${GREEN}Your existing configuration will be preserved.${NC}"
else
echo -e "${GREEN}Fresh installation. Will create new configuration.${NC}"
fi
# Check if modules exist, if not, extract them
if [ ! -f "${SCRIPT_DIR}/modules/config-module.sh" ]; then
echo -e "${YELLOW}Creating module files...${NC}"
# Create config module
cat > "${SCRIPT_DIR}/modules/config-module.sh" << 'EOL'
#!/bin/bash
# Configuration module for Transmission RSS Manager Installation
# Configuration variables with defaults
INSTALL_DIR="/opt/transmission-rss-manager"
SERVICE_NAME="transmission-rss-manager"
USER=$(logname || echo $SUDO_USER)
PORT=3000
# Transmission configuration variables
TRANSMISSION_REMOTE=false
TRANSMISSION_HOST="localhost"
TRANSMISSION_PORT=9091
TRANSMISSION_USER=""
TRANSMISSION_PASS=""
TRANSMISSION_RPC_PATH="/transmission/rpc"
TRANSMISSION_DOWNLOAD_DIR="/var/lib/transmission-daemon/downloads"
TRANSMISSION_DIR_MAPPING="{}"
# Media path defaults
MEDIA_DIR="/mnt/media"
ENABLE_BOOK_SORTING=true
function gather_configuration() {
echo -e "${BOLD}Installation Configuration:${NC}"
echo -e "Please provide the following configuration parameters:"
echo
read -p "Installation directory [$INSTALL_DIR]: " input_install_dir
INSTALL_DIR=${input_install_dir:-$INSTALL_DIR}
read -p "Web interface port [$PORT]: " input_port
PORT=${input_port:-$PORT}
read -p "Run as user [$USER]: " input_user
USER=${input_user:-$USER}
echo
echo -e "${BOLD}Transmission Configuration:${NC}"
echo -e "Configure connection to your Transmission client:"
echo
read -p "Is Transmission running on a remote server? (y/n) [n]: " input_remote
if [[ $input_remote =~ ^[Yy]$ ]]; then
TRANSMISSION_REMOTE=true
read -p "Remote Transmission host [localhost]: " input_trans_host
TRANSMISSION_HOST=${input_trans_host:-$TRANSMISSION_HOST}
read -p "Remote Transmission port [9091]: " input_trans_port
TRANSMISSION_PORT=${input_trans_port:-$TRANSMISSION_PORT}
read -p "Remote Transmission username []: " input_trans_user
TRANSMISSION_USER=${input_trans_user:-$TRANSMISSION_USER}
read -p "Remote Transmission password []: " input_trans_pass
TRANSMISSION_PASS=${input_trans_pass:-$TRANSMISSION_PASS}
read -p "Remote Transmission RPC path [/transmission/rpc]: " input_trans_path
TRANSMISSION_RPC_PATH=${input_trans_path:-$TRANSMISSION_RPC_PATH}
# Configure directory mapping for remote setup
echo
echo -e "${YELLOW}Directory Mapping Configuration${NC}"
echo -e "When using a remote Transmission server, you need to map paths between servers."
echo -e "For each directory on the remote server, specify the corresponding local directory."
echo
# Get remote download directory
read -p "Remote Transmission download directory: " REMOTE_DOWNLOAD_DIR
REMOTE_DOWNLOAD_DIR=${REMOTE_DOWNLOAD_DIR:-"/var/lib/transmission-daemon/downloads"}
# Get local directory that corresponds to remote download directory
read -p "Local directory that corresponds to the remote download directory: " LOCAL_DOWNLOAD_DIR
LOCAL_DOWNLOAD_DIR=${LOCAL_DOWNLOAD_DIR:-"/mnt/transmission-downloads"}
# Create mapping JSON
TRANSMISSION_DIR_MAPPING=$(cat <<EOF
{
"$REMOTE_DOWNLOAD_DIR": "$LOCAL_DOWNLOAD_DIR"
}
EOF
)
# Create the local directory
mkdir -p "$LOCAL_DOWNLOAD_DIR"
chown -R $USER:$USER "$LOCAL_DOWNLOAD_DIR"
# Ask if want to add more mappings
while true; do
read -p "Add another directory mapping? (y/n) [n]: " add_another
if [[ ! $add_another =~ ^[Yy]$ ]]; then
break
fi
read -p "Remote directory path: " remote_dir
read -p "Corresponding local directory path: " local_dir
if [ -n "$remote_dir" ] && [ -n "$local_dir" ]; then
# Update mapping JSON (remove the last "}" and add the new mapping)
TRANSMISSION_DIR_MAPPING="${TRANSMISSION_DIR_MAPPING%\}}, \"$remote_dir\": \"$local_dir\" }"
# Create the local directory
mkdir -p "$local_dir"
chown -R $USER:$USER "$local_dir"
echo -e "${GREEN}Mapping added: $remote_dir → $local_dir${NC}"
fi
done
# Set Transmission download dir for configuration
TRANSMISSION_DOWNLOAD_DIR=$REMOTE_DOWNLOAD_DIR
else
read -p "Transmission download directory [/var/lib/transmission-daemon/downloads]: " input_trans_dir
TRANSMISSION_DOWNLOAD_DIR=${input_trans_dir:-$TRANSMISSION_DOWNLOAD_DIR}
fi
echo
echo -e "${BOLD}Media Destination Configuration:${NC}"
read -p "Media destination base directory [/mnt/media]: " input_media_dir
MEDIA_DIR=${input_media_dir:-$MEDIA_DIR}
# Ask about enabling book/magazine sorting
echo
echo -e "${BOLD}Content Type Configuration:${NC}"
read -p "Enable book and magazine sorting? (y/n) [y]: " input_book_sorting
ENABLE_BOOK_SORTING=true
if [[ $input_book_sorting =~ ^[Nn]$ ]]; then
ENABLE_BOOK_SORTING=false
fi
echo
echo -e "${GREEN}Configuration complete!${NC}"
echo
}
EOL
# Create utils module
cat > "${SCRIPT_DIR}/modules/utils-module.sh" << 'EOL'
#!/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!"
}