// Author: NiuJiuRu
// Email: niujiuru@qq.com

package baseapp

import (
	"fmt"
	"os"
	"os/signal"
	"path/filepath"
	"sync/atomic"
	"syscall"

	"github.com/alexflint/go-filemutex"
)

var Version, BuildTime string // 由Makefile传入

var isExit atomic.Bool
var exitCh = make(chan struct{})

func ModuleInit() {
	InitPath()
	InitLogger()
}

func SingleInstanceRun() { // 非阻塞单实例运行
	lockFile := filepath.Join(RUN_DIR, EXEC_FILENAME+".lock")
	mux, err := filemutex.New(lockFile)
	if err != nil {
		Logger.Fatalf("An error occurred while calling the filemutex.New() function: %v", err)
	}

	if err = mux.TryLock(); err != nil {
		Logger.Warnf("Another instance is already running!")
		mux.Close()
		Logger.Infof("Exited")
		os.Exit(0)
	}

	pid := os.Getpid()
	err = os.WriteFile(lockFile, fmt.Appendf(nil, "%d", pid), 0644)
	if err != nil {
		Logger.Warnf("Can't to write PID to the \"%s\" file: %v!", lockFile, err)
	}

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
	go func() {
		for s := range ch {
			switch s {
			case syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1:
				Logger.Infof("Received signal: %v", s)
				mux.Close()
				os.RemoveAll(lockFile)
				isExit.Store(true)
				close(exitCh)
				return
			default:
				Logger.Warnf("Received unexpected signal: %v!", s)
			}
		}
	}()
}

func IsExit1() bool {
	return isExit.Load()
}

func IsExit2() <-chan struct{} {
	return exitCh
}

func SafeExit() { // 安全退出
	pid := os.Getpid()
	syscall.Kill(pid, syscall.SIGUSR1)
	<-exitCh
}
