#! /usr/bin/ruby
# -*- mode: ruby -*-
#
# quickml - an easy-to-use mailing list server
#
# Copyright (C) 2002 Satoru Takabayashi <satoru@namazu.org> 
#     All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of 
# the GNU General Public License version 2.
#

$KCODE = "e"
require 'quickml'

def error (msg)
  STDERR.puts "#{$0}: #{msg}"
  exit(1)
end

def show_usage
  puts "Usage: quickml <data directory> <smtp server> <domain name>"
end

def check_directory (dir)
  error("#{dir}: No such directory") unless File.directory?(dir) 
  error("#{dir}: is not writable")   unless File.writable?(dir) 
end

def main (argv)
  config_file = if argv.length == 1 then 
		  argv.first 
		else 
		  File.join("/etc", "quickmlrc")
		end
  config = QuickML::Config::load(config_file)
  check_directory(config[:data_dir])

  config  = QuickML::Config.new(config)
  server  = QuickML::Server.new(config)
  sweeper = QuickML::Sweeper.new(config)
  trap(:TERM) { server.shutdown; sweeper.shutdown }
  trap(:INT)  { server.shutdown; sweeper.shutdown }
  trap(:HUP)  { config.logger.reopen }
  t = Thread.new { sweeper.start }
  t.abort_on_exception = true
  server.start
end
main(ARGV)
