' This program reads all HTML files placed in its directory, and
' outputs them to a Netscape bookmark.htm file in the same directory.
' Please see CONFIG variables below.

' DIRECTIONS: Dump a bunch of "5000 Links I Like" files into the same directory where you
' run this program. Run it. Take the bookmark.htm file it spits out, and copy it over your 
' existing Netscape bookmark file. Now tell that macro program of yours to click on all of 
' your bookmarks!
' To get more *fresh* bookmarks at anytime, just dump in new *.htm files and run it again!


DECLARE FUNCTION dupe! (link$)
COMMON SHARED dupeSize, dupepointer


CLS




'-------- CONFIG variables --------

maxFolders = 25     ' This many bookmark folders
maxBookmarks = 25   ' This many bookmarks in each folder

verify = 1          ' Set this to 1 to verify servers are responding, 0 to skip verification (requires c:\windows\ping.exe)
dupeSize = 100      ' Check for duplicates in the last N links. Large numbers may give you memory errors!


'Now, don't change anything below this line unless you know what you're doing :)


DIM SHARED dupearray$(dupeSize)

'**************
10

'************* Get file listing **********
trace$ = "MSDOSlistfiles"
s$ = "dir " + dir$ + " > " + dir$ + "dir.tmp"
SHELL s$

'-------
readDirectory:

trace$ = "readdirectory"
tmpFile$ = dir$ + "dir.tmp"
OPEN tmpFile$ FOR INPUT AS #2

'-----

OPEN "bookmark.htm" FOR OUTPUT AS #3
PRINT "Writing bookmark header..."

PRINT #3, "<!DOCTYPE NETSCAPE-Bookmark-file-1>"
PRINT #3, "<!-- This file autogenerated by Bookmark (0.9). Do Not Edit unless you know what you're doing! --> <TITLE>Bookmarks for Unsupervised Surfing</TITLE><H1>Lets make some free money :)</H1>"
PRINT #3,

'begin initial Folder...
numFolders = numFolders + 1
PRINT #3, "<DL><p>" ' begin document
GOSUB newfolder:

'-----

Nextfile:

IF EOF(2) THEN GOTO done:
LINE INPUT #2, f$
length = 0
IF MID$(f$, 9, 1) = " " AND MID$(f$, 13, 1) = " " AND MID$(f$, 10, 3) <> "   " THEN
  DO
  length = length + 1
  fil$ = f$
  LOOP UNTIL MID$(fil$, length, 1) = CHR$(32) OR length > 8  'get filename (w/o extension)
  fil$ = LEFT$(fil$, length - 1)
  fil$ = filpath$ + fil$ + "." + MID$(f$, 10, 3)  'add extension
  'CLS
  IF MID$(f$, 10, 3) = "HTM" THEN GOSUB extract:

END IF
GOTO Nextfile:

GOTO done:

'------
extract:
pointer = 1: pointtemp = 0
snag$ = "<a href=$": snagtemp$ = " "

OPEN fil$ FOR BINARY AS #1

WHILE pointer <= LOF(2)
  'LOCATE 1, 1: PRINT pointer
  GET #1, pointer, snag$
  IF UCASE$(LEFT$(snag$, 8)) = "<A HREF=" THEN
    pointer = pointer + 9
    DO:
      GET #1, pointer + pointtemp, snagtemp$
      pointtemp = pointtemp + 1
      'PRINT snagtemp$;
    LOOP UNTIL snagtemp$ = CHR$(34)' (a quotation mark)
    'WEND
    link$ = SPACE$(pointtemp - 1)
    GET #1, pointer, link$
    pointer = pointer + pointtemp
    pointtemp = 0
    IF LCASE$(LEFT$(link$, 7)) = "http://" THEN
      IF verify = 1 THEN GOSUB checkaddlink ELSE GOSUB addlink
    END IF
  END IF
  pointer = pointer + 1
WEND
CLOSE #1
RETURN

addlink:
IF INSTR(link$, "?") OR INSTR(link$, "&") OR INSTR(link$, ".cgi") OR INSTR(link$, ".exe") OR INSTR(link$, ".pl") THEN COLOR 14, 0: PRINT "Adlink "; link$; " rejected.": COLOR 7, 0: RETURN

IF dupe(link$) THEN
  COLOR 14, 0: PRINT "Duplicate link "; link$; " rejected.": COLOR 7, 0

ELSE
  PRINT #3, "<DT><A HREF=" + CHR$(34) + link$ + CHR$(34) + ">" + link$ + "</a>"
  lpf = lpf + 1: numLinks = numLinks + 1
  PRINT "   " + link$

  IF lpf = maxBookmarks AND numFolders = maxFolders THEN GOTO enddoc:
  IF lpf = maxBookmarks THEN GOSUB endfolder: GOSUB newfolder:

END IF
RETURN

checkaddlink:

linkend = INSTR(8, link$, "/")
IF linkend = 0 THEN server$ = LEFT$(link$, LEN(link$) - 8) ELSE server$ = MID$(link$, 8, linkend - 8)
'PRINT linkend, server$
cmd$ = "c:\windows\ping.exe -n 1 -w 1000 " + server$ + " > bm_ping.tmp"
'PRINT cmd$
SHELL cmd$

'DO: LOOP UNTIL INKEY$ <> ""

linkgood = 0
OPEN "bm_ping.tmp" FOR INPUT AS #4
WHILE NOT EOF(4)
  LINE INPUT #4, result$
  IF INSTR(result$, "Bad IP") OR INSTR(result$, "unreachable") OR INSTR(result$, "timed out") THEN
    linkgood = 0
    COLOR 4, 0: PRINT "   Server " + server$ + " unreachable, link rejected.": COLOR 7, 0
  END IF
  IF LEFT$(result$, 5) = "Reply" AND INSTR(result$, "unreachable") = 0 THEN GOSUB addlink:
  'GOSUB addlink:
WEND
CLOSE #4
RETURN


newfolder:
PRINT "Adding folder: Folder" + STR$(numFolders)

PRINT #3, "<DT><H3>Folder " + STR$(numFolders) + "</H3>"
PRINT #3, "<DD>"
PRINT #3, "<DL><p>"
lpf = 0
numFolders = numFolders + 1
RETURN

endfolder:
PRINT #3, "</DL><p>"
RETURN


enddoc:
PRINT #3, "</DL></p>"
CLOSE #3

done:

PRINT
PRINT numLinks; "bookmarks arranged into"; numFolders - 1; "folders."


END

FUNCTION dupe (link$)
FOR i = 1 TO dupeSize
  'IF dupearray$(i) = "" THEN dupe = 0: GOTO nodupe
  IF link$ = dupearray$(i) THEN dupe = 1: EXIT FUNCTION
NEXT

nodupe:
IF link$ <> "" THEN dupearray$(dupepointer) = link$
dupepointer = dupepointer + 1
IF dupepointer > dupeSize THEN dupepointer = 1

dupe = 0
END FUNCTION

