package main
import (
"os"
"path/filepath"
"runtime"
"strconv"
"github.com/integrii/flaggy"
)
func main() {
url := "https://toast.openfortress.fun/toast/"
var threads int = runtime.NumCPU()
var dir string
var dry bool
var http1 bool
var repair bool
var owGameInfo bool
var verifySigs bool
var tag string
var revno string
upgrade := flaggy.NewSubcommand("upgrade")
upgrade.Description = DESC_UPGRADE
upgrade.String(&url, "u", "url", DESC_REPO)
upgrade.Int(&threads, "c", "threads", "Number of threads to use for downloading, minimum two.")
upgrade.Bool(&http1, "1", "http1", DESC_HTTP1)
upgrade.Bool(&owGameInfo, "G", "overwrite-gameinfo", "When upgrading in a directory named open_fortress, overwrite any existing gameinfo.txt file.")
upgrade.Bool(&verifySigs, "s", "verify-sigs", DESC_KEY_STDIN+filepath.Dir(quarantineDir)+".")
upgrade.AddPositionalValue(&dir, "directory", 1, true, DESC_FILES)
flaggy.AttachSubcommand(upgrade, 1)
verify := flaggy.NewSubcommand("verify")
verify.Description = "Verifies an installation's files."
verify.AddPositionalValue(&dir, "directory", 1, true, "Directory of game files.")
verify.String(&url, "u", "url", DESC_REPO)
verify.Bool(&http1, "1", "http1", DESC_HTTP1)
verify.Bool(&repair, "r", "repair", "Repair the files where problems are found automatically.")
verify.Bool(&owGameInfo, "G", "overwrite-gameinfo", "When repairing in a directory named open_fortress, overwrite any existing gameinfo.txt file.")
verify.Bool(&verifySigs, "s", "verify-sigs", DESC_KEY_STDIN+filepath.Dir(quarantineDir)+".")
flaggy.AttachSubcommand(verify, 1)
var tvndir string
toast := flaggy.NewSubcommand("toast")
toast.Description = "Toasts files. Don't use this unless you know what you're doing."
toast.AddPositionalValue(&dir, "game files", 1, true, "Directory of game files to reference.")
toast.AddPositionalValue(&tvndir, "tvn directory", 2, true, "Directory of TVN folder to add changes to.")
toast.Bool(&dry, "d", "dry", DESC_DRY)
toast.Int(&threads, "c", "threads", "Number of threads to use when calculating changes.")
flaggy.AttachSubcommand(toast, 1)
rewrite := flaggy.NewSubcommand("rewrite")
rewrite.Description = DESC_REWRITE
rewriteWrap := flaggy.NewSubcommand("wrap")
rewriteWrap.Description = DESC_REWRITE_WRAP
rewriteWrap.AddPositionalValue(&tvndir, "tvn repository", 1, true, "TVN repository to convert.")
rewriteTag := flaggy.NewSubcommand("tag")
rewriteTag.Description = DESC_REWRITE_TAG
rewriteTag.AddPositionalValue(&tvndir, "tvn repository", 1, true, "TVN repository with revision.")
rewriteTag.AddPositionalValue(&revno, "revision number", 2, true, "Number of revision file to tag.")
rewriteTag.AddPositionalValue(&tag, "tag value", 3, false, "String to tag value with, if nothing is specified it is removed.")
rewrite.AttachSubcommand(rewriteWrap, 1)
rewrite.AttachSubcommand(rewriteTag, 1)
flaggy.AttachSubcommand(rewrite, 1)
flaggy.SetVersion(VERSION)
flaggy.Parse()
switch {
case upgrade.Used:
if threads < 1 {
flaggy.ShowHelp("Must have at least a single thread.")
os.Exit(1)
}
os.Exit(upgradeMain(dir, url, threads, http1, owGameInfo, verifySigs))
case verify.Used:
os.Exit(verifyMain(dir, url, repair, http1, owGameInfo, verifySigs))
case toast.Used:
if threads < 1 {
flaggy.ShowHelp("Must have at least a single thread.")
os.Exit(1)
}
os.Exit(toastMain(dir, tvndir, dry, threads))
case rewrite.Used:
switch {
case rewriteWrap.Used:
os.Exit(rewriteWrapMain(tvndir))
case rewriteTag.Used:
revno, err := strconv.Atoi(revno)
if err != nil || revno < 0 {
flaggy.ShowHelp("Invalid revision number supplied.")
os.Exit(1)
}
os.Exit(rewriteTagMain(tvndir, revno, tag))
default:
flaggy.ShowHelp("Please select a rewrite operation.")
os.Exit(1)
}
}
flaggy.ShowHelp("Please choose a subcommand.")
os.Exit(1)
}