/*
 *----------------------------------------------------------------------
 *           "THE BEER-WARE LICENSE" (Revision 42):
 * <jeang3nie@HitchHiker-Linux.org> wrote this file. As long as you
 * retain this notice you can do whatever you want with this stuff. If
 * we meet some day, and you think this stuff is worth it, you can buy
 * me a beer in return.
 * ---------------------------------------------------------------------
 *            ______   _______  _          _________
 *           (  __  \ (  ___  )( (    /|( )\__   __/
 *           | (  \  )| (   ) ||  \  ( ||/    ) (
 *           | |   ) || |   | ||   \ | |      | |
 *           | |   | || |   | || (\ \) |      | |
 *           | |   ) || |   | || | \   |      | |
 *           | (__/  )| (___) || )  \  |      | |
 *           (______/ (_______)|/    )_)      )_(
 *
 *         _______  _______  _       _________ _______
 *        (  ____ )(  ___  )( \    /|\__   __/(  ____ \
 *        | (    )|| (   ) ||  \  ( |   ) (   | (    |/
 *        | (____)|| (___) ||   \ | |   | |   | |
 *        |  _____)|  ___  || (\ \) |   | |   | |
 *        | (      | (   ) || | \   |   | |   | |
 *        | )      | )   ( || )  \  |___) (___| (____|\
 *        |/       |/     \||/    \_)\_______/(_______/
 *
 */

#define _XOPEN_SOURCE

#include <ctype.h>   // isprint
#include <libgen.h>  // basename
#include <stdio.h>   // fprintf, printf
#include <stdlib.h>  // exit
#include <unistd.h>  // getopt, unlink

static const char *__progname;
int vflag;

static void usage() { fprintf(stderr, "usage: %s target name\n", __progname); }

int _unlinkit(char *file) {
  // do the actual unlink
  if (unlink(file) == 0) {
    // verbose flag is set
    if (vflag) {
      fprintf(stderr, "unlink: %s\n", file);
    }
    return 0;
  } else {
    perror("unlink");
    exit(EXIT_FAILURE);
  }
}

int main(int argc, char *argv[]) {
  int index;
  int c;
  __progname = basename(argv[0]);

  while ((c = getopt(argc, argv, "hv")) != -1)
    switch (c) {
    case 'h':
      usage();
      return 0;
      break;
    case 'v':
      vflag = 1;
      break;
    case '?':
      if (isprint(optopt)) {
        usage();
        exit(1);
      }
    default:
      usage();
    }

  // no arguments supplied
  if (argv[optind] == NULL) {
    printf("Mandatory argument(s) missing\n");
    exit(1);
  }

  for (index = optind; index < argc; index++)
    _unlinkit(argv[index]);
}
