/********************************************************************
 * lindner
 * 3.3
 * 1993/11/02 06:14:09
 * /home/mudhoney/GopherSrc/CVS/gopher+/object/url.c,v
 * Exp
 *
 * Paul Lindner, University of Minnesota CIS.
 *
 * Copyright 1991, 1992 by the Regents of the University of Minnesota
 * see the file "Copyright" in the distribution for conditions of use.
 *********************************************************************
 * MODULE: url.c
 * Simplified method of getting urls..
 *********************************************************************
 * Revision History:
 * url.c,v
 * Revision 3.3  1993/11/02  06:14:09  lindner
 * Add url html hack
 *
 *
 *********************************************************************/

#include "url.h"
#include "GSgopherobj.h"
#include "Malloc.h"
#include "String.h"

Url *
URLnew()
{
     Url *temp;

     temp = (Url *) malloc(sizeof(Url));
     temp->url = STRnew();
     
     return(temp);
}


void
URLdestroy(url)
  Url *url;
{
     STRdestroy(url->url);
     free(url);
}

/*
 * Make a default url from the current Gopher Object...
 */

void
URLfromGS(url, gs)
  Url       *url;
  GopherObj *gs;
{
     char u[256], *path, *cp;

     *u = '\0';
     path  = GSgetPath(gs);

     if (path == NULL)
	  return;

     /** This is a server specific hack for now..  **/

     if (strncmp(path, "ftp:", 4) == 0) {
	  strcpy(u, "ftp://");
	  
	  cp = path+4;

	  /*** Find the hostname of the ftp link... ***/
	  while (*cp != '\0' && *cp != '@') 
	       cp++;
	  if (*cp == '\0')
	       return;

	  strncat(u, path+4, cp - path - 4);

	  cp ++;

	  /*** The rest is the file/path ***/
	  if (*cp != '/')
	       strcat(u, "/");

	  strcat(u, cp);

     }
     else if (strncmp(path, "waissrc:", 8) == 0) {
	  ;
     }
     else {
	  sprintf(u, "gopher://%s:%d/%c%s", GSgetHost(gs), GSgetPort(gs),
		  GSgetType(gs), path);
     }

     URLset(url, u);
}


/*
 * Hack gopher directories into an HTML type...
 */

void
URLmakeHTML(url)
  Url *url;
{
     char *cp = URLget(url);
     char *host;

     if (cp == NULL) 
	  return;

     if (strncmp(cp, "gopher://", 9) != 0)
	  return;

     /** find the type character **/
     cp = strchr(cp+10, '/');
     
     if (cp ==NULL)
	  return;

     host = cp+10;
     
     /** Test link for current host **/
/*     if (strcasecmp(host, hostname) != 0) 
	  return;*/
     
     cp ++;
     /** cp is now pointed at the type character **/
     
     if (*cp == '1' && *(cp+1) == '1') {
	  /** It's a directory **/
	  *cp = 'h';
	  *(cp+1) = 'h';
     }
}



/*
 * Get the transport of the specified URL
 */

char *
URLgetTransport(url)
  Url *url;
{
     static char trans[16];  /** Shouldn't need more than that.. yet.. */
     char *urlcp, *cp;

     urlcp = URLget(url);
     if (urlcp == NULL)
	  return(NULL);

     cp = strchr(urlcp, ':');
     if (cp == NULL)
	  return(NULL);
     
     strncpy(trans, urlcp, (int)(cp-urlcp));
}
     
     
     


