#!/usr/bin/env ruby
# $Id$
# Copyright (C) 2003-2004 Kouichirou Eto, 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 GPL2.

$LOAD_PATH.unshift('lib')
require 'getopts'
require 'pathname'
require 'fileutils'
include FileUtils
require 'qwik/config'
require 'qwik/util'

class Make
  def initialize(opt)
    @opt = opt
    @config = Qwik::Config.new(true)
    @version = @config.version.to_s
    @release_date = @config.release_date.to_s
    @base = "qwik-"+@version
    # @base = "qwik-#{@version}-#{@release_date}"
    @targz = @base+".tar.gz"
    @upload_dest = "eto.com:/var/www/2004/qwikWeb/"
  end

  def check_file(out, f)
    return if f.directory?
    s = f.to_s
    ignore_dir = %w(data etc grave work souko theme compat log)
    ignore_dir.each {|dir|
      return if s =~ /^#{dir}/
    }
    return if s =~ /\/CVS/
    return if s =~ /sfcring/
    return if s =~ /qwikred/
    return if s =~ /_ring/
    return if s =~ /etc\/password.txt/
    return if s =~ /watch-/
    return if s =~ /^qwik-/
    return if s =~ /bin\/quickml/
    return if s =~ /bin\/ssl/
    return if s =~ /bin\/crontab/
    return if s =~ /.tar.gz$/
    return if s =~ /memo.txt/
    return if s =~ /~$/
    if s =~ /^lib\/theme\//
      if s =~ /^lib\/theme\/qwik/ ||
	  s =~ /^lib\/theme\/hiki/ ||
	  s =~ /^lib\/theme\/base.css/
	out.puts s
      end
      return
    end
    out.puts s
  end

  def make_manifest()
    open("MANIFEST", "wb"){|out|
      ".".path.find {|f|
	check_file(out, f)
      }
      static_files = %w(
log/
cache/
etc/
grave/
log/
data/www/SiteConfig
)
      static_files.each {|f|
	out.puts f
      }
    }
  end

  def system_p(cmd, options={})
    print "#{cmd}\n" if options[:verbose]
    return if options[:noop]
    system cmd
  end

  def cp_p( src, dest, options = {} )
    opt = options.dup
    opt[:preserve] = true
    cp(src, dest, opt)
  end

  def cp_all(src, dest, opt={})
    src.each {|file|
      dir = File.dirname(file)
      dir = file.chop if file =~ /\/$/
      destdir = dest+"/"+dir
      mkdir_p "#{@base}/#{dir}", opt unless FileTest.directory? destdir
      cp_p(file, destdir, opt) unless file =~ /\/$/
    }
  end

  def make_file
    open(@base+"/etc/password.txt", "wb"){|out|
    }
  end

  def make_dist
    @files = open("MANIFEST"){|f| f.read }.split
    rm_rf @base, @opt
    rm_f  @targz, @opt
    mkdir @base, @opt
    cp_all @files, @base, @opt
    make_file
    system_p "tar zcf #{@targz} #{@base}", @opt
    rm_rf @base, @opt
  end

  def upload
    system_p("scp "+@targz+" "+@upload_dest, @opt)
  end
end

def main
  getopts("nmdu")
  opt = {}
  opt.update({:noop => true, :verbose => true}) if $OPT_n

  m = Make.new(opt)
  if $OPT_m
    m.make_manifest()
  elsif $OPT_d
    m.make_dist
  elsif $OPT_u
    m.upload
  else
    puts "usage:"
    puts "./make.rb -m -> make manifest"
    puts "./make.rb -m -> make manifest"
    puts "-n to see the work without execution"
  end
end
main
