/*
 This file is part of SLRN.

 Copyright (c) 1994, 1999 John E. Davis <davis@space.mit.edu>

 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 of the License, 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, Inc., 675
 Mass Ave, Cambridge, MA 02139, USA. 
*/
#include "config.h"
#include "slrnfeat.h"

#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifdef HAVE_SYS_UTSNAME_H
# include <sys/utsname.h>
#endif

#include <slang.h>
#include "version.h"
#include "server.h"
#include "group.h"
#include "art.h"
#include "chmap.h"

char *Slrn_Version = SLRN_VERSION;

typedef struct 
{
   char *name;
   int value;
}
Compile_Option_Type;

static Compile_Option_Type Compile_Options [] =
{
     {"PULL_SUPPORT",		SLRN_HAS_PULL_SUPPORT},
     {"NNTP_SUPPORT",		SLRN_HAS_NNTP_SUPPORT},
     {"SPOOL_SUPPORT",		SLRN_HAS_SPOOL_SUPPORT},
     {"INEWS_SUPPORT",		SLRN_HAS_INEWS_SUPPORT},
     {"SPOILERS",		SLRN_HAS_SPOILERS},
     {"MIME",		SLRN_HAS_MIME},
     {"SORT_BY_SCORE",		SLRN_HAS_SORT_BY_SCORE},
     {"USER_AGENT",		SLRN_HAS_USER_AGENT},
     {"TILDE_FEATURE",		SLRN_HAS_TILDE_FEATURE},
     {"END_OF_THREAD",		SLRN_HAS_END_OF_THREAD},
     {"GROUPLENS",		SLRN_HAS_GROUPLENS},
     {"MSGID_CACHE",		SLRN_HAS_MSGID_CACHE},
     {"SLANG",		SLRN_HAS_SLANG},
     {"GEN_MSGID",		SLRN_HAS_GEN_MSGID},
     {"CHARACTER_MAP",		SLRN_HAS_CHARACTER_MAP},
     {"UUDEVIEW",		SLRN_HAS_UUDEVIEW},
     {"FAKE_REFS",		SLRN_HAS_FAKE_REFS},
     {"DECODE",			SLRN_HAS_DECODE},
     {"PIPING",			SLRN_HAS_PIPING},
     {"PIPING",			SLRN_HAS_PIPING},
     {"DECODE",			SLRN_HAS_DECODE},
     {"RNLOCK",			SLRN_HAS_RNLOCK},
     {"EMPHASIZED_TEXT",	SLRN_HAS_EMPHASIZED_TEXT},
     {"VERBATUM_MARKS",		SLRN_HAS_VERBATUM_MARKS},
   
   {NULL, 0}
};

static void show_compile_time_options (void)
{
   Compile_Option_Type *opt;
   unsigned int len;

   fprintf (stdout, "\nslrn compile-time options:\n");
   
   opt = Compile_Options;
   len = 0;
   while (opt->name != NULL)
     {
	unsigned int dlen = strlen (opt->name) + 3;

	len += dlen;
	if (len >= 80)
	  {
	     fputc ('\n', stdout);
	     len = dlen;
	  }
	fprintf (stdout, " %c%s", (opt->value ? '+' : '-'), opt->name);
	opt++;
     }
   fputc ('\n', stdout);
}



static char *make_slang_version (unsigned int v)
{
#if SLANG_VERSION >= 10307
   (void) v;
   return SLang_Version_String;
#else
   unsigned int a, b, c;
   static char buf[32];
   
   a = v/10000;
   b = (v - a * 10000) / 100;
   c = v - (a * 10000) - (b * 100);

   sprintf (buf, "%u.%u.%u", a, b, c);
   return buf;
#endif
}

void slrn_show_version (void) /*{{{*/
{
   char *os;

   os = slrn_get_os_name ();

# if defined(__DATE__) && defined(__TIME__)
   fprintf (stdout, "Slrn Version: %s (%s %s)\n", Slrn_Version, __DATE__, __TIME__);
#else
   fprintf (stdout, "Slrn Version: %s\n", Slrn_Version);
#endif
   fprintf (stdout, "S-Lang Library Version: %s\n", make_slang_version (SLang_Version));
   if (SLANG_VERSION != SLang_Version)
     {
	fprintf (stdout, "\t** Note: This program was compiled against version %s.\n",
		 make_slang_version (SLANG_VERSION));
     }
   fprintf (stdout, "OS: %s\n", os);
   
   show_compile_time_options ();

   fprintf (stdout, "\nDefault server object: %s\n",
	    slrn_map_object_id_to_name (0, SLRN_DEFAULT_SERVER_OBJ));
   
   fprintf (stdout, "Default posting mechanism: %s\n",
	    slrn_map_object_id_to_name (1, SLRN_DEFAULT_POST_OBJ));

   
#if SLRN_HAS_CHARACTER_MAP
   slrn_chmap_show_supported ();
#endif
   exit (0);
}

/*}}}*/

#ifdef REAL_UNIX_SYSTEM
static char *get_unix_system_name (void)
{
# ifdef HAVE_UNAME
   static struct utsname u;
   
   if (-1 != uname (&u))
     return u.sysname;
# endif
   return "Unix";
}
#endif				       /* Unix */

#ifdef VMS
static char *get_vms_system_name (void)
{
# ifdef MULTINET
   return "VMS/Multinet";
# else
#  ifdef UXC
   return "VMS/UCX";
#  else
#   ifdef NETLIB
   return "VMS/Netlib";
#   else
   return "VMS";
#   endif
#  endif
# endif
}
#endif				       /* VMS */

#ifdef __os2__
static char *get_os2_system_name (void)
{
   return "OS/2";
}
#endif

#ifdef AMIGA
static char *get_amiga_system_name (void)
{
   return "Amiga";
}
#endif

   
#if defined(__WIN32__)
static char *get_win32_system_name (void)
{
   return "Win32";
}
#endif

char *slrn_get_os_name (void)
{
#ifdef REAL_UNIX_SYSTEM
   return get_unix_system_name ();
#else
# ifdef VMS
   return get_vms_system_name ();
# else
#  ifdef __os2__
   return get_os2_system_name ();
#  else
#   ifdef AMIGA
   return get_amiga_system_name ();
#   else
#    if defined(__WIN32__)
   return get_win32_system_name ();
#    else
   return "Unknown";
#    endif
#   endif
#  endif
# endif
#endif
}
