#!/bin/bash

# Release script for Tailscale Clone
set -e

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

VERSION=${1:-"1.0.0"}
RELEASE_DIR="releases/v${VERSION}"

echo -e "${GREEN}Creating release v${VERSION}...${NC}"

# Create release directory
mkdir -p "${RELEASE_DIR}"

# Build all platforms
echo -e "${YELLOW}Building for all platforms...${NC}"
./scripts/build-all.sh

# Create platform-specific packages
echo -e "${YELLOW}Creating release packages...${NC}"

# Linux package
echo -e "${GREEN}Creating Linux package...${NC}"
mkdir -p "${RELEASE_DIR}/linux"
cp bin/tailscale-client-linux-* "${RELEASE_DIR}/linux/"
cp bin/tailscale-controller-linux-amd64 "${RELEASE_DIR}/linux/"
cp README.md "${RELEASE_DIR}/linux/"
cp LICENSE "${RELEASE_DIR}/linux/" 2>/dev/null || echo "# MIT License" > "${RELEASE_DIR}/linux/LICENSE"

# Create Linux install script
cat > "${RELEASE_DIR}/linux/install.sh" << 'EOF'
#!/bin/bash
echo "Installing Tailscale Clone..."
sudo cp tailscale-controller-linux-amd64 /usr/local/bin/tailscale-controller
sudo cp tailscale-client-linux-amd64 /usr/local/bin/tailscale-client
sudo chmod +x /usr/local/bin/tailscale-controller
sudo chmod +x /usr/local/bin/tailscale-client
echo "Installation complete!"
echo "Usage:"
echo "  Controller: sudo tailscale-controller"
echo "  Client: sudo tailscale-client -name my-node"
EOF
chmod +x "${RELEASE_DIR}/linux/install.sh"

# Windows package
echo -e "${GREEN}Creating Windows package...${NC}"
mkdir -p "${RELEASE_DIR}/windows"
cp bin/tailscale-client-windows-*.exe "${RELEASE_DIR}/windows/"
cp README.md "${RELEASE_DIR}/windows/"
cp LICENSE "${RELEASE_DIR}/windows/" 2>/dev/null || echo "# MIT License" > "${RELEASE_DIR}/windows/LICENSE"

# Create Windows batch file
cat > "${RELEASE_DIR}/windows/install.bat" << 'EOF'
@echo off
echo Installing Tailscale Clone...
copy tailscale-client-windows-amd64.exe C:\Windows\System32\tailscale-client.exe
echo Installation complete!
echo Usage:
echo   tailscale-client.exe -name my-node
pause
EOF

# macOS package
echo -e "${GREEN}Creating macOS package...${NC}"
mkdir -p "${RELEASE_DIR}/macos"
cp bin/tailscale-client-darwin-* "${RELEASE_DIR}/macos/"
cp README.md "${RELEASE_DIR}/macos/"
cp LICENSE "${RELEASE_DIR}/macos/" 2>/dev/null || echo "# MIT License" > "${RELEASE_DIR}/macos/LICENSE"

# Create macOS install script
cat > "${RELEASE_DIR}/macos/install.sh" << 'EOF'
#!/bin/bash
echo "Installing Tailscale Clone..."
sudo cp tailscale-client-darwin-amd64 /usr/local/bin/tailscale-client
sudo chmod +x /usr/local/bin/tailscale-client
echo "Installation complete!"
echo "Usage:"
echo "  sudo tailscale-client -name my-node"
EOF
chmod +x "${RELEASE_DIR}/macos/install.sh"

# Create archives
echo -e "${YELLOW}Creating archives...${NC}"
cd "${RELEASE_DIR}"

# Linux archive
tar -czf "tailscale-clone-linux-v${VERSION}.tar.gz" linux/

# Windows archive
zip -r "tailscale-clone-windows-v${VERSION}.zip" windows/

# macOS archive
tar -czf "tailscale-clone-macos-v${VERSION}.tar.gz" macos/

# Create checksums
echo -e "${YELLOW}Creating checksums...${NC}"
sha256sum *.tar.gz *.zip > SHA256SUMS

cd ../..

echo -e "${GREEN}Release v${VERSION} created successfully!${NC}"
echo -e "${GREEN}Release files:${NC}"
ls -la "${RELEASE_DIR}/"

echo ""
echo -e "${YELLOW}Distribution files:${NC}"
echo "  Linux:   ${RELEASE_DIR}/tailscale-clone-linux-v${VERSION}.tar.gz"
echo "  Windows: ${RELEASE_DIR}/tailscale-clone-windows-v${VERSION}.zip"
echo "  macOS:   ${RELEASE_DIR}/tailscale-clone-macos-v${VERSION}.tar.gz" 