package main

import (
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"
	"strconv"
	"strings"
	"time"

	"srs.epita.fr/fic-server/settings"

	"gopkg.in/fsnotify.v1"
)

var SettingsDistDir = "./SETTINGSDIST/"
var TmpSettingsDirectory string
var TmpSettingsDistDirectory string

func watchsubdir(l *distList, watcher *fsnotify.Watcher, pathname string) error {
	log.Println("Watch new directory:", pathname)
	if err := watcher.Add(pathname); err != nil {
		return err
	}

	if ds, err := ioutil.ReadDir(pathname); err != nil {
		return err
	} else {
		for _, d := range ds {
			p := path.Join(pathname, d.Name())
			if d.Mode().IsRegular() && d.Name() != ".tmp" {
				l.treat(p)
			}
		}
		return nil
	}
}

func main() {
	flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where read settings")
	flag.StringVar(&SettingsDistDir, "settingsDist", SettingsDistDir, "Directory where place settings to distribute")
	var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
	flag.Parse()

	log.SetPrefix("[evdist] ")

	settings.SettingsDir = path.Clean(settings.SettingsDir)

	log.Println("Creating settingsDist directory...")
	TmpSettingsDistDirectory = path.Join(SettingsDistDir, ".tmp")
	if _, err := os.Stat(TmpSettingsDistDirectory); os.IsNotExist(err) {
		if err = os.MkdirAll(TmpSettingsDistDirectory, 0751); err != nil {
			log.Fatal("Unable to create settingsdist directory:", err)
		}
	}

	TmpSettingsDirectory = path.Join(settings.SettingsDir, ".tmp")
	if _, err := os.Stat(TmpSettingsDirectory); os.IsNotExist(err) {
		if err = os.MkdirAll(TmpSettingsDirectory, 0751); err != nil {
			log.Fatal("Unable to create settings directory:", err)
		}
	}

	log.Println("Registering directory events...")
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	l := &distList{}
	l.Timer = time.NewTimer(time.Minute)

	if err := watchsubdir(l, watcher, settings.SettingsDir); err != nil {
		log.Fatal(err)
	}

	watchedNotify := fsnotify.Create

	for {
		select {
		case <-l.Timer.C:
			if v := l.Pop(); v != nil {
				log.Printf("TREATING DIFF: %v", v)

				v, err = settings.ReadNextSettingsFile(path.Join(settings.SettingsDir, fmt.Sprintf("%d.json", v.Id)), v.Id)
				if err != nil {
					log.Printf("Unable to read json: %s", err.Error())
				} else if cur_settings, err := settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile)); err != nil {
					log.Printf("Unable to read settings.json: %s", err.Error())
				} else {
					cur_settings = settings.MergeSettings(*cur_settings, v.Values)

					if err = settings.SaveSettings(path.Join(TmpSettingsDirectory, "settings.json"), cur_settings); err != nil {
						log.Printf("Unable to save settings.json to tmp dir: %s", err.Error())
					} else if err = os.Rename(path.Join(TmpSettingsDirectory, "settings.json"), path.Join(settings.SettingsDir, "settings.json")); err != nil {
						log.Printf("Unable to move settings.json to dest dir: %s", err.Error())
					} else if err = os.Remove(path.Join(settings.SettingsDir, fmt.Sprintf("%d.json", v.Id))); err != nil {
						log.Printf("Unable to remove initial diff file (%d.json): %s", v.Id, err.Error())
					}
				}
			}
			l.ResetTimer()
		case ev := <-watcher.Events:
			if d, err := os.Lstat(ev.Name); err == nil && ev.Op&watchedNotify == watchedNotify && d.Name() != ".tmp" && d.Mode().IsRegular() {
				if *debugINotify {
					log.Println("Treating event:", ev, "for", ev.Name)
				}
				l.treat(ev.Name)
			} else if err == nil && ev.Op&watchedNotify == fsnotify.Remove && d.Mode().IsRegular() {
				if *debugINotify {
					log.Println("Treating deletion event:", ev, "for", ev.Name)
				}
				if ts, err := strconv.ParseInt(strings.TrimSuffix(path.Base(ev.Name), ".json"), 10, 64); err == nil {
					log.Println("Unable to parseint", ev.Name, err.Error())
				} else {
					l.DelEvent(ts)
				}
			} else if err == nil && ev.Op&fsnotify.Write == fsnotify.Write {
				log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
				watchedNotify = fsnotify.Write
			} else if *debugINotify {
				log.Println("Skipped event:", ev, "for", ev.Name)
			}
		case err := <-watcher.Errors:
			log.Println("error:", err)
		}
	}
}

func (l *distList) treat(raw_path string) {
	bpath := path.Base(raw_path)

	if bpath == "challenge.json" || bpath == "settings.json" {
		log.Printf("Copying %s to SETTINGDIST...", bpath)
		// Copy content through tmp file
		fd, err := os.Open(raw_path)
		if err != nil {
			log.Printf("ERROR: Unable to open %s: %s", raw_path, err.Error())
			return
		}
		defer fd.Close()

		tmpfile, err := ioutil.TempFile(TmpSettingsDistDirectory, "")
		if err != nil {
			log.Printf("ERROR: Unable to create temporary file for %s: %s", bpath, err.Error())
			return
		}

		_, err = io.Copy(tmpfile, fd)
		tmpfile.Close()

		if err != nil {
			log.Printf("ERROR: Unable to copy content to temporary file (%s): %s", bpath, err.Error())
			return
		}

		os.Chmod(tmpfile.Name(), 0644)

		if err = os.Rename(tmpfile.Name(), path.Join(SettingsDistDir, bpath)); err != nil {
			log.Println("ERROR: Unable to move file:", err)
			return
		}
	} else if ts, err := strconv.ParseInt(strings.TrimSuffix(bpath, ".json"), 10, 64); err == nil {
		activateTime := time.Unix(ts, 0)

		log.Printf("Preparing %s: activation time at %s", bpath, activateTime)

		l.AddEvent(&settings.NextSettingsFile{
			Id:   ts,
			Date: activateTime,
		})
	} else {
		log.Println("WARNING: Unknown file to treat: not a valid timestamp:", err.Error())
	}
}
