/*
//
//   ADOBE SYSTEMS INCORPORATED
//   Copyright (C) 2000-2003 Adobe Systems Incorporated
//   All rights reserved.
//
//   NOTICE: Adobe permits you to use, modify, and distribute this file
//   in accordance with the terms of the Adobe license agreement
//   accompanying it. If you have received this file from a source other
//   than Adobe, then your use, modification, or distribution of it
//   requires the prior written permission of Adobe.
//
*/

#include "ASCalls.h"
#include "macUtils.h"
#include <ApplicationServices/ApplicationServices.h>
#include <Foundation/NSBundle.h>
#include <Foundation/NSFileManager.h>

static   void  *  scratchFourBytes;

ASPathName   GetMacPath( const   char  * filename)
{
/* This function returns an ASPathName for opening and closing PDF files on the Macintosh platform.
Paths can be specified with a relative Unix path and an ASPathName will be created relative 
to the directory from which the application was launched. The ASPathName returned must be freed with
ASFileSysReleasePath(ASGetDefaultFileSys(),path);

*****
The code in this function is unsupported. 
It is a simple utility for the sample applications for handling paths without the need for a dialog.       
*****
*/
                ASPathName  newPathName =  NULL;
                ASPathName  appPathName =  NULL;

                //relative? Then relative to app
                if (filename[0] !=  '/' )
               {
                                //On the mac we need an absolute path, so we get the location of the application
                               CFBundleRef bundleRef = CFBundleGetMainBundle (); 
                               CFURLRef bundleURL = CFBundleCopyExecutableURL ( bundleRef );                     
                                ASPathName  pathName =  ASFileSysCreatePathName(NULLASAtomFromString( "CFURLRef" ), bundleURL,  NULL);
                               CFRelease(bundleURL);
                               
                                //bounce out of the package to the app container folder
                               appPathName =  ASFileSysPathFromDIPath(NULL, "../../.." , pathName);
                                ASFileSysReleasePath(ASGetDefaultFileSys(),pathName);
                               newPathName =  ASFileSysCreatePathFromDIPath(NULL,filename, appPathName);                                               
               }
                else
                               newPathName =  ASFileSysCreatePathNameNULL,
                                                                                ASAtomFromString(  "POSIXPath"  ), filename,  NULL  );

                if (appPathName)
                                ASFileSysReleasePath(ASGetDefaultFileSys(),appPathName);

               

                return  newPathName;
}



/* This sets the working directory so that standard C library calls write to the directory containing the executeable */

OSErr      PDFLSetWorkDirToAppDir()
{
       NSString * arrpath = [[NSBundle mainBundle] bundlePath];
       NSFileManager *fm =[NSFileManager defaultManager];
        if  (! [fm changeCurrentDirectoryPath:arrpath]) {
                return  procNotFound;
       }  else  {
                return  noErr;
       }

}