#!/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}🔍 Verifying version synchronization...${NC}"

# Get version from package.json
if [ -f "package.json" ]; then
    NPM_VERSION=$(node -p "require('./package.json').version")
    echo -e "${GREEN}✅ npm version: ${NPM_VERSION}${NC}"
else
    echo -e "${RED}❌ package.json not found${NC}"
    exit 1
fi

# Get version from debian/changelog
if [ -f "debian/changelog" ]; then
    DEB_VERSION=$(head -1 debian/changelog | sed -n 's/.*(\([^)]*\)).*/\1/p')
    echo -e "${GREEN}✅ Debian version: ${DEB_VERSION}${NC}"
else
    echo -e "${RED}❌ debian/changelog not found${NC}"
    exit 1
fi

# Extract base version from Debian version (remove -1 suffix)
DEB_BASE_VERSION=$(echo "$DEB_VERSION" | sed 's/-[0-9]*$//')

# Compare versions
if [ "$NPM_VERSION" = "$DEB_BASE_VERSION" ]; then
    echo -e "${GREEN}✅ Versions are synchronized!${NC}"
    echo -e "${BLUE}📦 npm: ${NPM_VERSION}${NC}"
    echo -e "${BLUE}📦 Debian: ${DEB_VERSION} (base: ${DEB_BASE_VERSION})${NC}"
    exit 0
else
    echo -e "${RED}❌ Version mismatch!${NC}"
    echo -e "${YELLOW}📦 npm: ${NPM_VERSION}${NC}"
    echo -e "${YELLOW}📦 Debian: ${DEB_VERSION} (base: ${DEB_BASE_VERSION})${NC}"
    echo -e "${YELLOW}💡 To fix, update debian/changelog with version ${NPM_VERSION}-1${NC}"
    exit 1
fi 