/*
 * Copyright (c)2004 The DragonFly Project.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *   Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 *   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.
 * 
 *   Neither the name of the DragonFly Project nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
 * COPYRIGHT HOLDERS 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. 
 */

/*
 * package.c
 * Manage installation etc of packages.
 * $Id: package.c,v 1.3 2004/07/11 16:45:52 cpressey Exp $
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dfui/dfui.h>
#include <dfui/fspred.h>

#include "diskutil.h"
#include "uiutil.h"
#include "commands.h"
#include "pathnames.h"

extern struct dfui_connection *c;
extern char dfly_root[];

/*
 * Determine whether a package is installed on a DragonFly HDD.
 */
int
pkg_exists(char *pkg)
{
	return(is_dir("%smnt/var/db/pkg/%s", dfly_root, pkg));
}

int
pkg_clean(struct commands *cmds)
{
	command_add(cmds, "chroot %smnt/ /usr/sbin/pkg_delete '*'", dfly_root);
	return(1);
}

/*
 * Copy a package from an installation CD onto a DragonFly HDD.
 */
int
pkg_copy(struct commands *cmds, char *pkg_name)
{
	FILE *pipe;
	char *command, *rpkg_name;
	char line[256];

	/*
	 * Get all the packages that this package depends on, and
	 * recursively pkg_copy them first, if they're not already there.
	 */
	asprintf(&command,
	    "pkg_info -r %s | egrep '^Dependency:'", pkg_name);
	pipe = popen(command, "r");
	free(command);
	if (pipe == NULL)
		return(0);

	while (fgets(line, 255, pipe) != NULL) {
		rpkg_name = &line[12];
		if (!pkg_exists(rpkg_name)) {
			if (!pkg_copy(cmds, rpkg_name)) {
				pclose(pipe);
				return(0);
			}
		}
	}
	pclose(pipe);

	if (!pkg_exists(pkg_name)) {
		command_add(cmds, "pkg_create -b %s %smnt/tmp/%s.tgz",
		    pkg_name, dfly_root, pkg_name);
		command_add(cmds, "chroot %smnt/ /usr/sbin/pkg_add /tmp/%s.tgz",
		    dfly_root, pkg_name);
		command_add(cmds, "rm %smnt/tmp/%s.tgz", dfly_root, pkg_name);
	}

	return(1);
}

/*
 * Remove a package from a DragonFly HDD.
 */
int
pkg_remove(struct commands *cmds, char *pkg_name)
{
	FILE *pipe;
	char *command, *rpkg_name;
	char line[256];

	/*
	 * Get all the packages that this package depends on, and
	 * recursively pkg_copy them first, if they're not already there.
	 */
	asprintf(&command,
	    "chroot %smnt/ /usr/sbin/pkg_info -r %s | egrep '^Dependency:'",
	    dfly_root, pkg_name);
	pipe = popen(command, "r");
	free(command);
	if (pipe == NULL)
		return(0);

	while (fgets(line, 255, pipe) != NULL) {
		rpkg_name = &line[12];
		if (!pkg_exists(rpkg_name)) {
			if (!pkg_remove(cmds, rpkg_name)) {
				pclose(pipe);
				return(0);
			}
		}
	}
	pclose(pipe);

	if (pkg_exists(pkg_name)) {
		command_add(cmds, "chroot %smnt/ /usr/sbin/pkg_delete %s",
		    dfly_root, pkg_name);
	}

	return(1);
}
