/*
 * vcontrol.c --
 *    POSTGRES vacuumd control utility program
 *
 * Flags:
 *    -e    enable vacuum daemon on <dbname>
 *    -k  kill vacuum daemon on <dbname>
 *    -s  see if vacuum daemon is running on <dbname>
 *
 */

/*
* RcsId("$Header: RCS/vcontrol.c,v 1.5 90/09/25 17:24:19 kemnitz Exp $");
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <strings.h>

#include "support/master.h"    /* for DefaultVacuumDaemonPath */

#define OptionString    "eksh:p:"

/* XXX hacks below added to make this compile in the obj.dec tree */

main(argc, argv)
     int  argc;
     char *argv[];
{
     char *portString = NULL;
     char *hostString = NULL;
     char *dbname = NULL;
     char *datahome = NULL, *GetDataHome();
     char c;
     char *cmdname = NULL;
     char filebuf[128];
     struct stat stat_buf;

     int function = 0, vacuum_running = 0, errors = 0;
     extern char    *optarg;
     extern int        optind, opterr, getopt();

     while ((c = getopt(argc, argv, OptionString)) != EOF && !errors) {
         switch (c) {
         case 'e':
             function = 1;
             break;
         case 'k':
             function = 2;
             portString = optarg;
             break;
         case 's':
             function = 3;
             break;
         case 'h':    /* postmaster host */
             hostString = optarg;
             break;
         case 'p':
             portString = optarg;
             break;
         default:
             ++errors;
             break;
         }
     }

     datahome = GetDataHome();

     dbname = argv[optind];
     cmdname = argv[0];

     /*
	  * See if file exists.
	  */

     if (datahome == NULL)
     {
         fprintf(stderr, "%s: POSTGRESHOME not set.\n", cmdname);
	 }
	 else
	 {
		 sprintf(filebuf, "%s/data/base/%s", datahome, dbname);
		 if (stat(filebuf, &stat_buf) != -1)
		 {
			printf(stderr, "%s: database %s does not exist.", cmdname, dbname); 
		 }
	 }

     if (optind + 1 != argc || errors != 0) {
         printf("Usage: %s [-p port] [-h host] [-e | -k | -s] dbname\n",
            cmdname);
         exit(1);
     }

     /*
      * If the vacuum daemon is running, this function will return
      * a nonzero PID.  Otherwise, it will return a zero PID.
      */

     vacuum_running = DBNameGetVacuumDaemonProcessId(dbname) != 0;

     switch (function)
     {
        case 1: /* enable vacuumd */
             if (vacuum_running) {
                 fprintf(stderr, "%s: vacuumd already running on %s\n",
                         cmdname, dbname);
             } else {
                 /*
                  * Note:
                  *    NULL hostString means use libpq default.
                  */
                 InitVacuumDemon(hostString, dbname,
                     NULL, NULL, portString, DefaultVacuumDemonPath);
             }
             break;
        case 2: /* disable vacuumd */
             if (!vacuum_running) {
                 fprintf(stderr, "%s: no vacuumd running on %s\n",
                         cmdname, dbname);
             } else {
                 DBNameStopVacuumDaemon(dbname);
             }
             break;
        case 3:
             if (vacuum_running) {
                 fprintf(stderr, "vacuumd is running on %s.\n", dbname);
             } else {
                 fprintf(stderr, "vacuumd is not running on %s.\n", dbname);
             }
             break;
        default:
             fprintf(stderr, "Oops!  Bad state for function %d\n", function);
             break;
     }
} /*main()*/

/*
 * Useless functions which should never be called - they are here so links
 * would not fail.
 */

exitpg() 
{
    fprintf(stderr, "oops - exitpg was called.\n");
    exit(0);
}

char *
GetDataHome()
{
    char        *home;
    char	*p;
    extern char    *getenv();

    home = getenv("POSTGRESHOME");
    if (home == NULL) return NULL;
    p = index(home, ':');
    if (p != NULL)
       *p = '\0';
    return(home);
}
