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