package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func init() {

	fmt.Println("Ingesting local hosts file")
	ingestLocalBlacklists()
	ingestLocalWhiteLists()

}

func ingestLocalBlacklists() {

	fmt.Println("ingestLocalBlacklist: collecting urls from all configs...")
	_HostsFiles := urlsMap{}
	for config := range ZabovConfigs {
		ZabovHostsFile := ZabovConfigs[config].ZabovHostsFile
		if len(ZabovHostsFile) == 0 {
			continue
		}
		configs := _HostsFiles[ZabovHostsFile]
		if configs == nil {
			configs = stringarray{}
			_HostsFiles[ZabovHostsFile] = configs
		}
		configs = append(configs, config)
		_HostsFiles[ZabovHostsFile] = configs
	}

	for ZabovHostsFile, configs := range _HostsFiles {
		file, err := os.Open(ZabovHostsFile)
		if err != nil {
			fmt.Println(err.Error())
		}
		defer file.Close()

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			d := scanner.Text()
			if len(d) == 0 || strings.TrimSpace(d)[0] == '#' {
				continue
			}
			DomainKill(d, ZabovHostsFile, configs)
			incrementStats("Blacklist", 1)

		}

		if err := scanner.Err(); err != nil {
			fmt.Println(err.Error())
		}
	}

}

func ingestLocalWhiteLists() {

	fmt.Println("ingestLocalWhiteLists: collecting urls from all configs...")
	_WhiteListFiles := urlsMap{}
	for config := range ZabovConfigs {
		ZabovWhiteList := ZabovConfigs[config].ZabovWhiteList
		if len(ZabovWhiteList) == 0 {
			continue
		}
		configs := _WhiteListFiles[ZabovWhiteList]
		if configs == nil {
			configs = stringarray{}
			_WhiteListFiles[ZabovWhiteList] = configs
		}
		configs = append(configs, config)
		_WhiteListFiles[ZabovWhiteList] = configs
	}

	for ZabovWhiteList, configs := range _WhiteListFiles {
		file, err := os.Open(ZabovWhiteList)
		if err != nil {
			fmt.Println(err.Error())
		}
		defer file.Close()

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			d := scanner.Text()
			if len(d) == 0 || strings.TrimSpace(d)[0] == '#' {
				continue
			}
			DomainWhiteList(d, ZabovWhiteList, configs)
			incrementStats("WhiteList", 1)

		}

		if err := scanner.Err(); err != nil {
			fmt.Println(err.Error())
		}
	}

}

func fileByLines(filename string) (blurls []string) {

	file, err := os.Open(filename)
	if err != nil {
		fmt.Println(err.Error())
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		d := scanner.Text()
		blurls = append(blurls, d)

	}

	if err := scanner.Err(); err != nil {
		fmt.Println(err.Error())
	}

	return

}
