//
//  safariExport.m
//
//  Created by R A Fackler on Wed Jan 05 2005.
//  Copyright (c) 2005 R A Fackler. All rights reserved.
//
// This program uses code from the Adium Project with permission of Stephen Holt
// http://adiumx.com/
// This program is released under the GPL Version 2, see License.txt for details
// it's hackish, and I no longer care that it is for the night, as
// CoreFoundation is alien to me and I don't care to go swapping entities manually
// for now. Maybe some day in the future. After other projects are done. For now,
// you need to filter this output with sed or something similar to be useful.


#import "SHSafariBookmarksImporter.h"
#import "SHMarkedHyperlink.h"
#import "Cocoa/Cocoa.h"
#import <Foundation/Foundation.h>

void printArrayOfLinks(NSArray *array, unsigned indentLevel);

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	SHSafariBookmarksImporter *importer = [SHSafariBookmarksImporter 
						newInstanceOfImporter];
	NSArray *marks = [importer availableBookmarks];
	printArrayOfLinks(marks,0);
    [pool release];
    return 0;
}

void printArrayOfLinks(NSArray *array, unsigned indentLevel)
{
	unsigned i;
	unsigned j;
	for(i = 0 ; i < [array count]; i++ ){
		if ( [[array objectAtIndex:i] isKindOfClass:[NSDictionary class]] )
		{//new dir
			for ( j = 0 ; j <indentLevel; j++)
				printf("	");
			printf("<folder name=\"%s\">\n",[[[array objectAtIndex:i] objectForKey:SH_BOOKMARK_DICT_TITLE] UTF8String]);
			printArrayOfLinks([[array objectAtIndex:i] objectForKey:SH_BOOKMARK_DICT_CONTENT],indentLevel+1);
			for ( j = 0 ; j <indentLevel; j++)
				printf("	");
			printf("</folder>\n");
		}
		else if( [[array objectAtIndex:i] isKindOfClass:[SHMarkedHyperlink class]]){
			for ( j = 0 ; j <indentLevel; j++)
				printf("	");
			printf("<a href=\"%s\">%s</a>\n",[[[[array objectAtIndex:i] URL] absoluteString] UTF8String],[[[array objectAtIndex:i] parentString] UTF8String]);
			
		} else
			printf("Something happened in safariExport that went wrong!\n");
	}
}