/*    
 *     destroydb -- destroy a postgres database
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>

#ifndef sprite
#include <sys/signal.h>
#endif /* !sprite */

#include "tmp/c.h"
#include "daemon.h"
#include "strings.h"
#include "tmp/libpq.h"
#include "storage/fd.h"
#include "installinfo.h"

RcsId("$Header: RCS/destroydb.c,v 1.9 90/10/24 16:20:25 kemnitz Exp $");

extern char     *getenv();

extern char     *PQhost;     /* machine on which the backend is running */
extern char     *PQport;     /* comm. port with the postgres backend. */

extern char *optarg;
extern int optind,opterr;

main(argc,argv)
     int argc;
     char **argv;
{
    char *dbname;
    char *pstring;
    char *dir;
    FILE *file;
    char buf[100];
    int  errflag = 0;
    char c;
    char *pghomes[NDISKS];
    char *p, *p1;
    int  nstriping;
    int  i;
    int  handle_striping = 0, len;

    /* ----------------
     *    get the path to the directory containing the database
     * ----------------
     */
    dir = getenv("POSTGRESHOME");
    if (dir == (char *) NULL)
    dir = getenv("POSTHOME");
    if (dir == (char *) NULL)
    dir = DATAHOME;
    p = dir;

    len = strlen(p);
    for (i = 0; i < len && !handle_striping; i++)
    {
        if (p[i] == ':') handle_striping = 1;
    }

    if (handle_striping)
    {
        nstriping = 0;
        while ((p1=index(p, ':')) != NULL) {
            pghomes[nstriping++] = p;
            *p1 = '\0';
            p = p1 + 1;
        }
    }

    /* ----------------
     *    process command line options
     * ----------------
     */
    while ((c = getopt(argc, argv, "p:h:")) != EOF) {
    switch(c) {
        case 'h':
            PQhost = optarg;
            break;        
        case 'p':
            PQport = optarg;
            break;
        case '?' :
            errflag++;
        }
    }

    if (errflag) {
        fprintf(stderr, "usage: %s [-p port] [-h host] dbname\n", *argv);
        exit (1);
    }

    /* ----------------
     *    get the name of the database to use
     * ----------------
     */
    if ((dbname = argv[optind]) == NULL) {
    /* find default database */
    /* defaults database name to username. */
    if ((dbname = getenv("DATABASE")) == NULL)  
        dbname = getenv("USER");
    }

    /* get to the database directory */
    sprintf(buf, "%s/data/base/%s", dir, dbname);
    if (chdir(buf) < 0) {
        perror(buf);
        exit (1);
    }

    /* stop the vacuum daemon, if one is running */
    sprintf(buf, "%s.vacuum", dbname);
    if ((file = fopen(buf, "r")) != (FILE *) NULL) {
        int pid;
        fscanf(file, "%d", &pid);
        if (kill(pid, SIGKILLDAEMON1) < 0) {
            perror("can't kill vacuum daemon");
            exit (1);
        }
        fclose(file);
        if (unlink(buf) < 0) {
            perror(buf);
            exit (1);
        }
    }

    /* open a connection to the backend */
    PQsetdb(dbname);

    /* remove the tuple from pg_database */
    sprintf(buf, "delete pg_database where pg_database.datname = \"%s\"",
            dbname);
    pstring = PQexec(&buf[0]);     

    if (*pstring == 'E') {
        fprintf(stdout, "%s\ndestroydb failed", ++pstring);
        exit(1);
    }

    /* shut down communications */
    PQfinish();   

    /*
     * now remove the directory
     * don't use "-f" option to rm so that errors can be detected.
     */
    if (handle_striping)
    {
        for (i=0; i<nstriping; i++) {
            sprintf(buf, "rm -r %s/data/base/%s", pghomes[i], dbname);
            if (system(buf))
            {
                fprintf(stderr, "%s: rm failed\n", argv[0]);
            }
        }
    }
    else
    {
        sprintf(buf, "rm -r %s/data/base/%s", dir, dbname);
        if (system(buf))
        {
            fprintf(stderr, "%s: rm failed\n", argv[0]);
        }
    }
    exit(0);
}
