package main

import (
	"encoding/json"
	"fmt"
	"os"
)

func main() {
	// Read package.json
	data, err := os.ReadFile("package.json")
	if err != nil {
		fmt.Println("Error reading package.json, skipping version file generation.")
		os.Exit(0)
	}

	// Parse package.json
	var pkg struct {
		Version string `json:"version"`
	}
	if err := json.Unmarshal(data, &pkg); err != nil {
		fmt.Println("Error parsing package.json, skipping version file generation.")
		os.Exit(0)
	}

	// Create the version file
	content := fmt.Sprintf("package updater\n\n// Generated by go:generate. DO NOT EDIT.\n\nconst PkgVersion = %q\n", pkg.Version)
	err = os.WriteFile("version.go", []byte(content), 0644)
	if err != nil {
		fmt.Printf("Error writing version file: %v\n", err)
		os.Exit(1)
	}

	fmt.Println("Generated version.go with version:", pkg.Version)
}
