#!/bin/bash

# /mnt/user/scripts/tailscale.sh

# add the following script using the Unraid "User Scripts" plugin
# requires "screen" to be installed (via the Nerd Pack plugin)

set -x

# check latest version against what's installed
VER=$(curl -sL https://api.github.com/repos/tailscale/tailscale/releases/latest |  jq -r ".tag_name" | cut -c 2-)
if [ "$VER" = "$(tailscale version | head -n1)" ]; then
    echo "$VER already installed, exiting..."
    exit 0
fi

# download latest version, restart daemon
curl -fsSL -o /boot/config/tailscale/tailscale_static.tgz  "https://pkgs.tailscale.com/stable/tailscale_${VER}_amd64.tgz"
if [ $? -eq 0 ]; then
    screen -X -S tailscaled quit
    /usr/sbin/tailscaled -cleanup
    bash /boot/config/tailscale/install.sh
    bash /boot/config/tailscale/start.sh
fi