# -*-python-*-
# vim: ts=4	

import handlerbase

class DocsHandler(handlerbase.HandlerBase):
	def __init__(self):
		pass

	def collect(self, amfile):
		# adding default targets
		amfile.insertTarget("all", "docs", phony=1)
		amfile.insertTarget("docs", "", phony=1)
		
		if not amfile.is_defined("KDE_DOCS"):
			return
		
		docs = amfile.definition_rec("KDE_DOCS")

		if amfile.is_defined("KDE_LANG"):
			kdelang = amfile.definition_rec('KDE_LANG')
			if not len(kdelang) == 1:
				print_error("%s: KDE_LANG is not a value but a(n empty) list\n" % amfile.filename)
			kdelang = kdelang[0]
		else:
			kdelang = ''
			
		# Either find the files in the directory (AUTO) or use
		# only the specified po files.
		if docs[0] == 'AUTO':
			appname = os.path.basename(amfile.subdir)
			if appname == 'en':
				print_error("%s: KDE_DOCS = AUTO relies on the directory name. Yours is 'en' - you most likely want something else, e.g. KDE_DOCS = myapp\n" % amfile.filename)
		else:
			appname = docs[0]

		rulef = amfile.rulef

		if len(docs) > 2 and docs[1] == '-':
			files = docs[2:]
		else:
			files = []
			for file in os.listdir(amfile.subdir):
				if (file in ['CVS', 'core', 'index.cache.bz2']) or file.startswith('Makefile') or file[0] == '.' or file[-1] == '~' or (file[0] == '#' and file[-1] == '#'):
					continue
				if os.path.isfile(amfile.subdir + "/" + file):
					files.append(file)

			if not len(files):
				return

		dep_files = []
		for file in files:
			dep_files.append(amfile.rulef.source + file)
						   
		if 'index.docbook' in files:

			deps = amfile.value_list("KDE_DOCS_DEPS")
			rulef.add_define('MEINPROC', '$(kde_bindir)/meinproc')
			rulef.add_define("KDE_XSL_STYLESHEET", "$(kde_datadir)/ksgmltools2/customization/kde-chunk.xsl")

			rulef.insertTarget(amfile.rulef.build + "index.cache.bz2",
							   ["$(KDE_XSL_STYLESHEET)"] + dep_files + deps,
							   '@if test -n "$(MEINPROC)"; then echo $(MEINPROC) --check --cache %sindex.cache.bz2 '
							   '%sindex.docbook; $(MEINPROC) --check --cache %sindex.cache.bz2 %sindex.docbook; fi'
							   % (rulef.build, rulef.source, rulef.build, rulef.source))
            
			rulef.insertTarget("docs", amfile.rulef.build + "index.cache.bz2", phony=1)
			
			rulef.insertTarget("install-docs-%s" % amfile.canon_subdir, 
							   [amfile.rulef.build + "index.cache.bz2", "install-nls-%s" % amfile.canon_subdir],
							   ["$(mkinstalldirs) $(DESTDIR)$(kde_htmldir)/%s/%s" % (kdelang, appname),
								"@if test -f %sindex.cache.bz2; then \\" % rulef.build,
								"  echo $(INSTALL_DATA) %sindex.cache.bz2 $(DESTDIR)$(kde_htmldir)/%s/%s/ ;\\" %
								(rulef.build, kdelang, appname),
								"  $(INSTALL_DATA) %sindex.cache.bz2 $(DESTDIR)$(kde_htmldir)/%s/%s/; \\" %
								(rulef.build, kdelang, appname),
								"fi",
								"-rm -f $(DESTDIR)$(kde_htmldir)/%s/%s/common" % (kdelang, appname),
								"$(LN_S) $(kde_libs_htmldir)/%s/common $(DESTDIR)$(kde_htmldir)/%s/%s/common" % (kdelang, kdelang, appname)], phony=1)
			
			rulef.insertTarget("uninstall-docs-%s" % amfile.canon_subdir, "", 
							   "-rm -rf $(kde_htmldir)/%s/%s" % (kdelang, appname), phony=1)
			rulef.insertTarget("clean-docs-%s" % amfile.canon_subdir, "",
							   "-rm -f " + rulef.build + "index.cache.bz2", phony=1)
			
			rulef.insertTarget("install-data", "install-docs-%s" % amfile.canon_subdir, phony=1)
			rulef.insertTarget("uninstall", "uninstall-docs-%s" % amfile.canon_subdir, phony=1)
			rulef.insertTarget("clean", "clean-docs-%s" % amfile.canon_subdir, phony=1)
		else:
			rulef.insertTarget("docs", dep_files, phony=1)
			
		rulef.insertTarget("install-data", "install-nls-%s" % amfile.canon_subdir, phony=1)
		rulef.insertTarget("uninstall", "uninstall-nls-%s" % amfile.canon_subdir, phony=1)
		rulef.insertTarget("install", "install-data", phony=1)

		lines = ["$(mkinstalldirs) $(DESTDIR)$(kde_htmldir)/%s/%s" % (kdelang, appname),
				 "@for base in " + string.join(files) + "; do \\",
				 "  echo $(INSTALL_DATA) %s$$base $(DESTDIR)$(kde_htmldir)/%s/%s/$$base ;\\" % (rulef.source, kdelang, appname),
				 "  $(INSTALL_DATA) %s$$base $(DESTDIR)$(kde_htmldir)/%s/%s/$$base ;\\" % (rulef.source, kdelang, appname),
				 "done"]
		
		if appname == 'common':
			lines + ["@echo \"merging common and language specific dir\" ;\\",
					 "if test ! -f $(kde_htmldir)/en/common/kde-common.css; then echo 'no english docs found in $(kde_htmldir)/en/common/'; exit 1; fi",
					 "@com_files=`cd $(kde_htmldir)/en/common && echo *` ;\\",
					 "cd $(DESTDIR)$(kde_htmldir)/%s/common ;\\" % kdelang,
					 "if test -n \"$$com_files\"; then for p in $$com_files ; do \\",
					 "  case \" " + string.join(files) + " \" in \\",
					 "    *\" $$p \"*) ;; \\",
					 "    *) test ! -f $$p && echo $(LN_S) ../../en/common/$$p $(DESTDIR)$(kde_htmldir)/%s/common/$$p && $(LN_S) ../../en/common/$$p $$p ;; \\" % kdelang,
					 "  esac ; \\",
					 "done ; fi ; true"]
			
		rulef.insertTarget("install-nls-%s" % amfile.canon_subdir, "", lines, phony=1)
		rulef.insertTarget("uninstall-nls-%s" % amfile.canon_subdir, "",
						   ["for base in %s; do \\" % string.join(files),
							"  rm -f $(DESTDIR)$(kde_htmldir)/%s/%s/$$base ;\\" % (kdelang, appname),
							"done"], phony=1)
		
      #rulef.insertTarget("distdir", "distdir-nls-%s" % amfile.canon_subdir)
      #rulef.insertTarget("distdir-nls-%s" % amfile.canon_subdir, "",
      #                   ["for file in $files; do \\",
      #                    "  cp %s$$file $(distdir); \\" % rulef.source,
      #                    "done"])


register_handler(DocsHandler())
