package main

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

	"code.ivysaur.me/contented"
)

func main() {
	cwd, _ := os.Getwd()

	listenAddr := flag.String("listen", "127.0.0.1:80", "IP/Port to bind server")
	dataDir := flag.String("data", cwd, "Directory for stored content")
	dbPath := flag.String("db", "contented.db", "Path for metadata database")
	appTitle := flag.String("title", "contented", "Title used in web interface")
	maxUploadMb := flag.Int("max", 8, "Maximum size of uploaded files in MiB (set zero for unlimited)")
	maxUploadSpeed := flag.Int("speed", 0, "Maximum upload speed in bytes/sec (set zero for unlimited)")
	trustXForwardedFor := flag.Bool("trustXForwardedFor", false, "Trust X-Forwarded-For reverse proxy headers")
	enableHomepage := flag.Bool("enableHomepage", true, "Enable homepage (disable for embedded use only)")
	diskFilesWorldReadable := flag.Bool("diskFilesWorldReadable", false, "Save files as 0644 instead of 0600")
	maxConcurrentThumbs := flag.Int("concurrentthumbs", contented.DEFAULT_MAX_CONCURRENT_THUMBS, "Simultaneous thumbnail generation")

	flag.Parse()

	svr, err := contented.NewServer(&contented.ServerOptions{
		DataDirectory:          *dataDir,
		DBPath:                 *dbPath,
		BandwidthLimit:         int64(*maxUploadSpeed),
		TrustXForwardedFor:     *trustXForwardedFor,
		EnableHomepage:         *enableHomepage,
		DiskFilesWorldReadable: *diskFilesWorldReadable,
		MaxConcurrentThumbs:    *maxConcurrentThumbs,
		ServerPublicProperties: contented.ServerPublicProperties{
			AppTitle:       *appTitle,
			MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024,
		},
	})
	if err != nil {
		log.Println(err.Error())
		os.Exit(1)
	}

	err = http.ListenAndServe(*listenAddr, svr)
	if err != nil {
		log.Println(err.Error())
		os.Exit(1)
	}
}
