#!/usr/bin/env bash

GOROOT=${1:-../go}
JSONROOT="."

cp $JSONROOT/alias_gen.go $JSONROOT/alias_gen.go.bak
rm -r $JSONROOT/*.go $JSONROOT/internal $JSONROOT/jsontext $JSONROOT/v1
mv $JSONROOT/alias_gen.go.bak $JSONROOT/alias_gen.go
cp -r $GOROOT/src/encoding/json/v2/*.go $JSONROOT/
cp -r $GOROOT/src/encoding/json/internal/ $JSONROOT/internal/
cp -r $GOROOT/src/encoding/json/jsontext/ $JSONROOT/jsontext/
mkdir $JSONROOT/v1
for X in $GOROOT/src/encoding/json/v2_*.go; do
    cp $X $JSONROOT/v1/$(basename $X | sed "s/v2_//")
done
cd $JSONROOT
for X in $(git ls-files --cached --others --exclude-standard | grep ".*[.]go$"); do
    if [ ! -e "$X" ]; then
        continue
    fi
    sed -i 's/go:build goexperiment.jsonv2 && !goexperiment.jsonformat$/go:build (!goexperiment.jsonv2 || !go1.25) \&\& !goexperiment.jsonformat/' $X
    sed -i 's/go:build goexperiment.jsonv2 && goexperiment.jsonformat$/go:build (!goexperiment.jsonv2 || !go1.25) \&\& goexperiment.jsonformat/' $X
    sed -i 's/go:build goexperiment.jsonv2$/go:build !goexperiment.jsonv2 || !go1.25/' $X
    sed -i 's|"encoding/json/v2"|"github.com/metacubex/jsonv2"|' $X
    sed -i 's|"encoding/json/internal"|"github.com/metacubex/jsonv2/internal"|' $X
    sed -i 's|"encoding/json/internal/jsonflags"|"github.com/metacubex/jsonv2/internal/jsonflags"|' $X
    sed -i 's|"encoding/json/internal/jsonopts"|"github.com/metacubex/jsonv2/internal/jsonopts"|' $X
    sed -i 's|"encoding/json/internal/jsontest"|"github.com/metacubex/jsonv2/internal/jsontest"|' $X
    sed -i 's|"encoding/json/internal/jsonwire"|"github.com/metacubex/jsonv2/internal/jsonwire"|' $X
    sed -i 's|"encoding/json/jsontext"|"github.com/metacubex/jsonv2/jsontext"|' $X
    sed -i 's|"encoding/json"|"github.com/metacubex/jsonv2/v1"|' $X
    sed -i 's|"internal/zstd"|"github.com/metacubex/jsonv2/internal/zstd"|' $X
    goimports -w $X
done
sed -i 's/v2[.]struct/json.struct/' $JSONROOT/errors_test.go
sed -i 's|jsonv1 "github.com/metacubex/jsonv2/v1"|jsonv1 "encoding/json"|' $JSONROOT/bench_test.go

# Remove documentation that only makes sense within the stdlib.
sed -i  '/This package .* is experimental/,+4d' $JSONROOT/doc.go
sed -i  '/This package .* is experimental/,+4d' $JSONROOT/jsontext/doc.go

git checkout internal/zstd # we still need local copy of zstd for testing

go run alias_gen.go "encoding/json"          $JSONROOT/v1
go run alias_gen.go "encoding/json/v2"       $JSONROOT
go run alias_gen.go "encoding/json/jsontext" $JSONROOT/jsontext
