#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention docuementation generator
#  tools and configurations to build source documentations
#
# Copyright 2012-2014 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

"""
Univention source documentation
===============================

All source documentation ...

Under */usr/share/univention-doc/src* each package should add a
subdirectory with the source of the documentation. The directory should
also contain the *conf.py* containing the sphinx configuration.

"""
from optparse import OptionParser

import os
import shutil
import subprocess
import sys

DOC_SOURCE_DIR = '/usr/share/univention-doc/src'
DOC_OUTPUT_DIR = '/var/www/univention-doc'
SPHINX_BUILD = '/usr/bin/sphinx-build'

if __name__ == '__main__':
	parser = OptionParser( usage = '%prog [options] (install|uninstall) [documentations ...]' )
	parser.add_option( '-l', '--list', help = 'list available documentations', dest = 'list', action = 'store_true' )
	# parser.add_option( '-o', '--output', help = 'defines the output format of the documentation: A website (html) or a PDF document (latexpdf) [default: %default]', dest = 'output', action = 'store', default = 'html' )
	parser.add_option( '-q', '--quiet', help = 'no information output, but errors', dest = 'quiet', action = 'store_true', default = False )

	options, arguments = parser.parse_args()
	options.output = 'html'

	if options.list:
		for filename in os.listdir( DOC_SOURCE_DIR ):
			if os.path.isdir( os.path.join( DOC_SOURCE_DIR, filename ) ):
				print filename
		sys.exit( 0 )

	if not os.path.isfile( SPHINX_BUILD ):
		print >>sys.stderr, 'error: %s is required' % SPHINX_BUILD
		sys.exit( 1 )

	if not arguments:
		parser.error( 'no action given ' )
		sys.exit( 1 )

	if not arguments[ 0 ] in ( 'install', 'uninstall' ):
		parser.error( 'invalid action given' )
		parser.usage()
		sys.exit( 1 )

	if len( arguments ) < 2:
		parser.error( 'no documentations given' )
		parser.usage()
		sys.exit( 1 )

	if not options.output in ( 'html', 'latexpdf' ):
		parser.error( 'invalid output type' )
		sys.exit( 1 )

	if arguments[ 0 ] == 'uninstall':
		for doc in arguments[ 1: ]:
			shutil.rmtree( os.path.join( DOC_OUTPUT_DIR, doc ), True )
			if not options.quiet:
				print 'The documentation %s has been removed' % doc
	else:
		output_dir = os.path.join( DOC_OUTPUT_DIR )
		for doc in arguments[ 1: ]:
			ret = subprocess.call( [ SPHINX_BUILD,
									 '-b', options.output,
									 '-a',
									 '-E',
									 os.path.join( DOC_SOURCE_DIR, doc ),
									 os.path.join( DOC_OUTPUT_DIR, doc ) ] )
			if ret:
				print >>sys.stderr, 'error: failed to build documentation %s' % doc
			else:
				if not options.quiet:
					print 'The documentation %s has been build successfully' % doc
