package main

import (
	"net"

	"github.com/hashicorp/mdns"

	"github.com/paulbsd/go-aptproxy/src/server"

	"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	var (
		host      = flag.String("host", "0.0.0.0", "`host` to listen on")
		port      = flag.Int("port", 8000, "`port` to listen on")
		directory = flag.String("directory", "/var/cache/go-aptproxy", "`directory` used for storing packages")
	)
	flag.Parse()

	// Create the HTTP server and initialize the cache
	addr := fmt.Sprintf("%s:%d", *host, *port)
	httpServer, err := server.NewServer(addr, *directory)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	log.Println("Starting HTTP server...")
	if err = httpServer.Start(); err != nil {
		log.Println(err)
		os.Exit(1)
	}
	defer func() {
		httpServer.Stop()
		log.Println("Stopping HTTP server...")
	}()

	service, err := mdns.NewMDNSService(
		"thinkpad",
		"_apt_proxy._tcp",
		"",
		"",
		*port,
		[]net.IP{GetOutboundIP()},
		[]string{"go-aptproxy - Smarter APT Proxy"},
	)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	log.Println("Starting mDNS server...")
	mDNSServer, err := mdns.NewServer(&mdns.Config{Zone: service})
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	defer func() {
		mDNSServer.Shutdown()
		log.Println("Stopping mDNS server...")
	}()

	// Wait for a signal
	c := make(chan os.Signal)
	signal.Notify(c, syscall.SIGINT)
	<-c
	log.Println("Caught SIGINT")
}

// Get preferred outbound ip of this machine
func GetOutboundIP() net.IP {
	conn, err := net.Dial("udp", "8.8.8.8:53")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	localAddr := conn.LocalAddr().(*net.UDPAddr)

	return localAddr.IP
}
