#!/usr/bin/env ruby

# install.rb   May 2002
#
# Copyright (c) 2002 by Matthias Veit <matthias_veit@yahoo.de>
# 
# This program 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 2, 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
# 02111-1307, USA.

=begin

Simple Log Viewer.

= Introduction

This script installs rlogview.

= Usage

       ruby install.rb
              --install_dir <dir> - the directory to install
              --help              - get help

=end

require 'getoptlong'
require 'ftools'
require 'find'
require 'rbconfig'

SOURCE_DIR = 'objectteam'
LIBDIR = 'objectteam'

def parse_args
  install_dir = nil
  help = false
  g = GetoptLong.new(
        ['--install-dir', '-i', GetoptLong::REQUIRED_ARGUMENT],
        ['--help', '-h', GetoptLong::NO_ARGUMENT]
  )
  g.each { |opt, arg|
    if (opt == '--install-dir')
      install_dir = arg
    elsif (opt == '--help')
      help = true
    else
      help = true
    end
  }
  return [install_dir, help]
end

def install_dir
  Config::CONFIG['prefix']
end


def usage()
  puts "usage: #{$0}\n"+
       "       --install-dir <dir> - the directory to install (default= #{install_dir()} )\n"+
       "       --help              - this usage\n"
end

def adjustpath(installdir)
  tmp = "rlogview_tmp"
  rlogview = File.readlines(File.join("src", "rlogview.rb"))
  file = File.open(tmp, "w+")
  file.write("#!/usr/bin/env ruby\n")
  file.write("GladeFile = \""+File.join(installdir, "share", "rlogview", "log4rview.glade")+"\"\n")
  file.write(rlogview)
  file.close
  return tmp
end

def main()
  instdir, help = parse_args()
  if (help)
    #some informative description
    usage
  else
    begin
      #install directory
      instdir = (instdir or install_dir())
      log4rdir = File.join(Config::CONFIG["sitelibdir"], "log4r", "outputter")
      #make some directories
      File.makedirs(File.join(instdir, "share", "rlogview"))
      File.makedirs(File.join(instdir, "bin"))
      File.makedirs(log4rdir)
      #set path in executable
      f = adjustpath(instdir)
      #install executable
      File.install(f, File.join(instdir, "bin", "rlogview"), 0555, true) 
      #install glade file
      File.install(File.join("src", "log4rview.glade"), File.join(instdir, "share", "rlogview", "log4rview.glade"), 0444, true) 
      #install outputter
      File.install(File.join("src", "drbviewoutputter.rb"), File.join(log4rdir, "drbviewoutputter.rb"), 0444, true)
      File.install(File.join("src", "rompviewoutputter.rb"), File.join(log4rdir, "rompviewoutputter.rb"), 0444, true)
    ensure
      File.unlink(f) if (f and File.exists?(f))
    end
  end
end
  
main()
