/* program to check environment variables.

   This is mainly for use in shell scripts.
   The usages are:

	eqenv <variable>

      		This returns 0 if the environment variable <variable>
		exists, and 1 if it does not.

	eqenv <variable> <value>

		This returns 0 if the environment variable <variable>
		exists and is equal to <value>, and 1 otherwise

	The option -v causes the result to be printed as well.
*/

#include <stdio.h>

extern char *getenv();

main(argc, argv)
int argc;
char **argv;
{
	char *variable = NULL;
	char *value = NULL;
	char *check;
	int verbose = 0;
	int retval;

	argc--; argv++;
	while (argc)
	 { 
	   if (strcmp ("-v", *argv) == 0)
	    { verbose = 1;
	    }
	   else if (!variable)
	    { variable = *argv;
	    }
	   else if (!value)
	    { value = *argv;
	    }
	   else
	    { argc = -1;
	      break;
	    }
	   argv++; argc--;
	 }
	if (argc == -1)
	 { fprintf (stderr, "USAGE eqenv <variable> [<value>]\n");
	   exit (-1);
	 }

	check = getenv(variable);
	if (!value)
	 { if (check)
	    { retval = 0;
	    }
	   else
	    { retval = 1;
	    }
	 }
	else
	 { if (strcmp (check, value) == 0)
	    { retval = 0;
	    }
	   else
	    { retval = 1;
	    }
	 }
	if (verbose)
	 { printf ("%d\n", retval);
	 }
	exit (retval);
}

