package pkg

import (
	"io"
	"os"
)

// config.go - Configuration constants for the pkg tool.
const (
	VERSION = "0.11.0"

	BLACKLIST = "/etc/slackpkg/blacklist"

	// Command that will execute the SlackBuild script.
	BUILD_CMD = "/bin/bash --login "

	CONFIG_DIR       = "/etc/pkg"
	DEFAULT_PKG_FILE = CONFIG_DIR + "/pkg_list.txt"
	ENV_CMD          = "/usr/bin/env "
	DOC_DIR          = "/usr/doc"

	// Must be a string which, when appended by URLs, and executed by the
	// shell, will download the files to TMP_DIR. Possible alternative:
	// DOWNLOAD_CMD    = "/usr/bin/wget  --directory-prefix"
	DOWNLOAD_CMD = "/usr/bin/curl --remote-name-all --location --parallel --output-dir " + TMP_DIR

	// Pager to be used if $PAGER is not set. Used to display README and
	// SlackBuild files.
	FALLBACK_PAGER = "/usr/bin/less"

	// This command must clean the repository of any files that are not
	// in the upstream repository. Adjust if SYNC_CMD is changed.
	CLEAN_CMD = "/usr/bin/git -C " + REPO_PATH + " clean -fdx"

	// File containing a list of packages (one per line, comments "#" allowed),
	// which will be skipped during the upgrade process, can be disabled with
	// command line flag.
	GREYLIST = CONFIG_DIR + "/greylist"

	INSTALL_CMD    = "/sbin/upgradepkg --install-new --reinstall "
	MIRROR         = "https://github.com/SlackBuildsOrg/slackbuilds.git"
	MIRROR_CURRENT = "https://github.com/ponce/slackbuilds.git"

	// Maximum number of package builds to run at once. Can be overridden with
	// command line flag.
	NUM_DEFAULT_JOBS = 1

	TMP_DIR = "/tmp/pkg"

	// Will be copied to BLACKLIST, before BLACKLIST gets updated.
	OTHER_BLACKLIST = CONFIG_DIR + "/blacklist"

	OUTPUT_WIDTH = 80

	// Extension for prebuild hooks. If a file with `package_name`.PREBUILD_EXT
	// exists in CONFIG_DIR, it will be executed before the package is built.
	// The first argument will be the path to the package directory.
	PREBUILD_EXT = ".prebuild"

	// Command that must clone the repository to REPO_PATH. On stable releases,
	// {VERSION} and {MIRROR} will be replaced by the second field of
	// SLACKWARE_VERSION_FILE and the content of MIRROR, respectively. On the
	// current branch, {VERSION} will be replaced by "current" and {MIRROR} will
	// be replaced by MIRROR_CURRENT.
	PULL_CMD = "/usr/bin/git clone --depth 1 --no-tags --branch {VERSION} -- {MIRROR} " + REPO_PATH

	REPO_PATH              = "/var/lib/pkg/sbo"
	SLACKWARE_VERSION_FILE = "/etc/slackware-version"

	// Must be a string which, when executed by the shell, will update the
	// repository.
	SYNC_CMD = "/usr/bin/git -C " + REPO_PATH + " pull"

	// Flags for command line arguments.
	FILTERINSTALLED_FLAG = "i" // Don't filter installed packages
	FILE_FLAG            = "f" // Read package names from file
	JOBS_FLAG            = "j" // Maximum number of package builds to run at once
	LIST_INSTALLED_FLAG  = "l" // List installed packages
	MD5_FLAG             = "m" // Skip MD5Sum checks
	NO_DEPS_FLAG         = "n" // Don't install dependencies
	ROOT_FLAG            = "r" // Display dependents within the dependency graph
	RESPECTGREYLIST_FLAG = "g" // Ignore the greylist
	SEARCH_FLAG          = "s" // Search for a package
	TRACK_FLAG           = "t" // Track the given packages (show all dependencies)
	UPDATE_FLAG          = "u" // Update the repository (before doing anything else)
	VERSION_FLAG         = "v" // Display the version of pkg
	VIEW_FLAG            = "d" // Display information for the given packages
	VIEWBUILD_FLAG       = "b" // Display build scripts for the given packages
	YES_FLAG             = "y" // Skip all prompts
)

// DefaultRunArgs populates RunArgs with the default configuration constants.
func DefaultRunArgs() RunArgs {
	return RunArgs{
		Streams: StdStreams(), // Standard streams
		Argv:    os.Args,      // Command line arguments

		Config: Config{
			REPO_PATH, DOC_DIR, TMP_DIR, CONFIG_DIR, // Directory paths

			// Constant filepaths
			GREYLIST, BLACKLIST, OTHER_BLACKLIST, DEFAULT_PKG_FILE, SLACKWARE_VERSION_FILE,

			// Commands
			PULL_CMD, SYNC_CMD, CLEAN_CMD, DOWNLOAD_CMD, INSTALL_CMD, FALLBACK_PAGER,
		},
	}
}

// Streams groups the input, output, and error streams used by Run and
// RunWithUI.
type Streams struct {
	In  io.Reader
	Out io.Writer
	Err io.Writer
}

// StdStreams returns Streams wired to os.Stdin, os.Stdout, and os.Stderr.
func StdStreams() Streams {
	return Streams{In: os.Stdin, Out: os.Stdout, Err: os.Stderr}
}

// Config groups the quasi-constant paths and commands used by Run. Check the
// documentation of the similarly named constants for more information about
// each field.
type Config struct {
	RepoPath, DocDir, TmpDir, ConfigDir,
	Greylist, Blacklist, OtherBlacklist, DefaultPkgFile, SlackwareVersionFile,
	PullCmd, SyncCmd, CleanCmd, DownloadCmd, InstallCmd,
	FallbackPager string
}
