task default: [:install]
ConfigPath = (ENV["XDG_DATA_HOME"] or ENV["HOME"]+'/.config')

desc "Install (copy) config files"
task "install-files" do
  base = ["libs","modules","themes","widgets","rc.lua","desktop.conf"]
  sh "mkdir -p #{ConfigPath}/awesome"
  base.each { |f|
    cp_r "./#{f}", "#{ConfigPath}/awesome"
  }
end

desc "Install LuaPAM"
task "install-luapam" do
  unless File.exist? "#{ConfigPath}/awesome/libs/pam.so"
    sh "sh ./extra/install_luapam.sh"
    cp "./pam.so","#{ConfigPath}/awesome/libs/"
    cp_r "./pam","#{ConfigPath}/awesome/libs/"
    rm "./pam.so"
    rm_rf "./pam"
    rm_rf "./lua-pam" 
  else
    puts "LuaPAM already installed - skipping"
  end
end

desc "Force install config"
task "install-force" => ["install-files","install-luapam"] 

desc "Install config"
task :install do
  installed = true
  base = ["libs","modules","themes","widgets","rc.lua","desktop.conf"]
  (base+["libs/pam.so"]).each { |f|
    installed &= File.exist? "#{ConfigPath}/awesome/#{f}" 
  }
  if installed 
    puts "Baseline files already installed - skipping"
  else
    Rake::Task["install-force"].invoke
  end
end

desc "Build documentation"
task :doc do
  sh "ldoc ./.ldoc.lua"
end

desc "Install extras"
task "install-extra" do
  cp "./extra/udev/backlight.rules", "/etc/udev/rules.d"
  mkdir "#{ConfigPath}/autostart"
  begin
    sh "sudo cp /usr/share/applications/picom.desktop #{ConfigPath}/autostart/"
  rescue 
    puts "picom not installed - ignoring"
  else 
    cp "./extra/picom.conf", "#{ConfigPath}"
  end
  puts "Done! Reload awesome to complete installation"
end

desc "Uninstall from .config"
task :clean do
  rm_rf "#{ConfigPath}/awesome"
end 

desc "Wipe configuration and reinstall from scratch"
task reinstall: [:clean,:install]

