// tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
//              manager, WARC/geminispace browser
// Copyright (C) 2021-2026 Sergey Matveev <stargrave@stargrave.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package fifos

import (
	"path/filepath"

	"go.stargrave.org/tofuproxy/caches"
	"go.stargrave.org/tofuproxy/warc"
)

func Start(fifos string) {
	go logger(LogCert, filepath.Join(fifos, "log-cert"))
	go logger(LogDANE, filepath.Join(fifos, "log-dane"))
	go logger(LogErr, filepath.Join(fifos, "log-err"))
	go logger(LogHTTPAuth, filepath.Join(fifos, "log-http-auth"))
	go logger(LogNonOK, filepath.Join(fifos, "log-non-ok"))
	go logger(LogOK, filepath.Join(fifos, "log-ok"))
	go logger(LogRedir, filepath.Join(fifos, "log-redir"))
	go logger(LogReq, filepath.Join(fifos, "log-req"))
	go logger(LogTLS, filepath.Join(fifos, "log-tls"))
	go logger(LogTLSAuth, filepath.Join(fifos, "log-tls-auth"))
	go logger(LogVarious, filepath.Join(fifos, "log-various"))
	go logger(LogWARC, filepath.Join(fifos, "log-warc"))

	go listAccepted(filepath.Join(fifos, "list-accepted"))
	go listHTTPAuth(filepath.Join(fifos, "list-http-auth"))
	go listRejected(filepath.Join(fifos, "list-rejected"))
	go listRestricted(filepath.Join(fifos, "list-restricted"))
	go listSpies(filepath.Join(fifos, "list-spies"))
	go listTLSAuth(filepath.Join(fifos, "list-tls-auth"))
	go listWARCs(filepath.Join(fifos, "list-warcs"))

	go del(
		&caches.AcceptedM, func(host string) { delete(caches.Accepted, host) },
		filepath.Join(fifos, "del-accepted"),
	)
	go del(
		&caches.HTTPAuthCacheM, func(host string) { delete(caches.HTTPAuthCache, host) },
		filepath.Join(fifos, "del-http-auth"),
	)
	go del(
		&caches.RejectedM, func(host string) { delete(caches.Rejected, host) },
		filepath.Join(fifos, "del-rejected"),
	)

	go addRestricted(filepath.Join(fifos, "add-restricted"))

	go addSpy(filepath.Join(fifos, "add-spies"))
	go del(
		&caches.SpiesM, func(host string) {
			for i, spy := range caches.Spies {
				if spy == host {
					caches.Spies[i] = caches.Spies[len(caches.Spies)-1]
					caches.Spies = caches.Spies[:len(caches.Spies)-1]
					return
				}
			}
		},
		filepath.Join(fifos, "del-spies"),
	)

	go addWARC(filepath.Join(fifos, "add-warcs"))
	go del(
		&warc.WARCsM, func(warcPath string) {
			delete(warc.WARCs, warcPath)
			delete(warc.WARCsOffsets, warcPath)
		},
		filepath.Join(fifos, "del-warcs"),
	)

	go addTLSAuth(filepath.Join(fifos, "add-tls-auth"))
	go del(
		&caches.TLSAuthCacheM, func(host string) { delete(caches.TLSAuthCache, host) },
		filepath.Join(fifos, "del-tls-auth"),
	)
}
