#!/usr/local/bin/icmake -qi

/***************************************************************************

    This file shows an example of a shell around ftp. I use this program a 
lot in situations where I want to transfer a file from one unix host to 
another, and when I know beforehand what file from what directory I want to 
transfer. For the installation:
	- copy this file to your personal bin directory
	- do a chmod +x of the file
    The program will prompt for the user name and password to use for the 
ftp transfer. If you often access one host with this program, and don't wont 
to type the user/password all the time, you can do the follwing for bash:
	> set FTPUSER my_login_name_on_that_host
	> export FTPUSER
	> set FTPASS my_password_on_that_host
	> export FTPASS
For tcsh, try:
	> setenv FTPUSER my_login_name_on_that_host
	> setenv FTPASS my_password_on_that_host
Net result: this program won't prompt you for the strings, but will retrieve 
them from the environment table. 

*****************************************************************************/

// here's a couple of defines, no need to change them..
#define VER "1.00"
#define YEARS "1993"
#define TMPFILE "/tmp/ftpxfer.tmp"

list
    envp;					// environment strings

string
    host,					// host to transfer from/to
    dir,					// foreign directory
    file,					// local file
    direction;					// "get" or "put" file?
    
string getenv (string varname)    		// purpose: returns setting
{						// of environment variable
    int						// 'varname'
    	i;
    	
    for (i = 0; i < sizeof (envp); i += 2)	// loop thru envp...
    	if (element (i, envp) == varname)	// found varname?
    	    return (element (i + 1, envp));	// yes -- return setting
    	    
    return ("");				// no -- return empty string
}

void inittmp ()					// purpose: initialize temp
{						// file

    if (exists (TMPFILE))			// remove any old version
    	exec ("rm", TMPFILE);			// if it exists
    	
    exec ("touch", TMPFILE);			// make empty file
    exec ("chmod", "600", TMPFILE);		// make it r/w only for user
}
    
void process ()					// purpose: do the actual 
{						// ftp transfer

    string
    	user,					// user name on foreign host
    	password,				// password
    	foreignfile;				// full name of foreign file
    	
    inittmp ();					// make new temp file
    	
    foreignfile = change_path (file, dir);	// foreign file name equals
    						// local file, except for 
    						// foreign directory
    						
    if (! (user = getenv ("FTPUSER")) )		// get username from envp
    {						// or prompt for it
    	printf ("User name: ");
    	user = gets ();
    }
    if (! (password = getenv ("FTPASS")) )	// get passwd from envp
    {						// or prompt for it
    	printf ("Password : ");
    	password = gets ();
    }
    
    fprintf (TMPFILE, 				// write ftp login procedure
    	"open ", host, "\n",			// to tmpfile, followed
    	"user ", user, " ", password, "\n",	// by transfer commands
    	"binary\n");
    if (dir == "get")
    	fprintf (TMPFILE, "get ", foreignfile, " ", file, "\n");
    else
    	fprintf (TMPFILE, "put ", file, " ", foreignfile, "\n");
    fprintf (TMPFILE, "quit\n");
    
    exec (P_NOCHECK, "ftp", 			// do the ftp transfer
        "-v -n -i", "< ", TMPFILE);
    
    exec (P_NOCHECK, "rm", TMPFILE);		// remove temp file
}    
    
void usage ()					// purpose: print usage info
{						// and die
    printf ("\n"
    	    "ICCE Ftp-based File Transfer Shell  V", VER, "\n"
    	    "Copyright (c) ICCE ", YEARS, ". All rights reserved.\n"
    	    "\n"
    	    "Usage: ftpxfer -p|-g host file [directory]\n"
    	    "where:\n"
    	    "       -p         : selects putting of file\n"
    	    "       -g         : selects getting of file\n"
    	    "       host       : host to put/get from/to\n"
    	    "       file       : file to transfer\n"
    	    "       directory  : optional directory at foreign host, if "
						    	    "not given:\n"
	    "                    current directory is used\n"
    	    "Ftpxfer will use the environment variables FTPUSER and FTPASS "
    	    						"when available,\n"
	    "or will prompt for the user and password.\n"
    	    "\n");
    exit (1);
}
    
void main (int argc, list argv, list evp)	// main function
{
    envp = evp;					// store environment
    echo (OFF);					// no re-echoing of commands
    
    if (element (1, argv) == "-p")		// first argument: must
    	direction = "put";			// be -p or -g
    else if (element (1, argv) == "-g")
    	direction = "get";
    else
    	usage ();
    	
    if (! (host = element (2, argv)) )		// second argument: must be
    	usage ();				// foreign host
    if (! (file = element (3, argv)) )		// third argument: must be
    	usage ();				// file to transfer
    if (direction == "put" && ! exists (file))	// if putting: file must
    {						// exist
    	printf ("File to put does not exist.\n");
    	exit (1);
    }    
    
    if (! (dir = element (4, argv)) )		// fourth element: may be 
    	dir = chdir (".");			// foreign directory
    
    process ();					// hit it!
    	
    exit (0);					// exitstatus: success
}
