#!/bin/bash echo "Running HarmonyLink Installer" # Check if running with root privileges if [[ $EUID -ne 0 ]]; then echo "This script requires root privileges. Re-running with sudo..." # Re-execute script with sudo and pass the original username as an argument sudo -E bash "$0" "$USER" exit $? fi # Store the original username target_user="$1" # Check if the target_user is empty if [ -z "$target_user" ]; then echo "Failed to determine the target user. Please provide the username as an argument." exit 1 fi # Check if the target_user exists if ! id -u "$target_user" >/dev/null 2>&1; then echo "The specified target user does not exist." exit 1 fi # Define the repository URL repo_url="https://api.github.com/repos/Jordonbc/HarmonyLinkServer/releases/latest" # Fetch the latest release information from GitHub API release_info=$(curl -s "$repo_url") # Extract the download URL for the Linux zip download_url=$(echo "$release_info" | grep -o "https://github.com[^\"']*Linux\.zip") # Check if the download URL is empty if [ -z "$download_url" ]; then echo "Failed to fetch the download URL. Please check the repository." exit 1 fi # Define the file name for the downloaded zip zip_file="HarmonyLinkServer_Linux.zip" # Define the extraction directory using the specified username extract_dir="/home/$target_user/harmonylink" # Check if the HarmonyLink service is already running if systemctl is-active --quiet harmonylink.service; then echo "Stopping existing HarmonyLink service..." systemctl stop harmonylink.service fi # Download the latest Linux zip wget "$download_url" -O "$zip_file" # Check if the download was successful if [ $? -eq 0 ]; then echo "Download complete: $zip_file" else echo "Failed to download the latest Linux zip." exit 1 fi # Extract the zip to the specified directory, overwriting existing files unzip -o -q "$zip_file" -d "$extract_dir" # Check if the extraction was successful if [ $? -eq 0 ]; then echo "Extraction complete: $zip_file" echo "Files extracted to: $extract_dir" else echo "Failed to extract the zip file." exit 1 fi # Clean up the downloaded zip file rm "$zip_file" chmod +x "$extract_dir/harmony_link_server" # Create the service unit file service_file="/etc/systemd/system/harmonylink.service" cat > "$service_file" <