/* upsrw - simple client for read/write variable access (formerly upsct2)

   Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "common.h"
#include "nut_platform.h"

#include <pwd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include "upsclient.h"

static char		*upsname = NULL, *hostname = NULL;
static UPSCONN_t	*ups = NULL;

struct list_t {
	char	*name;
	struct	list_t	*next;
};

static void usage(const char *prog)
{
	printf("Network UPS Tools %s %s\n\n", prog, UPS_VERSION);
	printf("usage: %s [-h]\n", prog);
	printf("       %s [-s <variable>] [-u <username>] [-p <password>] <ups>\n\n", prog);
	printf("Demo program to set variables within UPS hardware.\n");
	printf("\n");
	printf("  -h            display this help text\n");
	printf("  -s <variable>	specify variable to be changed\n");
	printf("		use -s VAR=VALUE to avoid prompting for value\n");
	printf("  -u <username> set username for command authentication\n");
	printf("  -p <password> set password for command authentication\n");
	printf("\n");
	printf("  <ups>         UPS identifier - <upsname>[@<hostname>[:<port>]]\n");
	printf("\n");
	printf("Call without -s to show all possible read/write variables.\n");
}

static void clean_exit(void)
{
	if (ups) {
		upscli_disconnect(ups);
	}

	free(upsname);
	free(hostname);
	free(ups);
}

static void do_set(const char *varname, const char *newval)
{
	char	buf[SMALLBUF], enc[SMALLBUF];

	snprintf(buf, sizeof(buf), "SET VAR %s %s \"%s\"\n", upsname, varname, pconf_encode(newval, enc, sizeof(enc)));

	if (upscli_sendline(ups, buf, strlen(buf)) < 0) {
		fatalx(EXIT_FAILURE, "Can't set variable: %s", upscli_strerror(ups));
	}

	if (upscli_readline(ups, buf, sizeof(buf)) < 0) {
		fatalx(EXIT_FAILURE, "Set variable failed: %s", upscli_strerror(ups));
	}

	/* FUTURE: status cookies will tie in here */
	if (strncmp(buf, "OK", 2) != 0) {
		fatalx(EXIT_FAILURE, "Unexpected response from upsd: %s", buf);
	}

	fprintf(stderr, "%s\n", buf);
}

static void do_setvar(const char *varname, char *uin, const char *pass)
{
	char	newval[SMALLBUF], temp[SMALLBUF], user[SMALLBUF], *ptr;
	struct passwd	*pw;

	if (uin) {
		snprintf(user, sizeof(user), "%s", uin);
	} else {
		memset(user, '\0', sizeof(user));

		pw = getpwuid(getuid());

		if (pw) {
			printf("Username (%s): ", pw->pw_name);
		} else {
			printf("Username: ");
		}

		if (fgets(user, sizeof(user), stdin) == NULL) {
			upsdebug_with_errno(LOG_INFO, "%s", __func__);
		}

		/* deal with that pesky newline */
		if (strlen(user) > 1) {
			user[strlen(user) - 1] = '\0';
		} else {
			if (!pw) {
				fatalx(EXIT_FAILURE, "No username available - even tried getpwuid");
			}

			snprintf(user, sizeof(user), "%s", pw->pw_name);
		}
	}

	/* leaks - use -p when running in valgrind */
	if (!pass) {
		pass = GETPASS("Password: " );

		if (!pass) {
			fatal_with_errno(EXIT_FAILURE, "getpass failed");
		}
	}

	/* Check if varname is in VAR=VALUE form */
	if ((ptr = strchr(varname, '=')) != NULL) {
		*ptr++ = 0;
		snprintf(newval, sizeof(newval), "%s", ptr);
	} else {
		printf("Enter new value for %s: ", varname);
		fflush(stdout);
		if (fgets(newval, sizeof(newval), stdin) == NULL) {
			upsdebug_with_errno(LOG_INFO, "%s", __func__);
		}
		newval[strlen(newval) - 1] = '\0';
	}

	snprintf(temp, sizeof(temp), "USERNAME %s\n", user);

	if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
		fatalx(EXIT_FAILURE, "Can't set username: %s", upscli_strerror(ups));
	}

	if (upscli_readline(ups, temp, sizeof(temp)) < 0) {

		if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
			fatalx(EXIT_FAILURE, "Set username failed due to an unknown command. You probably need to upgrade upsd.");
		}

		fatalx(EXIT_FAILURE, "Set username failed: %s", upscli_strerror(ups));
	}

	snprintf(temp, sizeof(temp), "PASSWORD %s\n", pass);

	if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
		fatalx(EXIT_FAILURE, "Can't set password: %s", upscli_strerror(ups));
	}

	if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
		fatalx(EXIT_FAILURE, "Set password failed: %s", upscli_strerror(ups));
	}

	/* no upsname means die */
	if (!upsname) {
		fatalx(EXIT_FAILURE, "Error: a UPS name must be specified (upsname[@hostname[:port]])");
	}

	/* old variable names are no longer supported */
	if (!strchr(varname, '.')) {
		fatalx(EXIT_FAILURE, "Error: old variable names are not supported");
	}

	do_set(varname, newval);
}

