/* (C) Copyright 1993 by Mike W. Meyer
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Mike W.
 * Meyer not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  Mike W. Meyer makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * MIKE W. MEYER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL MIKE W. MEYER BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <exec/types.h>
#include <exec/execbase.h>
#include <dos/dos.h>

#include <proto/exec.h>
#include <proto/dos.h>

#include <stdio.h>
#include <errno.h>
#include "version.h"

extern int optind;
extern char *optarg;

extern int overwrite_files ;

#define TEMPLATE	"Files/M,-f=Overwrite/S,-C=Directory/K"
enum { FILES, OVERWRITE, DIRECTORY, OPT_COUNT } ;
struct RDArgs *args = NULL ;
BPTR	OldDir = NULL ;

#define HELPSTRING "munpack vesion" MPACK_VERSION "\n\
Unpack input files. If no files are present, reads from standard in. The\n\
arguments other than file names are:\n\
-f=Overwrite/S		Causes the unpacked file to overwrite any existing\n\
			files of the same name. Otherwise, an extension\n\
			with a period and a number is added to the file\n\
			name.\n\
-C=Directory/K		Change to the given directory before unpacking.\n\
			Path names will be interpreted relative to the this\n\
			directory, not the current one.\n"

void
FinishArgs(void) {

	if (args) {
		FreeArgs(args) ;
		FreeDosObject(DOS_RDARGS, args) ;
		}
	if (OldDir) UnLock(CurrentDir(OldDir)) ;
	}

main(int argc, char **argv) {
	long opts[OPT_COUNT] ;
	FILE *file ;
	int goodenough ;
	extern struct ExecBase *SysBase ;

	goodenough = SysBase->LibNode.lib_Version > 36 ;

	/* Do the 2.x argument parsing stuff */
	if (!goodenough) opts[FILES] = argc ;
	else {
		onexit(FinishArgs) ;
		memset((char *) opts, 0, sizeof(opts)) ;
		if (!(args = AllocDosObject(DOS_RDARGS, NULL))) {
			PrintFault(IoErr(), *argv) ;
			exit(RETURN_FAIL) ;
			}
		args->RDA_ExtHelp = HELPSTRING ;
		if (!(args = ReadArgs(TEMPLATE, opts, args))) {
			PrintFault(IoErr(), *argv) ;
			exit(RETURN_FAIL) ;
			}
		overwrite_files = opts[OVERWRITE] ;
	
		if (opts[DIRECTORY])
			if (OldDir = Lock((char *) opts[DIRECTORY], SHARED_LOCK))
				OldDir = CurrentDir(OldDir) ;
			else PrintFault(IoErr(), (char *) opts[DIRECTORY]) ;

		argv = ((char **) opts[FILES]) - 1 ;
		}

	if (!opts[FILES]) {
		fprintf(stderr, "munpack: reading from standard input\n");
		handleMessage(stdin, "text/plain", 0) ;
		}
	else {
		while (*++argv)
			if (!(file = fopen(*argv, "r")))
				os_perror(*argv) ;
			else {
				handleMessage(file, "text/plain", 0) ;
				fclose(file) ;
				}
			}

	exit(0) ;
	}

warn(char *s) {
	fprintf(stderr, "munpack: warning: %s\n", s);
	}
