/* ARISA - HELP! Don't Panic!
 * Copyright (C) 2003 Carl Ritson
 *
 * 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 <zlib.h>

#include "help.h"

static void admin_load_static_help(void) {
	unsigned long help_text_size;
	char **cmds,**lines,*help_text;
	int no_cmds,no_lines;
	int i,opos,pos = 0;
	menu_t *cmd;
	
	help_text_size = help_original_size+16;
	help_text = xalloc(help_text_size);
	
	if(uncompress(help_text,&help_text_size,help_data,help_size) != Z_OK) {
		xfree(help_text);
		LOGP(L_ERR,"Error decompressing help.");
		return;
	}
	
	help_text[help_text_size] = '\0';

	while(help_text[pos] != '\0') {
		cmds	= NULL; 
		no_cmds	= 0;
		lines	= NULL;
		no_lines= 0;

		while(help_text[pos] == '/') {
			opos = pos;
			while(help_text[pos] != '\0' && help_text[pos] != '\n')
				pos++;
			if(help_text[pos] != '\0')
				help_text[pos++] = '\0'; // note the increment
			PTRARR_ADD(&cmds,&no_cmds,&(help_text[opos]));
		}
		while(help_text[pos] != '/' && help_text[pos] != '\0') {
			opos = pos;
			while(help_text[pos] != '\0' && help_text[pos] != '\n')
				pos++;
			if(help_text[pos] != '\0')
				help_text[pos++] = '\0'; // note the increment
			PTRARR_ADD(&lines,&no_lines,&(help_text[opos]));
		}
		for(i = no_lines-1; i >= 0; --i) {
			if(strlen(lines[i]) == 0)
				PTRARR_DEL(&lines,&no_lines,lines[i]);
			else
				i = -1; // break loop
		}
		for(i = 0; i < no_cmds; ++i) {
			cmd = menu_resolv(cmds[i]);
			if(cmd != NULL) {
				LOCK(cmd);
				if(cmd->help != NULL)
					strarr_free(&cmd->help,&cmd->no_help);
				cmd->help = strarr_dup(lines,no_lines,
						&cmd->no_help);
				UNLOCK(cmd);
				menu_deref(cmd);
			} else
				D("Unable register help for %s",cmds[i]);
		}
		if(cmds != NULL)
			xfree(cmds);
		if(lines != NULL)
			xfree(lines);
	}

	xfree(help_text);
}
