#include "config.h"
#include "kanji.h"

void
usage(const_string str)
{
  fprintf (stderr, "Try `%s --help' for more information.\n", str);
  uexit (1);
}

/* Call usage if the program exits by printing the help message.
   MESSAGE is an NULL-terminated array or strings which make up the
   help message.  Each string is printed on a separate line.
   We use arrays instead of a single string to work around compiler
   limitations (sigh).
*/
void
usagehelp(const_string *message, const_string bug_email)
{
  if (!bug_email)
    bug_email = "ptex-staff@ml.asciimw.jp";
  while (*message) {
    fprintf(stdout, "%s\n", *message);
    ++message;
  }
/*
  fprintf(stdout, "\nEmail bug reports to %s.\n", bug_email);
*/
  fflush(stdout);
  uexit (0);
}

void
printversionandexit(const_string banner,
                        const_string copyright_holder,  
                        const_string author,
                        char *extra_info)
{
  extern string based_prog_name;
  extern string versionstring;
  extern KPSEDLL string kpathsea_version_string;/* from kpathsea/version.c */
  string prog_name;
  int len;
  const_string prog_name_end;
  const_string prog_version;

  prog_name_end = strchr (banner, ',');
  prog_version = prog_name_end + sizeof("Version") + 2;
  len = prog_name_end - banner - sizeof ("This is");
  prog_name = (string)xmalloc (len + 1);
  strncpy (prog_name, banner + sizeof ("This is"), len);
  prog_name[len] = 0;

  /* The Web2c version string starts with a space.  */
  fprintf (stdout, "%s %s (%s)%s\n",
     prog_name, prog_version, get_enc_string(), versionstring);
  fputs (kpathsea_version_string, stdout);
  fprintf(stdout, "\n");

  if (copyright_holder) {
    printf ("Copyright 2009 %s.\n", copyright_holder);
    if (!author)
      author = copyright_holder;
  }

  puts ("Copyright 2009 Peter Breitenlohner (e-TeX).");

  fputs ("There is NO warranty.  Redistribution of this software is\n", stdout);
  fputs ("covered by the terms of ", stdout);
  /* DVIcopy is GPL'd, so no additional words needed. */
  if (/*copyright_holder && */!STREQ (prog_name, "DVIcopy")) {
    fprintf (stdout, "both the %s copyright and\n", prog_name);
  }
  fputs ("the GNU General Public License.\n", stdout);
  fputs ("For more information about these matters, see the files\n", stdout);
  fprintf (stdout, "named COPYING and the %s source.\n", prog_name);
  fprintf (stdout, "Primary author of %s: %s.\n", based_prog_name, author);

  fflush(stdout);

  if (extra_info) {
    puts (extra_info);
  }

  free(prog_name);
  uexit (0);
}
