2025-03-04 22:28:11 +00:00

178 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
# Update script for Transmission RSS Manager
# Text formatting
BOLD='\033[1m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Get script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
APP_DIR="$(dirname "$SCRIPT_DIR")"
# 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
# Print header
echo -e "${BOLD}==================================================${NC}"
echo -e "${BOLD} Transmission RSS Manager Updater ${NC}"
echo -e "${BOLD} Version 1.2.0 ${NC}"
echo -e "${BOLD}==================================================${NC}"
echo
# Function to check if a service is running
service_is_running() {
systemctl is-active --quiet "$1"
return $?
}
# Backup existing files
backup_app() {
echo -e "${BOLD}Backing up existing installation...${NC}"
TIMESTAMP=$(date +%Y%m%d%H%M%S)
BACKUP_DIR="${APP_DIR}_backup_${TIMESTAMP}"
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Copy files to backup directory
cp -rf "$APP_DIR"/* "$BACKUP_DIR"
echo -e "${GREEN}Backup created at: $BACKUP_DIR${NC}"
}
# Update the application
update_app() {
echo -e "${BOLD}Updating application...${NC}"
# Get user account that owns the files
APP_USER=$(stat -c '%U' "$APP_DIR")
# Check if app is running as a service
WAS_RUNNING=false
if service_is_running transmission-rss-manager; then
WAS_RUNNING=true
echo -e "${YELLOW}Stopping service during update...${NC}"
systemctl stop transmission-rss-manager
fi
# Set environment variable to indicate it's an update
export IS_UPDATE=true
# Backup config files before update
if [ -f "$APP_DIR/config.json" ]; then
echo -e "${YELLOW}Backing up configuration file...${NC}"
CONFIG_BACKUP="${APP_DIR}/config.json.bak.$(date +%Y%m%d%H%M%S)"
cp "$APP_DIR/config.json" "$CONFIG_BACKUP"
echo -e "${GREEN}Configuration backed up to $CONFIG_BACKUP${NC}"
fi
# Update npm dependencies
cd "$APP_DIR"
echo -e "${YELLOW}Updating dependencies...${NC}"
npm install
# Fix permissions
chown -R $APP_USER:$APP_USER "$APP_DIR"
# Check if update script was successful
UPDATE_SUCCESS=true
# Restart service if it was running before
if [ "$WAS_RUNNING" = true ]; then
echo -e "${YELLOW}Restarting service...${NC}"
systemctl daemon-reload
systemctl start transmission-rss-manager
# Check if service started successfully
if service_is_running transmission-rss-manager; then
echo -e "${GREEN}Service restarted successfully.${NC}"
else
echo -e "${RED}Failed to restart service. Check logs with: journalctl -u transmission-rss-manager${NC}"
UPDATE_SUCCESS=false
fi
else
echo -e "${YELLOW}Service was not running before update. Not restarting.${NC}"
fi
# Provide info about configuration changes
if [ -f "$APP_DIR/config.json" ]; then
# Check if the configuration was updated by the service
if [ $(stat -c %Y "$APP_DIR/config.json") -gt $(stat -c %Y "$CONFIG_BACKUP") ]; then
echo -e "${GREEN}Configuration updated successfully with new options.${NC}"
echo -e "${YELLOW}Your existing settings have been preserved.${NC}"
else
echo -e "${YELLOW}Configuration was not modified during update.${NC}"
echo -e "${YELLOW}If you experience issues, check for new configuration options.${NC}"
fi
fi
if [ "$UPDATE_SUCCESS" = true ]; then
echo -e "${GREEN}Update completed successfully.${NC}"
else
echo -e "${RED}Update completed with some issues.${NC}"
echo -e "${YELLOW}If needed, you can restore configuration from: $CONFIG_BACKUP${NC}"
fi
}
# Check for updates in Git repository
check_git_updates() {
echo -e "${BOLD}Checking for updates in Git repository...${NC}"
# Check if git is installed
if ! command -v git &> /dev/null; then
echo -e "${YELLOW}Git is not installed, skipping Git update check.${NC}"
return 1
fi
# Check if app directory is a git repository
if [ ! -d "$APP_DIR/.git" ]; then
echo -e "${YELLOW}Not a Git repository, skipping Git update check.${NC}"
return 1
fi
# Check for updates
cd "$APP_DIR"
git fetch
# Check if we're behind the remote
BEHIND=$(git rev-list HEAD..origin/main --count)
if [ "$BEHIND" -gt 0 ]; then
echo -e "${GREEN}Updates available: $BEHIND new commit(s)${NC}"
# Confirm update
read -p "Do you want to pull the latest changes? (y/n) [y]: " CONFIRM
CONFIRM=${CONFIRM:-y}
if [[ $CONFIRM =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Pulling latest changes...${NC}"
git pull
return 0
else
echo -e "${YELLOW}Skipping Git update.${NC}"
return 1
fi
else
echo -e "${GREEN}Already up to date.${NC}"
return 1
fi
}
# Main update process
backup_app
if check_git_updates || [ "$1" = "--force" ]; then
update_app
else
echo -e "${YELLOW}No updates needed or available.${NC}"
echo -e "${YELLOW}Use --force flag to update dependencies anyway.${NC}"
fi
echo -e "${BOLD}==================================================${NC}"
echo -e "${BOLD} Update process completed ${NC}"
echo -e "${BOLD}==================================================${NC}"