package main

import (
	"fmt"
	"math/rand"
	"os"
	"time"

	"ur.gs/lysenko/handlers/quotedb"
)

// This is the same seeding as done by lysenko himself, so should provide the
// same results.
//
// Note, however, that we actually use the sqlite RANDOM() function to get a
// random quote ID, not this seed.
func init() {
	rand.Seed(time.Now().Unix())
}

func main() {
	if len(os.Args) != 3 {
		fmt.Fprintf(os.Stderr, "Usage: %v <filename> <channel>\n", os.Args[0])
		os.Exit(1)
	}

	filename := os.Args[1]
	channel := os.Args[2]

	if _, err := os.Stat(filename); os.IsNotExist(err) {
		fmt.Fprintf(os.Stderr, "File %q doesn't exist\n", filename)
		os.Exit(1)
	}

	db, err := quotedb.New(filename)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Couldn't open database %q: %v\n", filename, err)
		os.Exit(1)
	}

	for count := uint64(0); ; count++ {
		quote, err := db.FindRandomQuote(channel)
		if err != nil {
			fmt.Fprintf(os.Stderr, "i=%v: couldn't find random quote: %v\n", count, err)
			os.Exit(1)
		}

		fmt.Fprintf(os.Stdout, "%v ", quote.Id)
	}

	os.Exit(0)
}
