/* examples/example.c - example program for demonstrating libsizzle usage
 
  Copyright (C) 2000 Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
 
  This 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, or (at your option)
  any later version.
  
  This software 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 package; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.  */

#if HAVE_CONFIG_H
# include <config.h>
#endif 

#include <stdio.h>

#if ENABLE_NLS
# include <libintl.h>
# define _(String) gettext(String)
#else
# define _(String) (String)
#endif

/* This header file must be included into all programs using the
   Sizzle library.  */
#include <libsizzle/libsizzle.h>

/* These are the variables which will be bound to Scheme variables.  */
static int binary_port = 1212;
static int command_port = 1213;
static char hostname[256] = "bla.fasel.org";
static int verbose = 0;
static char download_area[512] = "/tmp";
static zzz_scm_t kill_lines = NULL;

int
main (int argc, char * argv[])
{
  zzz_scm_t dummy;
  int return_val;

  /* Announce top of stack to Sizzle library, so conservative marking
     works.  */
  zzz_set_top_of_stack (&dummy);

  /* Initialize the library.  */
  zzz_initialize ();
  
  /* Make command line options available to Scheme code.  The zero'th
     argument is handled specially to make pre-processing of the
     arguments easier (see src/sizzle.c for details).  */
  zzz_set_arguments (argc - 1, argv[0], argv + 1);

  /* Bind some variables of different types.  The last argument is a
     flag whether the variable is allowed to get changed by Scheme
     code.  */
  zzz_bind_int_variable ("binary-port", &binary_port, 0);
  zzz_bind_int_variable ("command-port", &command_port, 0);
  zzz_bind_bool_variable ("verbose", &verbose, 0);
  zzz_bind_string_variable ("hostname", hostname, sizeof (hostname), 0);
  zzz_bind_string_variable ("download-area", download_area,
			    sizeof (download_area), 0);
  zzz_bind_scm_variable ("kill-lines", &kill_lines, 0);

  /* Evaluate startup file.  */
  if ((return_val = zzz_evaluate_file (zzz_toplevel_env, "startup.scm")) != 0)
    fprintf (stderr, "example: error while reading init file\n");

  /* Interpretation of bound variables of type zzz_scm_t must be done
     before the library is finalized, otherwise dangling pointers will
     make your life hard.  */

  /* Iterate over list.  */
  while (cons_p (kill_lines))
    {
      /* Make sure we are dealing with strings.  */
      if (string_p (car (kill_lines)))
	{
	  printf ("Kill line: %s\n", string_val (car (kill_lines)));
	}
      kill_lines = cdr (kill_lines);
    }
  /* Free all memory used by the interpreter.  */
  zzz_finalize ();

  /* Print the variables' values, so we can check whether everything
     works fine.  */
  printf ("Binary port: %d\n", binary_port);
  printf ("Command port: %d\n", command_port);
  printf ("Hostname: %s\n", hostname);
  printf ("Download area: %s\n", download_area);
  printf ("Verbose: %s\n", verbose ? "yes" : "no");

  /* Check for memory leaks...  */
  printf("%s: %d\n", _("Allocated bytes"), zzz_allocated_bytes);

  /* Return the result of the call to `zzz_evaluate_file ()' as exit
     code.  */
  return return_val;
}
