package mountfilesystem

import (
	"fmt"
	"log"
	"os"

	"golang.org/x/sys/unix"
	"kisekinopureya.com.tr/updater/internal/logger"
)

func MountFileSystem(source string, target string, fsType string, flags int, data string) {
	logger.InitializeLogger("/mnt/var/log/patcher")

	if err := unix.Mkdir(target, 0755); err != nil && !os.IsExist(err) {
		log.Panic(err)
	}

	err := unix.Mount(source, target, fsType, uintptr(flags), data)
	if err != nil {
		log.Panic(fmt.Errorf("mount failed: %w", err))
	}
}

func UnmountFileSystem(target string) {
	logger.InitializeLogger("/mnt/var/log/patcher")
	err := unix.Unmount(target, 0)
	if err != nil {
		log.Panic(err)
	}
}
