require 'optparse'
require 'ftools'
require 'fileutils'
      
module Rpanel
  class CLI
    IGNORE_DIRECTORIES = ['.', '..','deploy','quota.user','aquota.user','.localized', '.cpan', '.cpcpan', 'cpeasyapache', 'cprubybuild', 'cprubygemsbuild', 'domlogs', 'liquidweb', 'lost+found', 'fantasti', 'MySQL-install', 'virtfs']
    def self.execute(stdout, arguments=[])

      options = {
        :basedir => '/home',
        :fix_sc     => nil,
        :fix_owner     => nil,
        :fix_files => nil,
        :fix_directories => nil,
        :username => nil,
        :all_accounts => nil
      }
      mandatory_options = %w( )

      parser = OptionParser.new do |opts|
        opts.banner = <<-BANNER.gsub(/^          /,'')
          This application is wonderful because...

          Usage: #{File.basename($0)} [options]

          Options are:
        BANNER
        opts.separator ""
        opts.on("-b","--base-dir=BASEDIR", String, "Set base directory. defaults to /home"){|arg| options[:basedir] = arg.strip}
        opts.on("-l", "--list", "List all accounts"){ |arg| options[:list] = true }
        opts.on("-s", "--fix-source-control", 
                "Ensure .svn or .git directories are not group or world-readable") { |arg| options[:fix_sc] = true }
        opts.on("-o", "--fix-owner", 
                "Ensure all files in public_html are owned by the account username") { |arg| options[:fix_owner] = true }
        opts.on("-f", "--fix-files",  
                "Ensure all normal files are set no higher than 0644") { |arg| options[:fix_files] = true }
        opts.on("-d", "--fix-directories", 
                "Ensure all normal files are set no higher than 0755") { |arg| options[:fix_directories] = true }
        opts.on("-u", "--apply-user=USER", String, 
                "Your cpanel account username (ex: peppyhep)") { |arg| options[:username] = arg.strip }

        opts.on("-a", "--apply-all-accounts", 
                "Apply the selected update to permissions and ownerships to each account in the list") { |arg| options[:apply_all_accounts] = true }
        
        opts.on("-e", "--all", 
                "Apply ALL updates to permissions and ownerships to each account in the list")  {|args|  options[:all] = true }    
        opts.on("-h", "--help",
                "Show this help message.") { stdout.puts opts; exit }
        opts.parse!(arguments)

        if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? } || (options[:username].nil? && options[:apply_all_accounts].nil? )
          stdout.puts opts; exit
        end
      end

      if options[:list] || options[:apply_all_accounts]
          dirs = Dir.entries(options[:basedir]) - IGNORE_DIRECTORIES
          accounts = dirs.sort{|a,z| a.downcase <=>z.downcase}
      end
      
      if options[:list]
        puts accounts
        exit
      end
            
      username = options[:username]
      if options[:all]
        options[:fix_sc] = true
        options[:fix_owner] = true
        options[:fix_files] = true
        options[:fix_directories] = true 
      end

      
      if options[:fix_sc]
        puts "Fixing Source Control ownership"
        if accounts
          accounts.each do |account|
            puts "Fixing #{account} Source files"
            system("find #{File.join(options[:basedir], account, 'public_html')} -type d -name '.svn' -exec chown -R #{account}:#{account} {} \\;")
            system("find #{File.join(options[:basedir], account, 'public_html')} -type d -name '.svn' -exec chmod 0700 {} \\;")
            system("find #{File.join(options[:basedir], account, 'public_html')} -type d -name '.svn' -exec -exec find {} -type f -print0 \\; |xargs -0 chmod 0600 ")
          end
        else
          puts "Fixing #{options[:username]} Source files"
            system("find #{File.join(options[:basedir], options[:username], 'public_html')} -type d -name '.svn' -exec chown -R #{options[:username]}:#{options[:username]} {} \\;")
            system("find #{File.join(options[:basedir], options[:username], 'public_html')} -type d -name '.svn' -exec chmod 0700 {} \\;")
            system("find #{File.join(options[:basedir], options[:username], 'public_html')} -type d -name '.svn' -exec -exec find {} -type f -print0 \\; |xargs -0 chmod 0600 ")        end
        puts "Done with Source Control ownership"
      end

      if options[:fix_owner] 
        puts "Fixing file ownership"
        if accounts
          accounts.each do |account|
            puts "Fixing #{account} file ownership"
            system("find #{File.join(options[:basedir], account, 'public_html')} -exec chown -R #{account}:#{account} {} \\;")
          end
        else
          puts "Fixing #{options[:username]} file ownership"
          system("find #{File.join(options[:basedir], options[:username], 'public_html')} -exec chown -R #{options[:username]}:#{options[:username]} {} \\;")
        end
        puts "Done Fixing file ownership"
      end

      if options[:fix_files]
        puts "Fixing file permissions"
       if accounts
          accounts.each do |account|
            puts "Fixing #{account} file permissions"
            system("find #{File.join(options[:basedir], account, 'public_html')} -type f -perm 0777 -o -perm 0666 -exec chmod 0644 {} \\;")
          end
        else
          puts "Fixing #{options[:username]} file permissions"
          system("find #{File.join(options[:basedir], options[:username], 'public_html')} -type f -perm 0777 -o -perm 0666 -exec chmod 0644 {} \\;")
        end
        puts "Done Fixing file permissions"
      end

      if options[:fix_directories] 
        puts "Fixing directories permissions"
        if accounts
          accounts.each do |account|
            puts "Fixing #{account} directory permissions"
            system("find #{File.join(options[:basedir], account, 'public_html')} -perm 0777 -type d -exec chmod 0755 {} \\;")
          end
        else
          puts "Fixing #{options[:username]} directory permissions"
          system("find #{File.join(options[:basedir], options[:username], 'public_html')} -perm 0777 -type d -exec chmod 0755 {} \\;")
        end
        puts "Done Fixing directories permissions"
      end

      stdout.puts "All Done!"

      # raise "Path is required!" unless File.directory?(rails_app_path) or File.directory?("#{rails_app_path}/public")
      # FileUtils::mkdir_p(config_path)  
    end
  end
end