/*
 * inst.c
 *
 * Copy files about an Acorn system
 *
 *  1998 Straylight/Edgeware
 */

/*----- Licensing note ----------------------------------------------------*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to the Free Software Foundation,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*----- Header files ------------------------------------------------------*/

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "swis.h"
#include "swiv.h"

#include "cmdr.h"

/*----- Install programs --------------------------------------------------*/

static int exitstat = 0;		/* Exit status */

/* -- @copy@ --- *
 *
 * Arguments:	@const char *s@ = pointer to source filename
 *		@const char *d@ = pointer to destination filename
 *
 * Returns:	---
 *
 * Use:		Copies a file.
 */

static void copy(const char *s, const char *d)
{
  _kernel_oserror *e;

  e = _swix(OS_FSControl, _inr(0, 3), 26, s, d, 0x1002);
  if (e) {
    fprintf(stderr, "inst: error copying `%s' to `%s': %s\n",
            s, d, e->errmess);
    exitstat = 1;
  }
}

/* --- @main@ --- *
 *
 * Arguments:	@int argc@ = number of arguments
 *		@char *argv[]@ = list of arguments
 *
 * Returns:	Zero if all went well
 *
 * Use:		Installs files in the right places.  This is a bit of a
 *		rip-off of unix `cp'.
 */

int main(int argc, char *argv[])
{
  const char *d;
  int ty;

  /* --- Expand wildcards in the arguments --- */

  cmdreplace(&argc, &argv);

  /* --- Report an error if there aren't enough arguments --- */

  if (argc < 3) {
    fprintf(stderr,
            "Usage:\n"
            "        inst SRCFILE DSTFILE\n"
            "        inst SRCFILE [SRCFILE...] DSTDIR\n");
    exit(1);
  }

  /* --- Sort out how to copy things --- */

  d = argv[argc - 1];
  ty = _swi(OS_File, _inr(0, 1) | _return(0), 17, d);

  if (ty < 2 && argc > 3) {
    fprintf(stderr,
            "inst: too many files; destination is not a directory\n");
    exit(1);
  }

  /* --- Do the copying --- */

  if ((ty & 1 || ty == 0) && argc == 3)
    copy(argv[1], argv[2]);
  else {
    char buf[1024];
    size_t sz = strlen(d);
    int i;

    memcpy(buf, d, sz);
    buf[sz] = '.';

    for (i = 1; i < argc - 1; i++) {
      char *p = strrchr(argv[i], '.');
      if (p) p++; else p = argv[i];
      strcpy(buf + sz + 1, p);
      copy(argv[i], buf);
    }
  }

  /* --- Done --- */

  return (exitstat);
}

/*----- That's all, folks -------------------------------------------------*/
