package main

import (
	"os"
	"os/signal"
	"syscall"

	"github.com/einand/applephotos/internal/photos"
)

func main() {
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)

	done := make(chan struct{})
	var rc int

	go func() {
		rc = run(os.Args[1:], os.Stdout, os.Stderr, photos.DefaultBridge)
		close(done)
	}()

	select {
	case <-done:
	case sig := <-sigCh:
		photos.DefaultBridge.Cancel()
		os.Stderr.Write([]byte("\nreceived signal, finishing current file...\n"))
		<-done
		_ = sig
	}

	os.Exit(rc)
}