#!/usr/bin/env ruby
#    Rubyripper - A secure ripper for Linux/BSD/OSX
#    Copyright (C) 2007 - 2010  Bouke Woudstra (boukewoudstra@gmail.com)
#
#    This file is part of Rubyripper. Rubyripper is free software: you can
#    redistribute it and/or modify it under the terms of the GNU General
#    Public License as published by the Free Software Foundation, either
#    version 3 of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>

# TODO fix the config script, to not default to /usr/local/share

###################################
# CHECK CORRECT ENVIRONMENT FIRST #
###################################

if ENV['PWD'] == nil
  puts "Your current working directory cannot be determined."
  puts "There are two possible reasons for this:"
  puts "1) You run configure with sudo (you shouldn't)."
  puts "2) Your shell needs to export the $PWD variable."
  exit()
end

###########################
# SETUP INSTALL VARIABLES #
###########################

$PREFIX='/usr/local'
$BINDIR='/bin'
$LOCALE='/share/locale'
$ICONDIR='/share/icons/hicolor/128x128/apps'
$DESKTOP='/share/applications'
$RUBYDIR= '/lib/rubyripper'

if RUBY_PLATFORM.include?('darwin')
  $INSTALL = 'install'
else
  $INSTALL = 'install -D'
end

$GTK3 = false
$CLI = false
$LANG = []
$LANG_SUPPORT = ["nl", "de", "fr", "hu", "ru", "es", "se", "bg", "it", "da"]

##################################
# START SOME DEPENDENCY CHECKING #
##################################
# Put the local lib directory on top of the ruby default lib search path
$:.insert(0, File.expand_path('../lib', __FILE__))

require 'rubyripper/base'
require 'rubyripper/system/dependency'

begin
  require 'gettext/tools'
  require 'gettext/tools/task'
rescue LoadError
  puts "Could not import gettext; skip language support..."
  $LANG_SUPPORT = []
end

####################
# SHOW THE OPTIONS #
####################

if ARGV.include?('--help') || ARGV.include?('-h') || ARGV.length == 0
  puts ""
  puts "--prefix=<destination_dir>  (default: #{$PREFIX})"
  puts ""
  puts "--enable-gtk3  (install the gtk3 frontend)"
  puts "--enable-cli  (install the cli frontend)"
  puts "--enable-lang-all  (install all locale files)"
  puts "--enable-lang=<xx>  (install specific locale file, separate with a comma)"
  puts ""
  puts "LANGUAGE SUPPORT"
  puts "--update-po (updates all po translation files, input = source code)"
  puts "--update-mo (update all binary translation files, input = po files)"
  puts ""
  exit()
end

##############################################
# Update the po-files (editable)             #
# Update the mo-files (binary)               #
# The mo files are created from the Makefile #
##############################################
def update_po
  if ($LANG_SUPPORT.size != 0)
    updatePoAndMoTask = GetText::Tools::Task.define do |task|
      task.domain = 'rubyripper'
      task.files = Dir.glob("lib/**/*.rb") + Dir.glob("bin/*")
      task.package_name = 'Rubyripper'
      task.package_version = "#{$rr_version}"
      task.xgettext_options = ["--copyright-holder", "BleskoDev",
                               "--copyright-year", "2021",
                               "--msgid-bugs-address", "bleskodev@ennumia.eu"]
    end
    updatePoAndMoTask.invoke
  end
end

def update_mo
  if ($LANG_SUPPORT.size != 0)
    updateMoTask = GetText::Tools::Task.define do |task|
      task.domain = 'rubyripper'
      task.files = Dir.glob("lib/**/*.rb") + Dir.glob("bin/*")
      task.package_name = 'Rubyripper'
      task.package_version = "#{$rr_version}"
      task.enable_po = false
    end
    updateMoTask.invoke
  end
end

def check_deps
  deps = Dependency.instance
  deps.verify(true, false)
end

ARGV.each do |argument|
  if argument[0,9] == "--prefix="
    $PREFIX = argument[9..-1]
  elsif argument == '--enable-gtk3'
    $GTK3 = true
  elsif argument == '--enable-cli'
    $CLI = true
  elsif argument == '--enable-lang-all'
    $LANG=$LANG_SUPPORT
  elsif argument[0,14] == '--enable-lang='
    argument[14..-1].split(',').each{|lang| if $LANG_SUPPORT.include?(lang) ; $LANG << lang end}
  elsif argument == "--update-po"
    update_po()
  elsif argument == "--update-mo"
    update_mo()
  end
