
#ifdef TPLTER

WB_Delete = {

    SHORT = {{ simulate WB deleting a file }};

    BUGS = {{
	* WB_Delete breaks on the first error
	* Disk.info is not handled proberly
    }};

    DESCRIPTION = {{
	"WB_Delete file" deletes the files
	"file" and "file.info" and additionally
	deletes the DiskObject of "file" (the visible
	icon) from the workbench.
    }};

    TODO = {{
	handling for "Disk.info" (we try to delete a file called 'disk')
    }};

    HISTORY = {{
	12-02-95 b_noll created
	20-02-95 b_noll restructured source
	21-02-95 b_noll added version/format-prefix/offset
	20-03-95 b_noll added args diagnostics
	19-08-95 b_noll created .data file
	30-08-95 b_noll v1.3 added wildcard support,
			added ctl-c handling (*ouch*)
    }};

    Libraries = { Icon; };
    Includes  = { "<string.h>" };

    Template = "FILE/M/A,ONLYICONS/S,ALL/S";
    Arguments = {{
	STRPTR* file;
	ULONG	onlyIcons;
	ULONG	all;
    }};

    UserData = {{
	UBYTE  apb[sizeof (struct AnchorPath) + 8 + MAXPATHLEN];
    }};

    version = "1.3";
    body = {{
	    long   cnt; 	    // for argv-slot
	    ULONG  error;	    // for match rv
	    struct AnchorPath  *ap; // long aligned Anchor

	    retval = RETURN_OK;

	    ap		      = (void *)(((ULONG)(userdata->apb) + 7) & ~7);

	    ap->ap_Strlen     = MAXPATHLEN;
	    ap->ap_BreakBits  = 0;
	    ap->ap_FoundBreak = 0;

	    for (cnt = 0; argv->file[cnt] && !retval && !IS_BROKEN(); ++cnt) {

		ap->ap_Flags = 0;

		for (error = MatchFirst(argv->file[cnt], ap); error == 0; error = MatchNext(ap)) {

		    /* ---- this is a directory - have we set the ALL flag? */

		    if (ap->ap_Info.fib_DirEntryType > 0 && argv->all) {

			/* ---- go into the other directory	      */
			/*	do not ye delete it - we'll come back */

			if (!(ap->ap_Flags & APF_DIDDIR)) {
			    ap->ap_Flags |= APF_DODIR;
//Printf (" entering %s\n", ap->ap_Buf);
			    continue;
			} /* if first visit */

			/* ---- leave the recent other directory */
			/*	and delete it upon return */

			ap->ap_Flags &= ~APF_DIDDIR;

		    } /* if isdir and 'all' */

		    if (BREAKCHECK())
			break;
//Printf ("doing WC %s\n", ap->ap_Buf);
		    retval = do_delete (ap->ap_Buf, gvars);

		} /* for wilds */
		MatchEnd(ap);


		if (error != ERROR_NO_MORE_ENTRIES) { /* abnormal error */
		    if ((error == ERROR_OBJECT_NOT_FOUND) && !(ap->ap_Flags & APF_ITSWILD)) {

			/* ---- we leave this backdoor in order 2 B able 2 */
			/*	delete icons whose files have been deleted */
			/*	by just typing the filename (w/o .info)    */

//Printf ("doing NW %s\n", ap->ap_Buf);
			retval = do_delete (argv->file[cnt], gvars);
			if (retval)
			    break;
		    } else {
			retval = RETURN_ERROR;
			break;
		    } /* if real error */
		} /* if possible error */
	    } /* for all patterns */


    }};

    addes = {{
	static __inline ULONG do_delete (STRPTR path, struct _gvars *gvars) {
	    UBYTE buffer [MAXPATHLEN];
	    BPTR lock;
	    ULONG retval = 0;
	    ULONG len, isIcon = 0;

	    strcpy (buffer, path);

	    /* ---- ignore .info endings */
	    len = strlen(path);
	    if (len > 5 && (!stricmp(path + len - 5, ".info"))) {
		len -= 5;
		buffer[len] = 0;
		isIcon = 1;
	    } /* if */

	    Printf ("deleting %s\n", buffer);

	    /* ---- Delete the main file */

	    if (!argv->onlyIcons || isIcon)
		if (lock = Lock(buffer, SHARED_LOCK)) {
		    UnLock(lock);

		    if (!DeleteFile(buffer)) {
			return RETURN_ERROR;
		    } /* if */
		} /* if */

	    /* ---- Delete the Icon and the .info file		      */
	    /*	    The rc may always be FALSE 'cause of FileNotFound */
	    /*	    for this reason we just ignore it		      */

	    DeleteDiskObject(buffer);

	    return retval;
	} /* do_delete */

    }};
};

#endif

    // for the sake of completeness, and i there is peraps an error
    // in the new way ... this is the old version ...

    body = {{
	    long cnt;
	    UBYTE buffer [MAXPATHLEN];
	    BPTR lock;

	    //if (IconBase = OpenLibrary(ICONNAME, 37)) {

		retval = RETURN_OK;

		for (cnt = 0; argv->file[cnt] && !retval; ++cnt) {

		    buffer[0] = 0;
		    strcat (buffer, argv->file[cnt]);

		    /* ---- Delete the main file */

		    if (!argv->onlyIcons)
		    if (lock = Lock(buffer, SHARED_LOCK)) {
			UnLock(lock);

			if (!DeleteFile(buffer)) {
			    retval = RETURN_ERROR;
			    break;
			} /* if */
		    } /* if */


		    /* ---- Delete the Icon and the .info file */
		    /*	    we delete file.info and diskobject separately, */
		    /*	    'cause else we get probs w/ defdiskobjects */

		    strcat (buffer, ".info");

		    if (lock = Lock(buffer, SHARED_LOCK)) {
			UnLock(lock);

			if (!DeleteFile(buffer)) {
			    retval = RETURN_ERROR;
			    break;
			} /* if */
		    } /* if */

		    /* ---- The rc is always FALSE 'cause of FileNotFound */
		    DeleteDiskObject(argv->file[cnt]);

		} /* for */

	    //	  CloseLibrary (IconBase);
	    //} /* if */
    }};