static const char *get_data(const char *type, const char *varname)
{
	int	ret;
	unsigned int	numq, numa;
	char	**answer;
	const char	*query[4];

	query[0] = type;
	query[1] = upsname;
	query[2] = varname;

	numq = 3;

	ret = upscli_get(ups, numq, query, &numa, &answer);

	if ((ret < 0) || (numa < numq)) {
		return NULL;
	}

	/* <type> <upsname> <varname> <desc> */
	return answer[3];
}

static void do_string(const char *varname, const int len)
{
	const char	*val;

	val = get_data("VAR", varname);

	if (!val) {
		fatalx(EXIT_FAILURE, "do_string: can't get current value of %s", varname);
	}

	printf("Type: STRING\n");
	printf("Maximum length: %d\n", len);
	printf("Value: %s\n", val);
}

static void do_enum(const char *varname)
{
	int	ret;
	unsigned int	numq, numa;
	char	**answer, buf[SMALLBUF];
	const char	*query[4], *val;

	/* get current value */
	val = get_data("VAR", varname);

	if (!val) {
		fatalx(EXIT_FAILURE, "do_enum: can't get current value of %s", varname);
	}

	snprintf(buf, sizeof(buf), "%s", val);

	query[0] = "ENUM";
	query[1] = upsname;
	query[2] = varname;
	numq = 3;

	ret = upscli_list_start(ups, numq, query);

	if (ret < 0) {
		fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
	}

	ret = upscli_list_next(ups, numq, query, &numa, &answer);

	printf("Type: ENUM\n");

	while (ret == 1) {

		/* ENUM <upsname> <varname> <value> */

		if (numa < 4) {
			fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
		}

		printf("Option: \"%s\"", answer[3]);

		if (!strcmp(answer[3], buf)) {
			printf(" SELECTED");
		}

		printf("\n");

		ret = upscli_list_next(ups, numq, query, &numa, &answer);
	}
}

static void do_range(const char *varname)
{
	int	ret;
	unsigned int	numq, numa;
	char	**answer;
	const char	*query[4], *val;
	int ival, min, max;

	/* get current value */
	val = get_data("VAR", varname);

	if (!val) {
		fatalx(EXIT_FAILURE, "do_range: can't get current value of %s", varname);
	}

	ival = atoi(val);

	query[0] = "RANGE";
	query[1] = upsname;
	query[2] = varname;
	numq = 3;

	ret = upscli_list_start(ups, numq, query);

	if (ret < 0) {
		fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
	}

	ret = upscli_list_next(ups, numq, query, &numa, &answer);

	printf("Type: RANGE\n");

	while (ret == 1) {

		/* RANGE <upsname> <varname> <min> <max> */

		if (numa < 5) {
			fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
		}

		min = atoi(answer[3]);
		max = atoi(answer[4]);

		printf("Option: \"%i-%i\"", min, max);

		if ((ival >= min) && (ival <= max)) {
			printf(" SELECTED");
		}

		printf("\n");

		ret = upscli_list_next(ups, numq, query, &numa, &answer);
	}
}

