#!/usr/local/bin/ruby
# $Id: install,v 1.2 2004/05/08 18:07:18 tommy Exp $

require "getopts"
require "fileutils"

$binfiles = ["tml", "tmladmin", "tmlctl"]
$libfiles = ["tml.rb", "mail.rb", "tml-file.rb", "tml-mysql.rb"]

$default_tml_dir = "/usr/local/tml"
$default_var_dir = "/var/spool/tml"
$default_ruby_path = nil
$defualt_domain = nil

$conf = "#{$default_tml_dir}/tml.conf"

$stdout.sync = true

unless getopts("f:") then
  puts "Usage: #{File.basename $0} [-f existing_config_file]"
  exit 1
end
if $OPT_f then
  load $OPT_f
else
  load $conf if File.exist? $conf
end

$default_ruby_path = $ruby_path if defined? $ruby_path
$default_tml_dir = $tml_dir if defined? $tml_dir
$default_var_dir = $var_dir if defined? $var_dir
$default_domain = $domain if defined? $domain

unless $default_ruby_path then
  ENV["PATH"].split(/:/).each do |p|
    if File.executable? "#{p}/ruby" then
      $default_ruby_path = "#{p}/ruby"
      break
    end
  end
end

loop do
  $ruby_path = nil
  while $ruby_path == nil
    print "Ruby path [#{$default_ruby_path}]: "
    ans = gets.chomp.strip
    if ans == "" then
      $ruby_path = $default_ruby_path
    else
      $ruby_path = ans
    end
    if $ruby_path and not File.executable? $ruby_path then
      puts "warning: #{$ruby_path}: not executable"
    end
  end

  print "tml directory [#{$default_tml_dir}]: "
  ans = gets.chomp.strip
  if ans == "" then
    $tml_dir = $default_tml_dir
  else
    $tml_dir = ans
  end
  unless File.directory? $tml_dir then
    puts "warning: #{$tml_dir} not found "
  end

  print "tml var directory [#{$default_var_dir}]: "
  ans = gets.chomp.strip
  if ans == "" then
    $var_dir = $default_var_dir
  else
    $var_dir = ans
  end
  unless File.directory? $var_dir then
    puts "warning: #{$var_dir} not found"
  end

  $domain = nil
  while $domain == nil
    print "Your domain (ex. example.com) [#{$default_domain}]: "
    ans = gets.chomp.strip
    if ans == "" then
      $domain = $default_domain
    else
      if ans !~ /^[a-z0-9-]+(\.[a-z0-9-]+)+/i then
        puts "warning: #{ans}: invalid domain"
      end
      $domain = ans
    end
  end

  puts <<EOS

Ruby path:         #{$ruby_path}
tml directory:     #{$tml_dir}
tml var directory: #{$var_dir}
domain:            #{$domain}

EOS
  print "correct? (Y/n) "
  ans = gets.chomp.strip
  puts
  break if ans == "" or ans.downcase[0] == ?y
end

FileUtils.mkdir_p $tml_dir
FileUtils.mkdir_p "#{$tml_dir}/templates"
FileUtils.mkdir_p $var_dir

$binfiles.each do |f|
  File.open("#{$tml_dir}/#{f}", "w") do |w|
    IO.foreach(f) do |l|
      w.puts l.gsub(%r|/usr/local/bin/ruby|, $ruby_path)
    end
  w.chmod 0755
  end
end
FileUtils.cp $libfiles, $tml_dir
Dir.glob("templates/*") do |f|
  FileUtils.cp f, "#{$tml_dir}/templates" if File.file? f
end

File.open("#{$tml_dir}/tml.conf-dist", "w") do |f|
  f.puts <<EOS
$ruby_path = "#{$ruby_path}"
$tml_dir = "#{$tml_dir}"
$var_dir = "#{$var_dir}"
$domain = "#{$domain}"
EOS
end
FileUtils.cp "#{$tml_dir}/tml.conf-dist", "#{$tml_dir}/tml.conf" unless File.exist? "#{$tml_dir}/tml.conf"
