/* $Id: main.c,v 1.43 2009/05/08 10:01:17 imil Exp $ */

/*
 * Copyright (c) 2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Emile "iMil" Heitor <imil@NetBSD.org> .
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>

#include "lib.h"
#include "pkg_dry.h"

static void	usage(void);
static void	split_repos(void);

int 		yesflag = 0;
char		*env_repos, **pkg_repos;

int
main(int argc, char *argv[])
{
	int 		ch;
	struct stat	sb;

	setprogname(argv[0]);

	if (argc < 2 || *argv[1] != '-')
		usage();

	/* for pkg_install */
	unsetenv("PKG_PATH");
	/* split PKG_REPOS env variable */
	split_repos();
	/* create base directories */
	if (stat(pkg_dry_cache, &sb) < 0)
		create_dirs();

	drydb_init();
	
	while ((ch = getopt(argc, argv, "hud:t:T:las:i:r:k:K:pymgGc")) != -1) {
		switch (ch) {
		case 'u':
			update_db(REMOTE_SUMMARY, NULL);
			break;
		case 'd':
			show_direct_depends(optarg);
			break;
		case 't':
			show_full_dep_tree(optarg,
				DIRECT_DEPS, "full dependency tree for %s\n");
			break;
		case 'T':
			show_full_dep_tree(optarg,
				LOCAL_REVERSE_DEPS,
				"local reverse dependency tree for %s\n");
			break;
		case 'l':
			list_pkgs(LOCAL_PKGS_QUERY);
			break;
		case 'a':
			list_pkgs(REMOTE_PKGS_QUERY);
			break;
		case 'i':
			pkg_dry_install(&argv[2]);
			break;
		case 'g':
			pkg_dry_upgrade(UPGRADE_KEEP);
			break;
		case 'G':
			pkg_dry_upgrade(UPGRADE_ALL);
			break;
		case 'r':
			pkg_dry_remove(&argv[2]);
			break;
		case 'm':
			pkg_dry_autoremove();
			break;
		case 'k':
			pkg_keep(KEEP, &argv[2]);
			break;
		case 'K':
			pkg_keep(UNKEEP, &argv[2]);
			break;
		case 'p':
			show_pkg_keep();
			break;
		case 's':
			search_pkg(optarg);
			break;
		case 'c':
			clean_cache();
			break;
		case 'y':
			yesflag = 1;
			break;
		case 'h':
			usage();
			/* NOTREACHED */
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	drydb_close();

	XFREE(env_repos);
	XFREE(pkg_repos);

	return EXIT_SUCCESS;
}

static void
usage()
{
	(void)fprintf(stderr,
		"usage: %s [-hlaugGmcy] [-sdtT package] [-irkK package ...]\n",
		getprogname());

	exit(EXIT_FAILURE);
}

static void
split_repos()
{
	int		repocount;
	char	*p;

	XSTRDUP(env_repos, getenv("PKG_REPOS"));

	if (env_repos == NULL)
		errx(EXIT_FAILURE,
			"PKG_REPOS must be set in order to run this program.");

	repocount = 2; /* 1st elm + NULL */

	XMALLOC(pkg_repos, repocount * sizeof(char *));
	*pkg_repos = env_repos;

	p = env_repos;

	while((p = strchr(p, ' ')) != NULL) {
		*p = '\0';
		p++;

		XREALLOC(pkg_repos, ++repocount * sizeof(char *));
		pkg_repos[repocount - 2] = p;
	}

	pkg_repos[repocount] = NULL;
}

void
cleanup(int i)
{
}

