#!/bin/bash
# Setup storage box directories and install SSH key
# Run once when setting up storage box access
set -euo pipefail

REMOTE="u458763-sub3@u458763.your-storagebox.de"
PORT=23
PUBKEY=$(cat /home/devops/.ssh/id_ed25519.pub)

echo "=== Storage Box Setup ==="
echo "Remote: $REMOTE (port $PORT)"
echo "Public key: $PUBKEY"
echo

# Test connection
echo "Testing connection..."
if sftp -P $PORT -oBatchMode=yes $REMOTE <<SFTP 2>/dev/null
ls
bye
SFTP
then
    echo "Connection OK"
else
    echo "ERROR: Cannot connect to storage box"
    echo "Please install the public key via Hetzner Robot panel:"
    echo "  1. Login to https://robot.your-server.de"
    echo "  2. Go to Storage Boxes -> u458763 -> Sub-accounts -> sub3"
    echo "  3. Add SSH key: $PUBKEY"
    exit 1
fi

# Create release directories
echo "Creating release directories..."
sftp -P $PORT -oBatchMode=yes $REMOTE <<SFTP
mkdir releases
mkdir releases/android
mkdir releases/ios
mkdir releases/web
mkdir releases/backups
bye
SFTP

echo "Storage box setup complete"
echo "Directories: releases/{android,ios,web,backups}"