static void do_type(const char *varname)
{
	int	ret;
	unsigned int	i, numq, numa;
	char	**answer;
	const char	*query[4];

	query[0] = "TYPE";
	query[1] = upsname;
	query[2] = varname;
	numq = 3;

	ret = upscli_get(ups, numq, query, &numa, &answer);

	if ((ret < 0) || (numa < numq)) {
		printf("Unknown type\n");	
		return;
	}

	/* TYPE <upsname> <varname> <type>... */
	for (i = 3; i < numa; i++) {

		if (!strcasecmp(answer[i], "ENUM")) {
			do_enum(varname);
			return;
		}

		if (!strcasecmp(answer[i], "RANGE")) {
			do_range(varname);
			return;
		}

		if (!strncasecmp(answer[i], "STRING:", 7)) {

			char	*len = answer[i] + 7;
			int	length = strtol(len, NULL, 10);

			do_string(varname, length);
			return;

		}

		if (!strcasecmp(answer[i], "NUMBER")) {
			printf("Type: NUMBER\n");
			return;
		}

		/* ignore this one */
		if (!strcasecmp(answer[i], "RW")) {
			continue;
		}

		printf("Type: %s (unrecognized)\n", answer[i]);
	}
}

static void print_rw(const char *varname)
{
	const char	*tmp;

	printf("[%s]\n", varname);

	tmp = get_data("DESC", varname);

	if (tmp) {
		printf("%s\n", tmp);
	} else {
		printf("Description unavailable\n");
	}

	do_type(varname);

	printf("\n");
}

static void print_rwlist(void)
{
	int	ret;
	unsigned int	numq, numa;
	const char	*query[2];
	char	**answer;
	struct	list_t	*lhead, *llast, *ltmp, *lnext;

	/* the upsname is now required */
	if (!upsname) {
		fatalx(EXIT_FAILURE, "Error: a UPS name must be specified (upsname[@hostname[:port]])");
	}

	llast = lhead = NULL;

	query[0] = "RW";
	query[1] = upsname;
	numq = 2;

	ret = upscli_list_start(ups, numq, query);

	if (ret < 0) {

		/* old upsd --> fall back on old LISTRW technique */
		if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
			fatalx(EXIT_FAILURE, "Error: upsd is too old to support this query");
		}

		fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
	}

	ret = upscli_list_next(ups, numq, query, &numa, &answer);

	while (ret == 1) {

		/* RW <upsname> <varname> <value> */
		if (numa < 4) {
			fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
		}

		/* sock this entry away for later */

		ltmp = xmalloc(sizeof(struct list_t));
		ltmp->name = xstrdup(answer[2]);
		ltmp->next = NULL;

		if (llast) {
			llast->next = ltmp;
		} else {
			lhead = ltmp;
		}

		llast = ltmp;

		ret = upscli_list_next(ups, numq, query, &numa, &answer);
	}

	/* use the list to get descriptions and types */

	ltmp = lhead;

	while (ltmp) {
		lnext = ltmp->next;

		print_rw(ltmp->name);

		free(ltmp->name);
		free(ltmp);
		ltmp = lnext;
	}
}

int main(int argc, char **argv)
{
	int	i, port;
	const char	*prog = xbasename(argv[0]);
	char	*password = NULL, *username = NULL, *setvar = NULL;

	while ((i = getopt(argc, argv, "+hs:p:u:V")) != -1) {
		switch (i)
		{
		case 's':
			setvar = optarg;
			break;
		case 'p':
			password = optarg;
			break;
		case 'u':
			username = optarg;
			break;
		case 'V':
			printf("Network UPS Tools %s %s\n", prog, UPS_VERSION);
			exit(EXIT_SUCCESS);
		case 'h':
		default:
			usage(prog);
			exit(EXIT_SUCCESS);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc < 1) {
		usage(prog);
		exit(EXIT_SUCCESS);
	}

	/* be a good little client that cleans up after itself */
	atexit(clean_exit);

	if (upscli_splitname(argv[0], &upsname, &hostname, &port) != 0) {
		fatalx(EXIT_FAILURE, "Error: invalid UPS definition.  Required format: upsname[@hostname[:port]]");
	}

	ups = xcalloc(1, sizeof(*ups));

	if (upscli_connect(ups, hostname, port, 0) < 0) {
		fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
	}

	if (setvar) {
		/* setting a variable */
		do_setvar(setvar, username, password);
	} else {
		/* if not, get the list of supported read/write variables */
		print_rwlist();
	}

	exit(EXIT_SUCCESS);
}


/* Formal do_upsconf_args implementation to satisfy linker on AIX */
#if (defined NUT_PLATFORM_AIX)
void do_upsconf_args(char *upsname, char *var, char *val) {
        fatalx(EXIT_FAILURE, "INTERNAL ERROR: formal do_upsconf_args called");
}
#endif  /* end of #if (defined NUT_PLATFORM_AIX) */