#!/bin/bash

set -e

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

echo -e "${BLUE}📝 Updating debian/changelog...${NC}"

# Get version from package.json
VERSION=$(node -p "require('./package.json').version")
PACKAGE_VERSION="${VERSION}-1"

echo -e "${YELLOW}Version: ${VERSION}${NC}"
echo -e "${YELLOW}Package version: ${PACKAGE_VERSION}${NC}"

# Get the latest commit message
LATEST_COMMIT=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "Release version ${VERSION}")

# Get recent commits for changelog (last 5 commits)
echo -e "${YELLOW}📋 Getting recent commits...${NC}"
RECENT_COMMITS=$(git log -5 --pretty=format:"  * %s" 2>/dev/null || echo "  * Release version ${VERSION}")

# Create new changelog entry
echo -e "${YELLOW}📝 Creating changelog entry...${NC}"
cat > debian/changelog.new << EOF
sesterce-cli (${PACKAGE_VERSION}) unstable; urgency=medium

  * Release version ${VERSION}
${RECENT_COMMITS}
  * GPU Cloud Management
  * AI Inference Services
  * Container Registry Management
  * SSH Key Management
  * Volume Management

 -- Sesterce <support@sesterce.com>  $(date -R)

EOF

# Append existing changelog
cat debian/changelog >> debian/changelog.new
mv debian/changelog.new debian/changelog

echo -e "${GREEN}✅ Changelog updated successfully!${NC}"
echo -e "${BLUE}📋 Updated changelog preview:${NC}"
head -15 debian/changelog 