end

unless ($GTK3 || $CLI)
  puts "You have to choose at least one frontend you want to install!"
  puts ""
  exit()
end

update_mo()
check_deps()

puts "Creating the Makefile..."
makefile = File.new("Makefile", "w+")
makefile.puts "#This Makefile is automatically created by configure"
makefile.puts ""
makefile.puts "BINDIR=#{$PREFIX}#{$BINDIR}"
makefile.puts "LOCALE=#{$PREFIX}#{$LOCALE}"
makefile.puts "ICONDIR=#{$PREFIX}#{$ICONDIR}"
makefile.puts "DESKTOP=#{$PREFIX}#{$DESKTOP}"
makefile.puts "RUBYDIR=#{$PREFIX}#{$RUBYDIR}"
makefile.puts ""
makefile.puts "install:"
Dir.glob("#{$RUBYDIR[1..-1]}/**/*.rb").each do |file|
  makefile.puts "\t#{$INSTALL} -m 644 #{file} $(prefix)$(DESTDIR)#{$PREFIX}/#{file}"
end

if $GTK3
  makefile.puts "\t#{$INSTALL} -m 755 bin/rubyripper_gtk3 $(prefix)$(DESTDIR)$(BINDIR)/rrip_gui"
  makefile.puts "\t#{$INSTALL} -m 644 #{$ICONDIR[1..-1]}/rubyripper.png $(prefix)$(DESTDIR)$(ICONDIR)/rubyripper.png"
  makefile.puts "\t#{$INSTALL} -m 644 #{$DESKTOP[1..-1]}/rubyripper.desktop $(prefix)$(DESTDIR)$(DESKTOP)/rubyripper.desktop"
end

if $CLI
  makefile.puts "\t#{$INSTALL} -m 755 -D bin/rubyripper_cli $(prefix)$(DESTDIR)$(BINDIR)/rrip_cli"
end

$LANG.each do |lang|
  makefile.puts "\t#{$INSTALL} -m 644 locale/#{lang}/LC_MESSAGES/rubyripper.mo $(prefix)$(DESTDIR)$(LOCALE)/#{lang}/LC_MESSAGES/rubyripper.mo"
end

makefile.puts ""
makefile.puts "uninstall:"
makefile.puts "\trm -rf $(prefix)$(DESTDIR)$(RUBYDIR)"

if $GTK3
  makefile.puts "\trm -f $(prefix)$(DESTDIR)$(BINDIR)/rrip_gui"
  makefile.puts "\trm -f $(prefix)$(DESTDIR)$(ICONDIR)/rubyripper.png"
  makefile.puts "\trm -f $(prefix)$(DESTDIR)$(DESKTOP)/rubyripper.desktop"
  makefile.puts "\tgtk-update-icon-cache -t $(prefix)$(DESTDIR)$(ICONDIR)"
end

if $CLI
  makefile.puts "\trm -f $(prefix)$(DESTDIR)$(BINDIR)/rrip_cli"
end

$LANG.each do |lang|
  makefile.puts "\trm -f $(prefix)$(DESTDIR)$(LOCALE)/#{lang}/LC_MESSAGES/rubyripper.mo"
end
makefile.puts ""
makefile.puts "clean:"
makefile.puts "\trm -rf locale/"
$LANG.each do |lang|
  makefile.puts "\trm -f po/#{lang}/rubyripper.edit.po"
  makefile.puts "\trm -f po/#{lang}/rubyripper.po.time_stamp"
end
makefile.puts "\trm Makefile"
makefile.puts "distclean:"
makefile.puts ""
makefile.close

puts "A summary of your settings:"
puts ""
puts "Using the following locations for install:"
puts "* Executables: #{$PREFIX}#{$BINDIR}"
puts "* Localization files: #{$PREFIX}#{$LOCALE}"
puts "* Icon file: #{$PREFIX}#{$ICONDIR}"
puts "* Desktop file: #{$PREFIX}#{$DESKTOP}"
puts "* Library files: #{$PREFIX}#{$RUBYDIR}"
puts ""

if $GTK3 == true; puts "Gtk3 frontend will be installed" end
if $CLI == true; puts "Cli frontend will be installed" end
unless $LANG.empty?; puts "Languages to be installed: #{$LANG.join(', ')}" end

puts ""
puts "You can now run make install"
puts "Make sure you've got the writing privileges"
puts ""
