/*      Get and Resume Elite EDition source code


Get and Resume Elite EDition (GREED)
Copyright (C) 1999  Anoakie Turner

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., 675 Mass Ave, Cambridge, MA 02139, USA.

For more information on the GPL, please go to:
http://www.gnu.org/copyleft/gpl.html


        Contact:  Anoakie Turner
                  Anoakie.Turner@asu.edu

                  13240 N. 94th Pl.
                  Scottsdale, AZ 85260
*/

#include "main.h"
/* New code +len, patch+ */
#include <pwd.h>
#include <sys/types.h>
/* End */


void Proxy(int NEWRC)
{
/*	char *file; */	/* Patched by len. */
	char *rcfile;	/* Patched by len. */
	char *buffer;
/*	char *home; */	/* Patched by len. */
	char *homerc;	/* Patched by len. */
	struct stat st;
#ifndef __riscos__
	struct passwd *pw_ent;	/* Patched by len. */
#endif /* !__riscos__ */
	char *temp = NULL;
	FILE *tempfp;
	char *http_proxy = getenv("HTTP_PROXY");
	int rc_exists = 0;
	int filesize = 0;

#ifndef __riscos__
/* New code added */
	/* Don't trust user's HOME environment variable. Look up by uid. */
	pw_ent = getpwuid(getuid());
	if(pw_ent == 0)
	{
#ifdef USE_GTK
			message_box("Fatal error", "Fatal: cannot lookup user data!\n"
				"Blame Anaokie, not me! :-)"); /*by bbg:)*/
#else
			fprintf(stderr, "greed: fatal: cannot lookup user data.\n");
#endif
	        exit(-1);
	}
	homerc = malloc(strlen(pw_ent->pw_dir) + strlen("/.greedrc") + 1);
	if(homerc == 0)
	{
#ifndef USE_GTK
		printf("greed: Home directory not found, "
 			"using current directory.\n");
#endif
		homerc = ".greedrc";
 	}
 	else
	{       strcpy(homerc, pw_ent->pw_dir);
	        strcat(homerc, "/.greedrc");
	}
#else /* __riscos__ */
	homerc = "<Choices$Write>.greedrc";
#endif /* __riscos__ */
	rcfile = homerc;
/* End new code */

/* Patched by len.
	home = malloc(strlen(getenv("HOME")) + strlen("/.greedrc") + 1);
	strcpy(home, getenv("HOME"));

	if (home == NULL)
	{	printf("Non fatal error: Home directory not found, "
			"using current directory.\n");
		file = home = ".greedrc";
	}
	else
		file = strcat(home, "/.greedrc");
*/

/* .8+ STABLE patch */
/*	filesize = 1; /**/	/* Patched by len */

/* Add new code */
	rc_exists = 1;
	if (stat(homerc, &st) != 0)
	{	if (stat("/etc/greedrc", &st) != 0)
/*			filesize = 0; */	/* Patched by len */
			rc_exists = 0;		/* Patched by len */
		else
/*			file = "/etc/greedrc"; */	/* Patched by len */
			rcfile = "/etc/greedrc";	/* Patched by len */
	}


/* Add new code */
	if (NEWRC == 0)
	{	if (http_proxy == NULL)
			http_proxy = getenv("http_proxy");
		if (http_proxy != NULL)
		{	temp = malloc(strlen(http_proxy));	/* DOCUMENTED MEMORY LEAK! :) */
			strcpy(temp, http_proxy);
			PROXY = temp + 7;
			if (strchr(temp, ':') == NULL)
				PROXYPORT = "8080";
			else
			{	PROXYPORT = strchr(temp, ':') + 1;
				PROXY[strchr(PROXY, ':') - PROXY] = '\0';
			}
		} else
		{	PROXY = malloc(1);
			PROXY[0] = '\0';
		}

		DEFAULT_DIR = NULL;
	}
/* else */
	if (NEWRC)
	{	tempfp = fopen(rcfile, "w+b");
/* End new code */

/* Patched by len
	if (filesize == 0)
	{	tempfp = fopen(file, "w+b");
*/
		CreateNewRC(tempfp);
		printf("Configuration file %s created!\n\nRestart greed for the new configuration"
#ifdef USE_GTK
		message_box("Done", "Configuration file created.\n"
			"Restart greed for the new configuration");
#else
/*			" to take effect!\n", file); */	/* Patched by len */
			" to take effect!\n", rcfile);	/* Patched by len */
		fclose (tempfp);
#endif
		exit(EXIT_SUCCESS);
	} else if (rc_exists) /*!NEWRC) */
/*	{	tempfp = fopen(file, "r+b"); */	/* Patched by len */
	{	tempfp = fopen(rcfile, "r+b");	/* Patched by len */

		fseek(tempfp, 0L, SEEK_END);
		filesize = ftell(tempfp);

		buffer = malloc(filesize+1);
		rewind(tempfp);
		fread(buffer, filesize, 1, tempfp);
		buffer[filesize+1] = '\0';


		PROXY = ReadString2(strstr(buffer, "\nSERVER=") + 8, '\n');
		PROXYPORT = ReadString2(strstr(buffer, "\nPORT=") + 6, '\n');

		if (PROXY == NULL)
		{	if (http_proxy == NULL)
				http_proxy = getenv("http_proxy");
			if (http_proxy != NULL)
			{	strcpy(temp, http_proxy);
				PROXY = temp + 7;

				if (strchr(temp, ':') == NULL)
					PROXYPORT = "8080";
				else
				{	PROXYPORT = strchr(temp, ':') + 1;
					PROXY[strchr(PROXY, ':') - PROXY] = '\0';
				}
			} else
			{	PROXY = malloc(1);
				PROXY[0] = '\0';
			}

		}

		if (PROXY[0] != 0 && OUTPUT_LEVEL > 5)
			printf("Using proxy at %s on port %s.\n", PROXY, PROXYPORT);
		ROLLBACK = atol(ReadString2(strstr(buffer, "\nROLLBACK=") + 10, '\n'));
		TIMEOUT_SEC = atoi(ReadString2(strstr(buffer, "\nTIMEOUT=") + 9, '\n'));
		MINSPEED = atoi(ReadString2(strstr(buffer, "\nMINSPEED=") + 10, '\n'));
		WAIT_RETRY = atoi(ReadString2(strstr(buffer, "\nWAIT_RETRY=") + 12, '\n'));
		FTP_WAIT_TIME = atoi(ReadString2(strstr(buffer, "\nSPEEDUP_FTP=") + 13, '\n'));
		if (strstr(buffer, "\nDEFAULT_DIR") != NULL)
		{	DEFAULT_DIR = ReadString2(strstr(buffer, "\nDEFAULT_DIR=\"") + 14, '"');
			if (DEFAULT_DIR != NULL)
				chdir(DEFAULT_DIR);
		}

		if (tempfp)
			fclose (tempfp);
	}

}

