--define globals
global completeMailboxList --list of all mailboxes to be imported or processed
global MacOSVersion --string for determining whether to system is Mac OS X or not
global pathToMailFolder --string for full path to Mail folder on Mac OS X
global pathToDesktop --string for path to Desktop folder on boot volume
global theOriginalMbox --mbox raw contents with ASCII 13 line endings
global theFinalMbox --mbox raw contents with ASCII 10 line endings
global importFolderName --string for name of import folder on Desktop
global importFolderLocation --string containing path to import folder on Desktop
global tempMboxFile --string for path to pre-processed mbox file
global mboxName --string for name of current mbox being processed
global mboxFile --string for path for post-processed mbox file
global pathToMailbox --string for path to mailbox in hierarchy
global emailClient --string for name of email client

--define variables
set completeMailboxList to {}
set AppleScript's text item delimiters to ""

--determine the version of Mac OS that is running
try
	get path to extensions
	set MacOSVersion to "Mac OS"
	tell application "Finder" to set pathToDesktop to ((desktop) as string)
on error
	display dialog "This script will only run when you have started up in Mac OS 9.x or earlier. Please use the Mac OS X scripts instead." buttons {"Quit"} default button 1
	return
end try

--set some defaults based on client name
set emailClient to "Netscape Communicator"
set importFolderName to "Netscape-Import"
activate
display dialog "To begin import of email from Netscape Communicator, you'll need to find the location where your email is stored." buttons {"Continue"} default button 1
activate
display dialog "In the next screen, you'll be asked to navigate to the folder named 'Mail'. Here is where it is typically located:" & return & return & ¬
	"System Folder" & return & ¬
	"       Preferences" & return & ¬
	"              Netscape Users" & return & ¬
	"                      " & return & ¬
	"                              Mail"
tell application "Finder"
	set pathToNetscapeMail to (choose folder with prompt "Navigate to your Netscape 'Mail' folder:")
	set netscapeFolderContents to every item of pathToNetscapeMail
	my createImportFolderOnDesktop()
	set importFolderLocation to alias (pathToDesktop & importFolderName & ":")
	duplicate (netscapeFolderContents) to importFolderLocation
	my traverseNetscapeFolders(importFolderLocation)
	my moveMailToFinalDestination()
	set AppleScript's text item delimiters to ""
end tell

--routines

on convertASCII()
	set theOriginalMbox to (open for access file tempMboxFile)
	set theFinalMbox to (open for access file mboxFile with write permission)
	try
		repeat
			set eachLine to (read theOriginalMbox before return)
			write eachLine & (ASCII character 10) to theFinalMbox starting at eof
		end repeat
	end try
	try
		close access theOriginalMbox
		delay 1
	end try
	try
		close access theFinalMbox
		delay 1
	end try
	tell application "Finder" to delete alias tempMboxFile
end convertASCII

on createImportFolderOnDesktop()
	set safeImportFolderName to my makeUniqueName(importFolderName, pathToDesktop)
	set importFolderName to safeImportFolderName
	tell application "Finder"
		make new folder at alias pathToDesktop with properties {name:importFolderName}
		activate
		open alias (pathToDesktop & importFolderName)
	end tell
end createImportFolderOnDesktop

on displayOpeningDialog(applicationName)
	activate
	display dialog applicationName & " is ready to go..." buttons {"Continue"} default button 1 giving up after 10
end displayOpeningDialog

on displayStartingImportDialog(localNameOfMailbox)
	activate
	display dialog ("Starting import of mailbox '" & localNameOfMailbox & "'..." & return & return & ¬
		"[This dialog will be dismissed in 3 seconds]") buttons {"•"} default button 1 giving up after 3
end displayStartingImportDialog

on makeUniqueName(proposedFile, proposedLocation)
	tell application "Finder"
		set mboxExtension to 0
		set dotPosition to (offset of ".mbox" in proposedFile) as number
		if (dotPosition is not equal to 0) then
			set mboxExtension to 1
			set namePrefix to ((characters 1 thru (dotPosition - 1) of proposedFile) as string)
		end if
		set success to 0
		set counter to 1
		set proposedName to proposedFile
		repeat until success is equal to 1
			set allNames to {}
			try
				set allNames to allNames & name of every file of alias proposedLocation
			end try
			try
				set allNames to allNames & name of every folder of alias proposedLocation
			end try
			if (allNames) contains proposedName then
				if (mboxExtension is equal to 1) then
					set proposedName to (namePrefix & " " & counter as string) & ".mbox"
				else
					set proposedName to proposedFile & " " & counter as string
				end if
				set counter to counter + 1
			else
				set success to 1
				return proposedName
			end if
		end repeat
	end tell
end makeUniqueName

on moveMailToFinalDestination()
	if MacOSVersion is equal to "Mac OS X" then
		tell application "Finder"
			set success to 0
			set counter to 1
			set originalFile to alias (pathToDesktop & importFolderName)
			set currentName to importFolderName
			repeat until success is equal to 1
				if name of every folder of alias pathToMailFolder contains currentName then
					set currentName to importFolderName & " " & counter as string
					set name of originalFile to currentName
					set originalFile to alias (pathToDesktop & currentName)
					set counter to counter + 1
				else
					set originalFile to pathToDesktop & currentName
					move alias originalFile to pathToMailFolder
					set success to 1
				end if
			end repeat
		end tell
		activate
		display dialog "Importing done! If Mail is already running, you need to quit and relaunch Mail." buttons {"OK"} default button 1
	else if MacOSVersion is equal to "Mac OS" then
		activate
		display dialog "Importing done! A folder was created on your Desktop called '" & importFolderName & ¬
			"' which contains your exported email from " & emailClient & ". " & return & return & "Follow the instructions in the Read Me file to move this folder to the proper location on your Mac OS X  machine." buttons {"OK"} default button 1
	end if
end moveMailToFinalDestination

on traverseNetscapeFolders(currentFolder)
	tell application "Finder"
		set completeMailboxList to (every file of currentFolder)
		repeat with eachItem in completeMailboxList
			set rawFileName to name of eachItem
			if ((offset of ".snm" in rawFileName) is not equal to 0) then
				delete eachItem
			else
				set mboxFile to eachItem as string
				set savedMboxName to name of alias mboxFile
				set createLocation to container of alias mboxFile
				set name of alias mboxFile to rawFileName & "Temp"
				make new folder at createLocation with properties {name:savedMboxName & ".mbox"}
				move alias (mboxFile & "Temp") to the result
				set tempMboxFile to (mboxFile & ".mbox:" & savedMboxName & "Temp")
				set mboxFile to mboxFile & ".mbox:mbox"
				my convertASCII()
			end if
		end repeat
		set subFolders to every folder of currentFolder whose name does not contain ".mbox"
		repeat with eachFolder in subFolders
			my traverseNetscapeFolders(eachFolder)
		end repeat
	end tell
end traverseNetscapeFolders

on writeToLog(passedString)
	set pathToLogfile to pathToDesktop & importFolderName & ":log"
	set theData to (open for access file pathToLogfile with write permission)
	write (passedString & return) to theData starting at eof
	close access theData
end writeToLog