/*
 *----------------------------------------------------------------------
 *           "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 _DEFAULT_SOURCE

#include <sys/sysinfo.h>  // get_nprocs, get_nprocs_conf

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

static const char *__progname;

static void usage() { fprintf(stderr, "usage: %s [-ha]\n", __progname); }

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

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

  // program accepts no arguments
  if (argv[optind] != NULL) {
    printf("%s: extra operand '%s'\n", __progname, argv[optind]);
    usage();
    exit(1);
  }

  if (aflag)
    printf("%d\n", get_nprocs_conf());
  else
    printf("%d\n", get_nprocs());
  return 0;
}
