package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"

	"isosilo/internal/handlers"
)

func main() {
	dir := flag.String("dir", ".", "Directory to serve")
	addr := flag.String("addr", ":8080", "Address")
	flag.Parse()

	if info, err := os.Stat(*dir); err != nil || !info.IsDir() {
		log.Fatalf("Invalid directory: %s", *dir)
	}

	mux := http.NewServeMux()
	h := handlers.New(*dir)

	mux.HandleFunc("/browse/", h.BrowseISO)
	mux.HandleFunc("/file/", h.DownloadFile)
	mux.HandleFunc("/raw/", h.RawFile)
	mux.HandleFunc("/static/", h.ServeStatic)
	mux.HandleFunc("/", h.ListISOs)

	fmt.Printf("ISOSilo running at %s\n", *addr)
	log.Fatal(http.ListenAndServe(*addr, mux))
}
