
/*
 *  mkdir.c
 *
 *  Author: Georg Hessmann (hessmann@fmi.uni-passau-de)
 *
 *  Copyright: source is public domain, no copyright
 *
 *  Version history:
 *
 *  1.0  25.Sep.92  First release.
 *
 *  1.1  04.Okt.92  Add DEFAULT_ICON define.
 *                  If the define is defined, than mkdir creates always
 *                  a icon, exept NOICON is given.
 *                  If DEFAULT_ICON is not defined, mkdir works like the 
 *                  c:makedir, exept the keyword ICON is given.
 *
 */


#define VERSION "1.1"


#include <exec/types.h>
#include <dos/dos.h>
#include <workbench/workbench.h>

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/icon_protos.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/icon_pragmas.h>

static char version[] = "\0$VER: mkdir " VERSION " (" __DATE__ ")";


#define MSG_DI_OBJ	"Can't create icon %s.info\n"
#define MSG_ERR_CRE	"Can't create directory %s\n"
#define MSG_ALR_EX	"%s already exists\n"
#define MSG_NO_ARG	"No name given\n"


#if defined(DEFAULT_ICON)
#  define TEMPLATE	"NI=NOICON/S,NAME/M"
#else
#  define TEMPLATE	"ICON/S,NAME/M"
#endif




long __main(char * line)
{
  long rc = RETURN_OK;
  long options[2];
  struct RDArgs * args;

  struct Library * SysBase  = *(struct Library **)(4);
  struct Library * DOSBase  = OpenLibrary("dos.library", 37);
  struct Library * IconBase = OpenLibrary("icon.library", 37);
  if (!(DOSBase && IconBase)) return RETURN_FAIL;

  options[0] = options[1] = 0L;
  args = ReadArgs(TEMPLATE, options, NULL);
  if (args) {
    char ** dirs = (char **)options[1];
    if (dirs) {
      char * ptr;

#if defined(DEFAULT_ICON)
      int CreateIcons = !options[0];	// key -- NOICON
#else
      int CreateIcons = options[0];	// key -- ICON
#endif

      for (; ptr = *dirs; dirs++) {
        BPTR lock;
        lock = Lock(ptr, ACCESS_READ);
        if (lock) {
          VPrintf(MSG_ALR_EX, (long*)&ptr);
          rc = RETURN_ERROR;
        }
        else {
          lock = CreateDir(ptr);

          if (lock) {
            if (CreateIcons) {
              struct DiskObject * di = GetDefDiskObject(WBDRAWER);
              if (di && PutDiskObject(ptr, di)) {
                FreeDiskObject(di);
              }
              else {
                VPrintf(MSG_DI_OBJ, (long *)&ptr);
                PrintFault(IoErr(), NULL);
                rc = RETURN_ERROR;
              }
            }
          }
          else {
            VPrintf(MSG_ERR_CRE, (long*)&ptr);
            PrintFault(IoErr(), NULL);
            rc = RETURN_ERROR;
          }
        }
        UnLock(lock);
      }
    
      FreeArgs(args);
    }
    else {
      PutStr(MSG_NO_ARG);
      rc = RETURN_ERROR;
    }
  }
  else {
    PrintFault(IoErr(), NULL);
    rc = RETURN_ERROR;
  }
  
  CloseLibrary(IconBase);
  CloseLibrary(DOSBase);

  return rc;
}

