#!/bin/bash
# Info: Script to install Radicle
# Author: Provisioning System
# Release: 1.0
# Date: 2025-07-24

USAGE="install-radicle.sh"
[ "$1" == "-h" ] && echo "$USAGE" && exit 1

[ -r "env-radicle" ] && . ./env-radicle

RADICLE_VERSION=${RADICLE_VERSION:-1.0.0}

RADICLE_URL=https://github.com/radicle-dev/radicle-node/releases/download
ARCH="$(uname -m | sed -e 's/x86_64/x86_64/' -e 's/aarch64$/aarch64/')"
RADICLE_FILE=v${RADICLE_VERSION}/radicle-node-v${RADICLE_VERSION}-${ARCH}-unknown-linux-gnu.tar.xz
RADICLE_ARCHIVE=radicle-node-v${RADICLE_VERSION}-${ARCH}-unknown-linux-gnu.tar.xz

RADICLE_RUN_PATH=${RADICLE_RUN_PATH:-/usr/local/bin}
RADICLE_SYSTEMCTL_MODE=${RADICLE_SYSTEMCTL_MODE:-enabled}

RADICLE_CONFIG_PATH=${RADICLE_CONFIG_PATH:-/etc/radicle}
RADICLE_WORK_PATH=${RADICLE_WORK_PATH:-/var/lib/radicle}
RADICLE_STORAGE_PATH=${RADICLE_STORAGE_PATH:-/var/lib/radicle/storage}

RADICLE_RUN_USER=${RADICLE_RUN_USER:-radicle}
RADICLE_RUN_GROUP=${RADICLE_RUN_GROUP:-radicle}
RADICLE_RUN_USER_HOME=${RADICLE_RUN_USER_HOME:-/home/radicle}

RADICLE_BIND_ADDR=${RADICLE_BIND_ADDR:-0.0.0.0}
RADICLE_BIND_PORT=${RADICLE_BIND_PORT:-8776}
RADICLE_PEER_PORT=${RADICLE_PEER_PORT:-8777}
RADICLE_WEB_UI_PORT=${RADICLE_WEB_UI_PORT:-8080}

RADICLE_LOG_LEVEL=${RADICLE_LOG_LEVEL:-info}

echo "Installing Radicle ${RADICLE_VERSION}..."

# Create user and group
if ! id "$RADICLE_RUN_USER" &>/dev/null; then
    groupadd -r "$RADICLE_RUN_GROUP"
    useradd -r -g "$RADICLE_RUN_GROUP" -d "$RADICLE_RUN_USER_HOME" -s /bin/bash -c "Radicle service user" "$RADICLE_RUN_USER"
fi

# Create directories
mkdir -p "$RADICLE_CONFIG_PATH"
mkdir -p "$RADICLE_WORK_PATH"
mkdir -p "$RADICLE_STORAGE_PATH"
mkdir -p "$RADICLE_RUN_USER_HOME"

# Download and install Radicle
cd /tmp
echo "Downloading Radicle from ${RADICLE_URL}/${RADICLE_FILE}..."
curl -L -o "$RADICLE_ARCHIVE" "${RADICLE_URL}/${RADICLE_FILE}"

if [ ! -f "$RADICLE_ARCHIVE" ]; then
    echo "Failed to download Radicle archive"
    exit 1
fi

# Extract and install binaries
echo "Extracting Radicle..."
tar -xf "$RADICLE_ARCHIVE"
EXTRACT_DIR=$(tar -tf "$RADICLE_ARCHIVE" | head -1 | cut -f1 -d"/")
cd "$EXTRACT_DIR"

# Install binaries
cp rad "$RADICLE_RUN_PATH/"
cp radicle-node "$RADICLE_RUN_PATH/"
cp radicle-httpd "$RADICLE_RUN_PATH/"

# Make binaries executable
chmod +x "$RADICLE_RUN_PATH/rad"
chmod +x "$RADICLE_RUN_PATH/radicle-node"
chmod +x "$RADICLE_RUN_PATH/radicle-httpd"

# Set ownership
chown -R "$RADICLE_RUN_USER:$RADICLE_RUN_GROUP" "$RADICLE_WORK_PATH"
chown -R "$RADICLE_RUN_USER:$RADICLE_RUN_GROUP" "$RADICLE_STORAGE_PATH"
chown -R "$RADICLE_RUN_USER:$RADICLE_RUN_GROUP" "$RADICLE_RUN_USER_HOME"
chown -R "$RADICLE_RUN_USER:$RADICLE_RUN_GROUP" "$RADICLE_CONFIG_PATH"

# Initialize Radicle node if not already initialized
if [ ! -f "$RADICLE_STORAGE_PATH/node.json" ]; then
    echo "Initializing Radicle node..."
    sudo -u "$RADICLE_RUN_USER" RAD_HOME="$RADICLE_WORK_PATH" "$RADICLE_RUN_PATH/rad" auth --init
fi

# Create systemd service file
cat > /etc/systemd/system/radicle-node.service << EOF
[Unit]
Description=Radicle Node
After=network.target

[Service]
Type=simple
User=$RADICLE_RUN_USER
Group=$RADICLE_RUN_GROUP
Environment=RAD_HOME=$RADICLE_WORK_PATH
WorkingDirectory=$RADICLE_WORK_PATH
ExecStart=$RADICLE_RUN_PATH/radicle-node --listen $RADICLE_BIND_ADDR:$RADICLE_PEER_PORT --log $RADICLE_LOG_LEVEL
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

# Create systemd service file for HTTP daemon
if [ "${RADICLE_HTTPD_ENABLED:-true}" = "true" ]; then
    cat > /etc/systemd/system/radicle-httpd.service << EOF
[Unit]
Description=Radicle HTTP Daemon
After=network.target radicle-node.service
Requires=radicle-node.service

[Service]
Type=simple
User=$RADICLE_RUN_USER
Group=$RADICLE_RUN_GROUP
Environment=RAD_HOME=$RADICLE_WORK_PATH
WorkingDirectory=$RADICLE_WORK_PATH
ExecStart=$RADICLE_RUN_PATH/radicle-httpd --listen ${RADICLE_HTTPD_BIND_ADDR:-$RADICLE_BIND_ADDR}:${RADICLE_HTTPD_BIND_PORT:-$RADICLE_WEB_UI_PORT}
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

    # Enable and start HTTP daemon service
    systemctl daemon-reload
    systemctl "$RADICLE_SYSTEMCTL_MODE" radicle-httpd.service
    if [ "$RADICLE_SYSTEMCTL_MODE" = "enabled" ]; then
        systemctl start radicle-httpd.service
    fi
fi

# Enable and start node service
systemctl daemon-reload 
systemctl "$RADICLE_SYSTEMCTL_MODE" radicle-node.service

if [ "$RADICLE_SYSTEMCTL_MODE" = "enabled" ]; then
    systemctl start radicle-node.service
fi

# Cleanup
cd /
rm -rf /tmp/"$RADICLE_ARCHIVE" /tmp/"$EXTRACT_DIR"

echo "Radicle installation completed!"
echo "Node service: radicle-node.service"
if [ "${RADICLE_HTTPD_ENABLED:-true}" = "true" ]; then
    echo "HTTP daemon service: radicle-httpd.service"
    echo "Web UI available at: http://${RADICLE_HTTPD_BIND_ADDR:-$RADICLE_BIND_ADDR}:${RADICLE_HTTPD_BIND_PORT:-$RADICLE_WEB_UI_PORT}"
fi
echo "Storage path: $RADICLE_STORAGE_PATH"