/* vim: set syntax=objc : */

/* CleanPath
 *
 * A tool to help clean up unix-style :-delimited paths by eliminating
 * redundant (and therefore useless) elements while preserving order and
 * observable behavior.
 *
 * Thanks to the folks on #gnustep for their advice and assistance.
 */

/* C includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Object-C imports */
#import <Foundation/NSSet.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>

/* helper functions */
void usage( char *program ) {
   fprintf(stderr,"Usage: %s path-list [path-list...]\n", program );
}

/* entry point */
int main( int argc, char **argv ) 
{
   NSMutableSet *set;
   NSMutableArray *list;
   NSAutoreleasePool *pool;
   int i,j;
   int size;

   pool = [[NSAutoreleasePool alloc] init];

   list = [[[NSMutableArray alloc] init] autorelease];
   set  = [[[NSMutableSet alloc] init] autorelease];

   if ( argc == 1 ) { usage( argv[0] ); exit( EXIT_FAILURE ); }

   for ( i = 1; i < argc ; i++ ) 
   {
      NSString *str = [NSString stringWithCString: argv[i]];
      NSArray *split = [str componentsSeparatedByString: @":"];

      size = [split count];
      for ( j = 0; j < size; j++ )
      {
         NSString *s = [split objectAtIndex: j];
         if ( ! [set containsObject: s] ) 
         {
            [set  addObject: s ];
            [list addObject: s ];
         }
      }
   }//for

   fprintf( stdout, "%s", [[list componentsJoinedByString: @":"] cString]);

   [pool release];
   return EXIT_SUCCESS;
}

