/**
 * Reads a text file or standard input, and extracts any urls in the text.
 * Any urls found will be copied to the clipboard.  Uses some support code
 * for clipboard use from the DevCD, which is included.  Written in mostly
 * ANSI C, with the only exceptions being the AmigaOS clipboard calls.
 *
 * This code is copyright (c) 1998 to Matthew Hunter.  It's my code.  Don't
 * sell it, don't claim you wrote it.  You can distribute it on PD collections
 * such as Aminet CDs and charge a reasonable cost for those.  This license
 * has nothing to do with the source for the support code from the DevCD, see
 * cb.h for that.
 *
 * If you change this code, it would be nice of you to send me your changes.
 *
 * Assume I included the usual legal mummery: I am not responsible if you
 * die while using this code, or even if your computer dies.  It works for me.
 * If it doesn't work for you, blame the phases of the moon or the configurations
 * of the stars, but don't blame me.
 **/

#include <exec/types.h>
#include <exec/ports.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <devices/clipboard.h>
#include <libraries/dosextens.h>
#include <libraries/dos.h>

#include "cb.h"

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


const static char VersTag[] = "\0$VER: URLScan 1.0 (24.4.1998)";

void striplf(char *line)
{
   for (; *line != NULL; *line++)
   {
      if ((*line == 10) || (*line == 13))
      {
         *line = NULL;
         return;
      }
   }
}

void cleanurl(char *line)
{
   int count;
   for (count = 0; line[count] != NULL; count++)
   {
      if (line[count] == ' ')
      {
         if (line[count-1] == '.') line[count - 1] = NULL;
         else line[count] = NULL;
         return;
      }
      if ((line[count] == ',') || (line[count] == '>') || (line[count] == '"'))
      {
         line[count] = NULL;
         return;
      }
   }
}

void main(int argc, char *argv[])
{
   char buffer[256] = {""};
   char *temp;
   FILE *input = NULL;
   ULONG count = 0;

   if (argc == 1)
   {
      // stdio processing
      input = stdin;
   }
   else if (argc == 2)
   {
      // File-based processing
      if ((input = fopen(argv[1], "r")) == NULL)
      {
         printf("Couldn't open input file: %s", argv[1]);
         exit(0);
      }
   }
   else exit(0);

   while (fgets(buffer, 256, input) != NULL)
   {
      if ((temp = strstr(buffer, "http://")) != NULL)
      {
         striplf(temp);
         cleanurl(temp);
         strcpy(buffer, temp);
      }
      else buffer[0] = NULL;

      if (buffer[0] != NULL)
      {
         struct IOClipReq *Clip;

         if ((Clip = CBOpen(0)) != NULL)
         {
            if (CBWriteFTXT(Clip, buffer) == TRUE)
            {
               printf("Unit %d: %s\n", count, buffer);
               count = count + 1;
            } else printf("Couldn't add to clipboard.");
         } else printf("Couldn't open clipboard device");
         CBClose(Clip);

         /**
          * This delay needed so clipboard-history programs have a chance to copy
          * information between different clipboard units.
          **/
         Delay(3);
      }
   }

   if (argc == 2)
   {
      fclose(input);
   }
}
