#!/bin/bash
set -e

SSH_TARGET="Daniel Galiano Banqueri@192.168.1.129"
NAS_PATH="/volume1/docker/dashboard-keller"

cat << 'REMOTE_SCRIPT' > remote_deploy.sh
#!/bin/bash
set -e
NAS_PATH="/volume1/docker/dashboard-keller"
REMOTE_SCRIPT

files=(
  "src/dashboard.js"
  "src/dashboard.html"
  "src/dashboard.css"
  "docker-compose.yml"
  "Dockerfile"
  "desplegar_en_nas.sh"
)

for f in "${files[@]}"; do
  B64=$(base64 < "$f")
  echo "echo '$B64' | base64 -d > $NAS_PATH/$f" >> remote_deploy.sh
done

echo "rm -f $NAS_PATH/entrypoint.sh $NAS_PATH/wisen_fetcher.rb $NAS_PATH/src/wisen_data.json" >> remote_deploy.sh
echo "cd $NAS_PATH && docker compose restart dashboard-keller && docker compose restart tailscale-funnel" >> remote_deploy.sh

# Now transfer the script using cat through SSH into the home directory where we have write access
cat remote_deploy.sh | ssh "$SSH_TARGET" "cat > remote_deploy.sh"
# Execute it
ssh -t "$SSH_TARGET" "sudo bash remote_deploy.sh && rm remote_deploy.sh"
