STRPTR WBtoCLIargs(struct WBArg * WBArg, STRPTR ArgTemplate)
{
	UBYTE *Argline, *ArglineSave, *SourcePtr, *DestPtr;
	UBYTE tempChar;
	BOOL sawEqual;
	int index;
	ULONG size;
	BPTR oldDir;
	struct DiskObject *dob;
	STRPTR TempBuffer;

	if (!(TempBuffer = AllocVec(BIG_BUF_SIZE, MEMF_CLEAR)))
		return NULL;

	oldDir = CurrentDir(WBArg->wa_Lock);

	if (!(dob = GetDiskObjectNew(WBArg->wa_Name))) {
		CurrentDir(oldDir);
		FreeVec(TempBuffer);
		return NULL;
	}

	/*
	 * if there are tooltypes then figure out how much memory we need to
	 * allocate to hold them as a command line.  anything that isn't a
	 * legal argument in our command line template is ignored.  This lets
	 * things like DONOTWAIT be used without causing an error at
	 * ReadArgs() time.
	 */

	if (dob->do_ToolTypes)
		for (size = index = 0; dob->do_ToolTypes[index]; index++) {
			SourcePtr = dob->do_ToolTypes[index];
			DestPtr = TempBuffer;

			while (tempChar = *SourcePtr++)
				if (tempChar == '=')
					break;
				else
					*DestPtr++ = tempChar;

			*DestPtr = '\0';

			if (FindArg(ArgTemplate, TempBuffer) != -1)
				size += strlen(dob->do_ToolTypes[index]) + 3;
		}
	else
		size = 0;

	if (Argline = AllocVec(size + 2, MEMF_CLEAR)) {
		ArglineSave = Argline;

		if (dob->do_ToolTypes)
			for (index = 0; dob->do_ToolTypes[index]; index++) {
				SourcePtr = dob->do_ToolTypes[index];
				DestPtr = TempBuffer;
				sawEqual = FALSE;

				while (tempChar = *SourcePtr++)
					if (tempChar == '=') {
						sawEqual = TRUE;
						break;
					}
					else
						*DestPtr++ = tempChar;

				*DestPtr = '\0';

				if (FindArg(ArgTemplate, TempBuffer) != -1) {
					CopyMem(TempBuffer, Argline, DestPtr - TempBuffer);
					Argline += DestPtr - TempBuffer;

					/*
					 * if we saw an equals sign when we
					 * broke the argument name out then
					 * we know we need to copy the
					 * argument's data over.  if we
					 * didn't see it then this argument
					 * is a switch and thus has no more
					 * data to copy over
					 */

					if (sawEqual) {
						*Argline++ = ' ';
						*Argline++ = '\"';

						while (tempChar = *SourcePtr++)
							*Argline++ = tempChar;

						*Argline++ = '\"';
					}

					*Argline++ = ' ';
				}
			}

		*Argline++ = '\n';
		*Argline = '\0';
	}
	else
		ArglineSave = NULL;

	FreeDiskObject(dob);
	CurrentDir(oldDir);
	FreeVec(TempBuffer);
	return ArglineSave;
}
