|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Extend the PATH to include the go binary directory |
| 4 | +export PATH=$PATH:/usr/local/go/bin |
| 5 | + |
| 6 | +# Function to display error messages and exit with status 1 |
| 7 | +handle_error() { |
| 8 | + echo "Error: $1" >&2 |
| 9 | + exit 1 |
| 10 | +} |
| 11 | + |
| 12 | +# Function to display usage information |
| 13 | +display_help() { |
| 14 | + echo "Usage: $0 [options]" |
| 15 | + echo "Options:" |
| 16 | + echo " -h, --help Display this help message" |
| 17 | + echo " --repo-url URL Set the repository URL (default: https://github.com/Notifiarr/notifiarr.git)" |
| 18 | + echo " --repo-dir DIR Set the repository directory (default: /opt/notifiarr-repo)" |
| 19 | + echo " --bin-path PATH Set the binary path (default: /usr/bin/notifiarr)" |
| 20 | + echo " --branch BRANCH Set the branch (default: master)" |
| 21 | + echo " --reinstall-apt Reinstall Notifiarr using apt without prompting." |
| 22 | + exit 0 |
| 23 | +} |
| 24 | + |
| 25 | +# Function to check and prompt for installation of a required tool |
| 26 | +ensure_tool_installed() { |
| 27 | + local tool=$1 |
| 28 | + local install_cmd=$2 |
| 29 | + if ! command -v "$tool" &>/dev/null; then |
| 30 | + read -p "$tool is not installed. Do you want to install it? [Y/n] " response |
| 31 | + if [[ "$response" =~ ^[Yy] ]]; then |
| 32 | + eval "$install_cmd" || handle_error "Failed to install $tool." |
| 33 | + else |
| 34 | + echo "$tool is required for this script. Exiting." |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + fi |
| 38 | +} |
| 39 | + |
| 40 | +# Default parameters |
| 41 | +repo_url="https://github.com/Notifiarr/notifiarr.git" |
| 42 | +repo_dir="/opt/notifiarr-repo" |
| 43 | +bin_path="/usr/bin/notifiarr" |
| 44 | +branch="master" |
| 45 | +apt_reinstall=false |
| 46 | + |
| 47 | +# Parse command line options |
| 48 | +while [[ $# -gt 0 ]]; do |
| 49 | + case "$1" in |
| 50 | + -h | --help) |
| 51 | + display_help |
| 52 | + ;; |
| 53 | + --repo-url) |
| 54 | + repo_url="$2" |
| 55 | + shift |
| 56 | + ;; |
| 57 | + --repo-dir) |
| 58 | + repo_dir="$2" |
| 59 | + shift |
| 60 | + ;; |
| 61 | + --bin-path) |
| 62 | + bin_path="$2" |
| 63 | + shift |
| 64 | + ;; |
| 65 | + --branch) |
| 66 | + branch="$2" |
| 67 | + shift |
| 68 | + ;; |
| 69 | + --reinstall-apt) |
| 70 | + apt_reinstall=true |
| 71 | + ;; |
| 72 | + *) |
| 73 | + echo "Invalid option: $1. Use -h for help." |
| 74 | + exit 1 |
| 75 | + ;; |
| 76 | + esac |
| 77 | + shift |
| 78 | +done |
| 79 | + |
| 80 | +# Ensure required tools are installed |
| 81 | +ensure_tool_installed "make" "sudo apt update && sudo apt install -y make" |
| 82 | + |
| 83 | +# Reinstallation condition handling |
| 84 | +reinstall_notifiarr() { |
| 85 | + sudo apt update && sudo apt install --reinstall notifiarr || handle_error "Failed to reinstall Notifiarr using apt." |
| 86 | +} |
| 87 | + |
| 88 | +[[ $apt_reinstall == true ]] && reinstall_notifiarr |
| 89 | + |
| 90 | +# Repository management |
| 91 | +if [[ ! -d "$repo_dir" ]]; then |
| 92 | + git clone "$repo_url" "$repo_dir" || handle_error "Failed to clone repository." |
| 93 | +else |
| 94 | + git -C "$repo_dir" fetch --all --prune || handle_error "Failed to fetch updates from remote." |
| 95 | +fi |
| 96 | + |
| 97 | +# Branch handling and updating |
| 98 | +current_branch=$(git -C "$repo_dir" rev-parse --abbrev-ref HEAD) |
| 99 | +read -p "Do you want to use the current branch ($current_branch)? [Y/n] " choice |
| 100 | +if [[ "$choice" =~ ^[Nn] ]]; then |
| 101 | + branches=$(git -C "$repo_dir" branch -r | sed 's/origin\///;s/* //') |
| 102 | + echo "Available branches:" |
| 103 | + echo "$branches" |
| 104 | + while true; do |
| 105 | + read -p "Enter the branch name you want to use: " branch |
| 106 | + if [[ $branches =~ $branch ]]; then |
| 107 | + git -C "$repo_dir" checkout "$branch" || handle_error "Failed to checkout branch $branch." |
| 108 | + break |
| 109 | + else |
| 110 | + echo "Invalid choice. Please select a valid branch." |
| 111 | + fi |
| 112 | + done |
| 113 | +fi |
| 114 | + |
| 115 | +git -C "$repo_dir" pull || handle_error "Failed to pull latest changes." |
| 116 | +make --directory="$repo_dir" || handle_error "Failed to compile." |
| 117 | + |
| 118 | +# Service management |
| 119 | +echo "Stopping notifiarr..." |
| 120 | +sudo systemctl stop notifiarr |
| 121 | + |
| 122 | +if [[ -f "$bin_path" ]]; then |
| 123 | + sudo mv "$bin_path" "$repo_dir".old && echo "Old binary moved to $repo_dir.old" |
| 124 | +fi |
| 125 | + |
| 126 | +sudo mv "$repo_dir/notifiarr" "$bin_path" && echo "New binary moved to $bin_path" |
| 127 | +sudo chown root:root "$bin_path" |
| 128 | + |
| 129 | +echo "Starting Notifiarr..." |
| 130 | +sudo systemctl start notifiarr |
| 131 | + |
| 132 | +if sudo systemctl is-active --quiet notifiarr; then |
| 133 | + echo "Notifiarr service started and is currently running" |
| 134 | +else |
| 135 | + handle_error "Failed to start Notifiarr service" |
| 136 | +fi |
| 137 | + |
| 138 | +exit 0 |
0 commit comments