void CreateNewRC(FILE* fp)
{	fwrite("##\n",3,1,fp);
#ifndef __riscos__
	fwrite("## This is the default .greedrc for greed.\n",43,1,fp);
	fwrite("## Edit this file how you like, then rename it to .greedrc\n",59,1,fp);
	fwrite("##  and drop it in your home directory (or your /etc/greedrc if you want a global config)\n",90,1,fp);
#else /* __riscos__ */
	fwrite("## This is the default greedrc for greed.\n",42,1,fp);
#endif /* __riscos__ */
	fwrite("##\n",3,1,fp);
	fwrite("## ",3,1,fp);
	fwrite(CURRENT_VERSION,strlen(CURRENT_VERSION),1,fp);
	fwrite(" - By: Anoakie Turner\n",22,1,fp);
	fwrite("##\n\n",4,1,fp);
	fwrite("# Proxy server's IP (leave empty if none)\nSERVER=\n\n# Proxy server's PORT\nPORT=8080\n\n",84,1,fp);
	fwrite("# Amount of bytes to download at a time (MUST BE > 1024)\nROLLBACK=4096\n\n# Time out (in ",87,1,fp);
	fwrite("seconds)\nTIMEOUT=300\n\n# Don't download faster then X k/sec (0 for no limit)\nMINSPEED=0",86,1,fp);
	fwrite("\n\n# Time to wait before retrying (in seconds)\nWAIT_RETRY=10\n\n",61,1,fp);
	fwrite("# Speeds up FTP connections by waiting X seconds to send data, a higher number means:\n",86,1,fp);
	fwrite("# higher compatibility, lower speed. (default is 0)\nSPEEDUP_FTP=0\n\n",67,1,fp);
	fwrite("# Sets the default download directory (\"\" = current dir)\n# DO NOT SET IF YOU ARE USING ",87,1,fp);
	fwrite("THIS AS A GLOBAL (/etc/greedrc) GREED CONFIGURATION FILE!\nDEFAULT_DIR=\"\"\n\n",74,1,fp);
	/*      123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890   */
}
