/*     DeleteLink  (or NewDeleteLink ;-)  
 *        by
 *    Phil Dietz -- NCEMRSoft
 *
 *    USAGE:    DeleteLink FILE/A
 *
 *		FILE/A  -  File to delete.  This can be ANY file!
 *
 *    SUMMARY:  Deletes a soft link pointing to an unmounted file
 *              which Commodore's 'delete' SHOULD do, but doesn't.
 *
 *    TIPS:     You know you have a bad soft link when:
 *              NewList82 shows 'link -> :UNKNOWN FILE:
 *          or  Newlist82 shows 'link -> !some_file
 *
 *              The link is not bad, it's just that you can't delete
 *              the link whenver you want.  That's why deletelink is here.
 *
 *    $VER: DeleteLink 2.0
 *
 *    Changes from Deletelink1.0 -
 *       Startup code removed.  Executable now only 360 bytes!
 *
 *    SC DeleteLink  NOLINK
 *    SLINK from DeleteLink.o to DeleteLink SC SD ND
 */

#include <stdio.h>
#include <exec/execbase.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/rdargs.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <devices/conunit.h>
#include <proto/dos.h>
#include <proto/exec.h>

#define TEMPLATE   "FILE/A\n"

static char ver[]="$VER: Deletelink (25.9.93) by Phil Dietz";

void __saveds mymain(char *cmd)
   {

	struct ExecBase		*SysBase = (*((struct ExecBase **) 4));
	struct Library		*DOSBase;
	struct WBStartup	*wbMsg = NULL;
	struct Process		*process;
	struct RDArgs		*rd;

	BPTR	lock;
	LONG	args=0L;
	int	rc=1;		/* return code */

	process = (struct Process *) SysBase->ThisTask;
	if (!(process->pr_CLI)) {
		WaitPort (&process->pr_MsgPort);
		wbMsg = (struct WBStartup *) GetMsg (&process->pr_MsgPort);
	}

	if (SysBase->LibNode.lib_Version < 37) goto xit;

	DOSBase = OpenLibrary ("dos.library", 37);
	if(DOSBase) {
		rd=ReadArgs(TEMPLATE,&args,NULL);	/* Parse args	*/

		if(args) {
			lock=Lock((STRPTR)args,SHARED_LOCK);	/* Get lock */
			if (lock) {
				if (!DeleteFile((STRPTR)args)) PutStr("Failed.\n");
				else rc=0;
			}
			FreeArgs(rd);	/* Free the readargs structure  */
		}
		CloseLibrary(DOSBase);	/* Close library 		*/
	}

xit:	if (wbMsg) {
		Forbid ();				/* Very important! */
		ReplyMsg ((struct Message *) wbMsg);	/* Must reply Workbench Msg */
	}
	process->pr_Result2=rc;
}
