#!/usr/bin/env ruby
# This software code is made available "AS IS" without warranties of any
# kind.  You may copy, display, modify and redistribute the software
# code either by itself or as incorporated into your code; provided that
# you do not remove any proprietary notices.  Your use of this software
# code is at your own risk and you waive any claim against the author
# with respect to your use of this software code.
# (c) 2007 Static-CMS

require 'optparse'
require 'fileutils'
require 'Scms'

options = {}
optparse = OptionParser.new do|opts|
  # Set a banner, displayed at the top
  # of the help screen.
  opts.banner = "Usage: Scms [options]"
 
  # Define the options, and what they do
  opts.on('-w', '--website WEBSITE', "Website directory (full path)") do |w|
    options[:website] = w
  end
  
  opts.on('-o', '--output BuildDIR', "Website build dir (full path)") do |o|
    options[:pub] = o
  end
 
  options[:action] = "build"
   opts.on( '-a', '--action ACTION', 'Build, Deploy, Create or Clean' ) do|a|
     options[:action] = a
  end
   
  options[:mode] = "pub"
  opts.on( '-m', '--mode MODE', 'CMS or Publish' ) do|m|
     options[:mode] = m
  end
   
  options[:html] = "true"
  opts.on( '-f', '--html HTML', 'true or false' ) do|h|
     options[:html] = h
  end

  options[:verbose] = false
  opts.on( '-v', '--verbose', 'Output more information' ) do
     options[:verbose] = true
  end
  # This displays the help screen, all programs are
  # assumed to have this option.
   opts.on( '-h', '--help', 'Display this help screen' ) do
     puts opts
     exit
   end
end
optparse.parse!

#Set globals
$website = (options[:website].nil?) ? Dir.pwd : options[:website] 
$html = (ENV["SCMS_HTML_OUT"] or "false")
$stdout.sync = true
root_folder = File.expand_path("../", File.dirname(__FILE__))
Folders = {
  :root => root_folder,
  :website => File.join($website),
  :pub => (ENV["SCMS_PUBLISH_FOLDER"] or options[:pub]),
  :assets => (ENV["SCMS_STATICCMS_FOLDER"] or File.join(root_folder, "assets")),
  :config => (ENV["SCMS_CONFIG_FOLDER"] or File.join($website))
}

if options[:action] == "create"
    if Dir.exists? Folders[:website]
        throw "Website already exists!!!"
    else
        puts "Making website: #{Folders[:website]}"
        FileUtils.mkdir_p Folders[:website]
        Dir.chdir(File.join(Folders[:assets], "blank-template")) do
            files = Dir.glob('*')
            FileUtils.cp_r files, Folders[:website]
        end
    end
    exit
end

#puts "System root folder = #{Folders[:root]}"
#puts "Website folder = #{Folders[:website]}"
#puts "Pub dir = #{Folders[:pub]}"
#puts "Config dir = #{Folders[:config]}"
#puts "Mode = #{options[:mode]}"

raise "No website in folder #{Folders[:website]}" if !File::directory?(Folders[:website])
Scms.Upgrade()
deployDir = Scms.build(Folders[:pub], Folders[:config], options[:mode])
#puts "deployDir = #{deployDir}"
Scms.deploy(deployDir, Folders[:config]) if options[:action] == "deploy"