# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

require "getopts"
#require "optparse"

require "time"
require "pathname"
require "fileutils"
include FileUtils

require "qwik/config"
require "qwik/util-time"
require "qwik/util-path"

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

  def main(argv)
    @opt = parse_options(argv)

    if $OPT_v
      make_version
    elsif $OPT_m
      make_manifest
    elsif $OPT_d
      make_dist
    elsif $OPT_u
      upload
    else
      show_usage
    end
  end

  def parse_options(argv)
    getopts("nvmdu") # getopts uses ARGV
    opt = {}
    opt.update({:noop => true, :verbose => true}) if $OPT_n
    return opt
  end

  # ==============================
  def make_version
    date = Time.now.ymd_s
   #open(@config.base_dir+"/lib/qwik/version.rb", "wb"){|out|
    open(@config.qwiklib_dir+"/version.rb", "wb"){|out|
      out.print <<"EOS"
module Qwik
  class Config
    VERSION = "#{@version}"
    RELEASE_DATE = "#{date}"
  end
end
EOS
    }
  end

  # ==============================
  def make_manifest()
    ar = []
    ".".path.find {|f|
      ar << f.to_s if public_file?(f)
    }
    ar += %w(etc/)

    open("MANIFEST", "wb"){|out|
      ar.sort.each {|f|
	out.puts f
      }
    }
  end

  def public_file?(f)
    return false if f.directory?
    s = f.to_s
    %w(backup cache data etc grave log work).each {|dir| # ignore dir
      return false if s =~ /^#{dir}/
    }

    ignore_pattern = %w(
CVS sfcring memo.txt tar.gz .rm .MP4 .cvsignore
.stackdump
.o
.so
testlog.txt
)

    ignore_pattern.each {|pat| # ignore pattern
      return false if s.include?(pat)
      return false if s.include?(pat)
    }
    return false if s =~ /~$/
    true
  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_default_siteconfig
    make_default_password
    system_p "tar zcf #{@targz} #{@base}", @opt
    rm_rf @base, @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 cp_p( src, dest, options = {} )
    opt = options.dup
    opt[:preserve] = true
    cp(src, dest, opt)
  end

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

  def make_default_siteconfig
    File.mkdir(@base+"/data") unless File.exist?(@base+"/data")
    File.mkdir(@base+"/data/www") unless File.exist?(@base+"/data/www")
    open(@base+"/data/www/,forward", "wb"){|out|
      out.print "\n"
    }
    open(@base+"/data/www/_SiteMember.txt", "wb"){|out|
      out.print ",guest@qwik\n"
    }
    open(@base+"/lib/super/_SiteConfig.txt", "rb"){|f|
      open(@base+"/data/www/_SiteConfig.txt", "wb"){|out|
	while line = f.gets
	  line = line.sub(/:open:false/, ":open:true")
	  out.print line
	end
      }
    }
  end

  def make_default_password
    file = "etc/password-dist.txt"
    str = read_file(file)
    write_file(@base+"/"+file, str)

    file = "etc/password-dist.txt"
    write_file(@base+"/"+file, str)

    file = "etc/config-dist.txt"
    str = read_file(file)
    write_file(@base+"/"+file, str)

    file = "etc/config.txt"
    str = read_file(file)
    write_file(@base+"/"+file, str)

    file = "etc/config-default.txt"
    str = read_file(file)
    write_file(@base+"/"+file, str)
  end

  def read_file(file)
    open(file){|f| f.read }
  end

  def write_file(file, str)
    open(file, "wb"){|f| f.print str }
  end

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

  # ==============================
  def show_usage
    puts "usage:"
    puts "./make.rb -m -> make manifest"
    puts "-n to see the work without execution"
  end
end
