// +build cgo

package mark2web

import (
	"io"
	"os"

	"gitbase.de/apairon/mark2web/pkg/logger"
	"github.com/itchio/go-brotli/enc"
)

var brotliSupported = true

func handleBrotliCompression(filename string, content []byte) {
	brFilename := filename + ".br"

	logger.I("writing to compressed output file: %s", brFilename)

	f, err := os.Create(brFilename)
	logger.Eexit(err, "could not create file '%s'", brFilename)

	defer f.Close()

	bw := enc.NewBrotliWriter(f, nil)
	defer bw.Close()

	if content != nil {
		// content given
		_, err = bw.Write(content)
		logger.Eexit(err, "could not write brotli content for '%s'", filename)
	} else {
		// read file
		r, err := os.Open(filename)
		logger.Eexit(err, "could not open file '%s'", filename)
		defer r.Close()

		_, err = io.Copy(bw, r)
		logger.Eexit(err, "could not write brotli file for '%s'", filename)
	}
}
