/* Config.c	-preliminary config file for sula



    Author: Tano Fotang, 1997

    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; see the file COPYING.  If not, write to
    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <pwd.h>

#define CONFIG		"../include/config.h"
#define MKCONFIG	"mkconfig.h"

void
main (int argc, char **argv)
{
  struct utsname ut;
  struct passwd *pwd;
  FILE *fp;
  char *config = CONFIG;
  char *buf;
  char *p_utmp[] =
  {"/var/run/utmp", "/var/adm/utmp", "/var/log/utmp", "/etc/utmp", NULL};


  if (access (config, F_OK) > -1)
    {
      char buf[256];
      if (access (config, W_OK) < 0)
	{
	  fprintf (stderr, "[config error] %s: %s\n", config, strerror (errno));
	  exit (-1);
	}
      sprintf (buf, "%s.bak", config);
      rename (config, buf);
    }

  fp = fopen (config, "w");
  if (fp == NULL)
    {
      fprintf (stderr, "[config error] %s: %s\n", config, strerror (errno));
      exit (-1);
    }
  fprintf (fp, "\n/*	%s for sula.\n\tgenerated on %s, %s\n*/\n\n", config, __DATE__, __TIME__);
  fputs ("#ifndef _sula_config_h\n", fp);
  fputs ("#define _sula_config_h\n", fp);
  fputs ("\n\n", fp);
  pwd = getpwuid (getuid ());
  if (pwd)
    fprintf (fp, "#define sula_COMPILED\t\t\"%s (%s)\"\n", pwd->pw_name, pwd->pw_gecos);
  else
    fprintf (fp, "#define sula_COMPILED\t\t\"<unknown>(<unknown>)\"\n");
  buf = getenv ("HOSTNAME");
  fprintf (fp, "#define sula_HOST\t\t\"%s\"\n", buf ? buf : "<unknown host>");
  if (!uname (&ut))
    fprintf (fp, "#define sula_OS		\t\"%s %s\"\n", ut.sysname, ut.release);
  else
    fprintf (fp, "#define sula_OS	\t\t\"<unknown OS>\"\n");

  buf = *p_utmp;
  while (buf)
    if (access (buf, F_OK) > -1)
      {
	fprintf (fp, "#define UTMP	\t\t\"%s\"\n", buf);
	break;
      }
    else
      buf++;

  fputs ("\n#define DEFAULT_PORT            6667\n\n", fp);
  fputs ("#define P_IGNORE                \"sula_ignore\"\n", fp);
  fputs ("#define P_HISTORY               \"sula_history\"\n", fp);
  fputs ("#define P_SERVERS               \"sula_servers\"\n", fp);
  fputs ("#define P_BOOKMARKS             \"sula_bookmarks\"\n", fp);
  fputs ("#define P_NICKNAMES             \"sula_nicknames\"\n",fp);

  fputs ("\n\n", fp);
  fputs ("#endif", fp);
  fclose (fp);

  if (access (MKCONFIG, F_OK) > -1)
    {
      if (access (MKCONFIG, W_OK) < 0)
	{
	  fprintf (stderr, "[config error] %s: %s\n", MKCONFIG, strerror (errno));
	  exit (-1);
	}
    }
  rename (MKCONFIG, MKCONFIG ".bak");

fp = fopen (MKCONFIG, "w");
 if (fp == NULL)
  {
    fprintf (stderr, "[config error] %s: %s\n", MKCONFIG, strerror (errno));
	  exit (-1);
	  }
	  fprintf (fp, "\n##	%s for sula.\n##\tgenerated on %s, %s\n\n\n",
		   MKCONFIG, __DATE__, __TIME__);
	  
	  
	  fputs("BINMODE = u=rwx,go=rx\n",fp);
	  fputs("#LIB_DIR:\n#This is where help files etc will be installed. However,\n"
	  	"#program will search only in the directory specified in an X\n"
		"#resource file. LIB_DIR will be the default.\n#See Directories.Libraries\n"
		"#in ../sula.app-defaults\n",
		fp);
	  fputs ("LIB_DIR=/usr/local/lib/sula\n\n", fp);
	  fputs ("## private stuff.. ~/P_HOME\n",fp);
	  fputs ("P_HOME=.sula\n\n", fp);
	  fputs ("MK_CFLAGS = -DPARANOID -DLIB_DIR=\\\"$(LIB_DIR)\\\""
	  " -DP_HOME=\\\"$(P_HOME)\\\"\n\n", fp);
	  fputs ("BIN_DIR = /usr/local/X11/bin\n", fp);
	  fputs("#system-wide X resource directory:\n",fp);
	  fputs ("RESOURCE_DIR =  /usr/lib/X11/app-defaults\n", fp);
	  fclose (fp);

}
