#!/bin/bash

set -e

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

echo -e "${GREEN}Building Debian package for Sesterce CLI...${NC}"

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

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

# Update changelog with new version and commits
./scripts/update-changelog.sh

# Clean previous builds
echo -e "${YELLOW}Cleaning previous builds...${NC}"
rm -rf dist/
rm -rf node_modules/
rm -rf debian/sesterce-cli/

# Install dependencies
echo -e "${YELLOW}Installing dependencies...${NC}"
npm install

# Build the application
echo -e "${YELLOW}Building application...${NC}"
npm run build:ci

# Verify version synchronization
echo -e "${YELLOW}Verifying version synchronization...${NC}"
./scripts/verify-version.sh

# Build the Debian package
echo -e "${YELLOW}Building Debian package...${NC}"
dpkg-buildpackage -b -us -uc

echo -e "${GREEN}Debian package built successfully!${NC}"
echo -e "${YELLOW}Package file: ../sesterce-cli_${PACKAGE_VERSION}_all.deb${NC}" 