package templater

import (
	"fmt"
	"os"

	"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)

type DebRepository struct {
	Repository
}

func (hr *DebRepository) Add() error {
	//deb http://fr.archive.ubuntu.com/ubuntu/ focal main restricted

	data := fmt.Sprintf("deb %s", hr.URL)
	if err := os.WriteFile("/etc/apt/source.list.d", []byte(data), 0600); err != nil {
		return err
	}

	return nil
}

func (hr *DebRepository) Update() error {
	if _, stdErr, err := utils.RunSystemCommand("apt", "update", "-y"); err != nil {
		return fmt.Errorf("%s [%s]", stdErr, err)
	}

	return nil
}

func (hr *DebRepository) Delete() error {
	//TODO
	return nil
}
func (hr *DebRepository) Manage() error {
	if hr.Enabled {
		return hr.Add()
	} else {
		return hr.Delete()
	}
}