#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

// Ensure the directory exists
const gitDataDir = path.resolve(__dirname, '../.next/static/git');
if (!fs.existsSync(gitDataDir)) {
  fs.mkdirSync(gitDataDir, { recursive: true });
}

let gitHash = 'unknown';

try {
  // Try to get the git hash
  gitHash = execSync('git rev-parse --short HEAD').toString().trim();
  console.log(`✅ Git commit hash: ${gitHash}`);
} catch (error) {
  console.error('⚠️ Could not get git commit hash:', error.message);
}

// Write the git hash to a file that can be imported
const gitInfoFilePath = path.resolve(gitDataDir, 'git-info.json');
fs.writeFileSync(
  gitInfoFilePath,
  JSON.stringify({ 
    commitHash: gitHash,
    buildTime: new Date().toISOString() 
  }, null, 2)
);

console.log(`📝 Git info written to: ${gitInfoFilePath}`);

// Also write to a location that can be imported during development
const gitInfoDevPath = path.resolve(__dirname, '../public/git-info.json');
fs.writeFileSync(
  gitInfoDevPath,
  JSON.stringify({ 
    commitHash: gitHash,
    buildTime: new Date().toISOString() 
  }, null, 2)
);

console.log(`📝 Git info written to: ${gitInfoDevPath}`); 