#!/usr/bin/env ruby18
# Copyright (c) 2008-2010, Edd Barrett <vext01@gmail.com>
# 
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 
# RBlatter
# $Id: rblatter,v 1.2 2010/10/20 13:15:55 edd Exp $
#
# == Synopsis
# RBlatter is a ruby script which allows you to add and subtract TeXmf
# subsets using the TeX Live TLPDB information. 
#
# == Usage
# rblatter [-h] [-v] -o dir -s svnroot eqn
#
# -a, --arch:
# 	specify the architecture to use for platform
# 	specific files. Should specify always.
# 	defaults to i386-openbsd
#
# -d, --dirs:
# 	add directories to conatin files to packing list.
# 	used for openbsd packing lists
#
# -h, --help:
#	show this message
#
# -n --no-missing-files:
# 	do not put missing files in PLIST
# 	(requires full texmf)
#
# -o --outdir:
# 	output directory
#
# -t, --svnroot SVNROOT:
#	root of texlive svn checkout (trunk/Master).
#
# -q, --quiet:
# 	do not show progress information.
#
# EQN: subset equation
# 	eg. +scheme-full,doc,run,src:+scheme-medium,run:-scheme-tetex,bin
#   	Means:
# 		take scheme-full's docfiles, runfiles and srcfiles.
#		add to this scheme-medium's runfiles.
#		subtract from these scheme-tetex's binfiles
#
#	Available file types are 'run', 'src', 'bin' and 'doc'.

$RBLATTER_V = "$OpenBSD: rblatter,v 1.2 2010/10/20 13:15:55 edd Exp $"
$QUIET = true;
$FILEPREFIX = ""
$ARCH = "i386-openbsd"
$MISSING_FILES = true
$ADD_DIRS = false

path = File.dirname $0
$: << path + "/lib"

require "getoptlong"
require "rdoc/usage"
require "subsetshaper"
require "eqnparser"

# Parse args
opts = GetoptLong.new(
	["--arch", "-a", GetoptLong::REQUIRED_ARGUMENT],
	["--dirs", "-d", GetoptLong::NO_ARGUMENT],
	["--help", "-h", GetoptLong::NO_ARGUMENT],
	["--no-missing-files", "-n", GetoptLong::NO_ARGUMENT],
	["--outdir", "-o", GetoptLong::REQUIRED_ARGUMENT],
	["--prefix", "-p", GetoptLong::REQUIRED_ARGUMENT],
	["--tlmaster", "-t", GetoptLong::REQUIRED_ARGUMENT],
	["--verbose", "-v", GetoptLong::NO_ARGUMENT]
)

opts.each do | opt, arg |
	case opt
	when '--arch'
		$ARCH = arg
	when '--dirs'
		$ADD_DIRS = true
	when '--help'
		RDoc::usage
		exit 1
	when '--no-missing-files'
		$MISSING_FILES = false
	when '--outdir'
		$OUTDIR = arg
	when '--prefix'
		$FILEPREFIX = arg
	when '--tlmaster'
		$TLMASTER = arg
	when '--verbose'
		$QUIET = false
	end
end

# Checks
if !defined? $TLMASTER then
	puts "*error: no --tlmaster | -t defined";
	exit 1
end

if !defined? $OUTDIR then
	puts "*error: no --outdir | -o  defined";
	exit 1
end

if ARGV.length != 1
	puts "*error: no subset equation specified."
	exit 0
end

# Yoink equation as whats left of command line
$EQN = ARGV.shift

# Go!
shaper = SubsetShaper.new

if !$QUIET then
	puts "Job done!"
end
