#!/usr/bin/env python

# prep-images
#
# Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
#
# This file is part of prep-images (hereafter referred to as "this program").
# See http://www.waxworlds.org/edam/software/gtk/prep-images for more
# information.
#
# 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 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/>.


# @PACKAGE_NAME@ version @PACKAGE_VERSION@

import sys, os
from os.path import join, abspath, exists
import getopt


# check for a development source directory and use that if it's there
PACKAGE_DIR = join( abspath( sys.path[ 0 ] ), "..", "lib" )
if exists( join( PACKAGE_DIR, "prep_images", "prefs_dialog.py" ) ):
	print >> sys.stderr, "!!! using development packages !!!"
	sys.path.insert( 0, PACKAGE_DIR )
else:
	del PACKAGE_DIR


def usage( error = True ):
	print >> sys.stderr, 'Try "@PACKAGE_NAME@ --help" for help'
	exit( 1 )


def help():
	print "Usage: @PACKAGE_NAME@ [OPTION]... FILE..."
	print
	print "Prepare the specified image FILEs for publishing."
	print
	print "Options:"
	print "     --help     display this help and exit"
	print "     --version  output version information and exit"
	exit( 0 )


def version():
	print "@PACKAGE_NAME@ @PACKAGE_VERSION@"
	print
	print "Copyright (C) 2011 to 2013 Tim Marston"
	print "License GPLv3+: GNU GPL version 3 or later " + \
	    "<http://gnu.org/licenses/gpl.html>"
	print "This is free software: you are free to change and " + \
	    "redistribute it."
	print
	print "For more information, including documentation, see the " +\
	    "project website, at"
	print "<@PACKAGE_URL@>. Please report bugs to <@PACKAGE_BUGREPORT@>."
	exit( 0 )


# handle command line
try:
	opts, args = getopt.gnu_getopt(
		sys.argv[ 1: ], "",
		[ "help", "version" ] )
except getopt.GetoptError as e:
	print str( e )
	usage()

# process options
for opt, args in opts:
	if opt == "--help":
		help()
	elif opt == "--version":
		version()
	else:
		print >> sys.stderr, "unknown option"
		usage()

# check for files
if len( args ) == 0:
	print >> sys.stderr, "no files specified"
	usage()

# try to detect no DISPLAY
if os.getenv( 'DISPLAY', '' ) == '':
	print >> sys.stderr, 'could not open display'
	exit( 1 )

# we only now import the main window stuff, because it pulls in PyGTK which
# complains if it can't connect to a display
from prep_images.main_window import *

# process each image in turn
for filename in args:
	MainWindow( filename ).main()
