package main

import (
	"fmt"
	"os"
	"os/signal"
	"sync"
	"syscall"
)

// section: code
func main() {
	// wire up a quit channel to monitor
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt)

	// create a hup channel
	hup := make(chan os.Signal, 1)
	signal.Notify(hup, os.Interrupt, syscall.SIGHUP)

	wg := &sync.WaitGroup{}
	wg.Add(1)
	go hotReload(hup, wg)

	<-quit
	fmt.Println("waiting for clean shutdown")
	wg.Wait()
	fmt.Println("clean shutdown")
}

func hotReload(hup chan os.Signal, wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		s := <-hup
		switch s {
		case syscall.SIGHUP:
			fmt.Println("reloading config...")
		case os.Interrupt:
			fmt.Println("closing config reload")
			return
		}
	}
}

// section: code

/*
// section: commands
$ ps | grep hup
46490 ttys006    0:00.01 ./hup

$ kill -s SIGHUP 46490

$ kill -s SIGINT 46490
// section: commands

// section: output
$ ./hup
reloading config...
waiting for clean shutdown
closing config reload
clean shutdown
// section: output




*/
