require 'rake/clean'
require 'tempfile'

# Build Constants
REPO_ROOT = File.dirname(__FILE__)
BUILD_DIR = File.join(REPO_ROOT, "build")
REPORT_DIR = File.join(REPO_ROOT, "reports")

# Packaging constants
DEPLOY_DIR = "/opt/wwc"
PACKAGE_NAME = "mitx"
LINK_PATH = "/opt/wwc/mitx"
PKG_VERSION = "0.1"
COMMIT = (ENV["GIT_COMMIT"] || `git rev-parse HEAD`).chomp()[0, 10]
BRANCH = (ENV["GIT_BRANCH"] || `git symbolic-ref -q HEAD`).chomp().gsub('refs/heads/', '').gsub('origin/', '')
BUILD_NUMBER = (ENV["BUILD_NUMBER"] || "dev").chomp()

if BRANCH == "master"
    DEPLOY_NAME = "#{PACKAGE_NAME}-#{BUILD_NUMBER}-#{COMMIT}"
else
    DEPLOY_NAME = "#{PACKAGE_NAME}-#{BRANCH}-#{BUILD_NUMBER}-#{COMMIT}"
end
PACKAGE_REPO = "packages@gp.mitx.mit.edu:/opt/pkgrepo.incoming"

NORMALIZED_DEPLOY_NAME = DEPLOY_NAME.downcase().gsub(/[_\/]/, '-')
INSTALL_DIR_PATH = File.join(DEPLOY_DIR, NORMALIZED_DEPLOY_NAME)

# Set up the clean and clobber tasks
CLOBBER.include(BUILD_DIR, REPORT_DIR, 'cover*', '.coverage')
CLEAN.include("#{BUILD_DIR}/*.deb", "#{BUILD_DIR}/util")

def select_executable(*cmds)
    cmds.find_all{ |cmd| system("which #{cmd} > /dev/null 2>&1") }[0] || fail("No executables found from #{cmds.join(', ')}")
end


task :default => [:pep8, :pylint, :test]

directory REPORT_DIR

desc "Run pep8 on all of djangoapps"
task :pep8 => REPORT_DIR do
    sh("pep8 --ignore=E501 djangoapps | tee #{REPORT_DIR}/pep8.report")
end

desc "Run pylint on all of djangoapps"
task :pylint => REPORT_DIR do
    Dir.chdir("djangoapps") do
        Dir["*"].each do |app|
            sh("pylint -f parseable #{app} | tee #{REPORT_DIR}/#{app}.pylint.report")
        end
    end
end

desc "Run all django tests on our djangoapps"
task :test => REPORT_DIR do
    ENV['NOSE_XUNIT_FILE'] = File.join(REPORT_DIR, "nosetests.xml")
    django_admin = ENV['DJANGO_ADMIN_PATH'] || select_executable('django-admin.py', 'django-admin')
    sh("#{django_admin} test --settings=envs.test --pythonpath=. $(ls djangoapps)")
end

desc "Start a local server with the specified environment (defaults to dev). Other useful environments are devplus (for dev testing with a real local database)"
task :runserver, [:env] => [] do |t, args|
    args.with_defaults(:env => 'dev')
    django_admin = ENV['DJANGO_ADMIN_PATH'] || select_executable('django-admin.py', 'django-admin')
    sh("#{django_admin} runserver --settings=envs.#{args.env} --pythonpath=.")
end

task :package do
    FileUtils.mkdir_p(BUILD_DIR)
    
    Dir.chdir(BUILD_DIR) do

        postinstall = Tempfile.new('postinstall')
        postinstall.write <<-POSTINSTALL.gsub(/^\s*/, '')
        #! /bin/sh
        set -e
        set -x

        chown -R makeitso:makeitso #{INSTALL_DIR_PATH}

        service gunicorn stop
        rm -f #{LINK_PATH}
        ln -s #{INSTALL_DIR_PATH} #{LINK_PATH}
        chown makeitso:makeitso #{LINK_PATH}
        service gunicorn start
        POSTINSTALL
        postinstall.close()
        FileUtils.chmod(0755, postinstall.path)

        args = ["fakeroot", "fpm", "-s", "dir", "-t", "deb",
            "--after-install=#{postinstall.path}",
            "--prefix=#{INSTALL_DIR_PATH}",
            "--exclude=build",
            "--exclude=rakefile",
            "--exclude=.git",
            "--exclude=**/*.pyc",
            "--exclude=reports",
            "-C", "#{REPO_ROOT}",
            "--provides=#{PACKAGE_NAME}",
            "--name=#{NORMALIZED_DEPLOY_NAME}",
            "--version=#{PKG_VERSION}",
            "-a", "all",
            "."]
        system(*args) || raise("fpm failed to build the .deb")
    end
end

task :publish => :package do
    sh("scp #{BUILD_DIR}/#{NORMALIZED_DEPLOY_NAME}_#{PKG_VERSION}*.deb #{PACKAGE_REPO}")
end
