#!/bin/bash
set -e

STORAGE_PATHS=$1
PORT=${2:-6060}

if [ -z "$STORAGE_PATHS" ]; then
    echo "Usage: install.sh /path1,/path2 [port]"
    exit 1
fi

APP=/usr/local/bin/storage-api

echo "Installing storage-api..."

curl -fsSL "https://filestore.myweb.net.au/File/DownloadFile?path=storage-api%2Fstorage-api" -o $APP

chmod +x $APP


cat > /etc/systemd/system/storage-api.service <<EOF
[Unit]
Description=Storage Monitoring API
After=network.target

[Service]
Type=simple
ExecStart=$APP
Restart=always
RestartSec=5
Environment="STORAGE_PATHS=$STORAGE_PATHS"
Environment="PORT=$PORT"

[Install]
WantedBy=multi-user.target
EOF


systemctl daemon-reload
systemctl enable storage-api
systemctl restart storage-api

echo "Installed"
echo "Port: $PORT"
echo "Storage paths: $STORAGE_PATHS"