# This makefile depends on the sgmltools-lite program.
#
# This makefile requires document filenames to follow this convention:
# document_title.language_code.sgml 
# The last two parts of the filename must be .language_code.sgml.  Anything
# you put before that is considered to be the title or similar name of the 
# document. Examples: 
#   LEGAL: newbiedoc.is.cool.en.sgml
# ILLEGAL: newbiedoc.is.cool.sgml.en (language code in wrong position)
# ILLEGAL: newbiedoc.is.cool.sgml (ommitted language code)
# ILLEGAL: newbiedoc_is_cool-en.sgml (must be a period before language code)
#
# To use this file copy it into the directory where the sgml file lives, rename
# it to "makefile", and edit the source, path_to_stylesheets, and
# newbiedoc_webcvs_path variables for your system.  Then simply type make.  If
# you're just starting the doc you can use the edit target (by typing "make
# edit" to generate a quick single page html for proofreading. This works great
# from inside vim.
#
# If you have the web cvs module installed and do commits regulary you can use
# the copy2cvs_dir target to make copying your rendered multi-page documents
# and tarballs over to that cvs tree easier.
#
# This package depends on sgmltools-lite and realpath.  There are other
# dependencies but these are ones you may be missing.
#
# $Log: sample_makefile,v $
# Revision 1.5  2002/05/03 03:10:46  jwgoerz
# I think its finally done
#
# Revision 1.4  2002/05/03 02:07:40  jwgoerz
# Thanks Romain, it works for real now, really, I mean it this time ;)
#
#

##############################################
# Edit these variables for your local system #
##############################################
# Do NOT use trailing slashes for directories.  You can leave the
# newbiedoc_webcvs_path unset if you are not using the copy2cvs_dir target.
source = 
path_to_stylesheets = 
newbiedoc_webcvs_path = 

#####################################
# Shouldn't need to edit below here #
#####################################
# Name mangling common to all formats
lang_field_pos := $(shell echo -n ${source} | sed 's/[^\.]//g' | wc -c)
language_code := $(shell echo ${source} | cut -d . -f${lang_field_pos})
basename_end_field_pos := $(shell echo ${lang_field_pos} - 1 | bc)

ifeq (${basename_end_field_pos},1)
base_filename := $(shell echo ${source} | cut -d . -f1)
else
base_filename := $(shell echo ${source} | cut -d . -f-${basename_end_field_pos})
endif

# Name mangling for single html output files
html_output_filename = ${base_filename}.${language_code}.html
html_new_filename = ${base_filename}.html.${language_code}

#html_output_filename = ${base_filename}.${language_code}.html
#html_new_filename = ${base_filename}.html.${language_code}

# Name mangling for tarballs
tar_source := $(shell echo ${source} | cut -d . -f-${basename_end_field_pos})
tar_new_filename = ${base_filename}.tgz.${language_code}

# For copying output files into the web cvs module
current_path := $(shell realpath .)


all: onehtml html tarball

onehtml:
# Debugging output, tab this in one position and it'll print to stdout
#lang field pos ${lang_field_pos}
#language code = ${language_code}
#basename end field = ${basename_end_field_pos}
#base filename = ${base_filename}
#tar source = ${tar_source}
#tar filename = ${tar_new_filename}
	sgmltools -b onehtml -s ${path_to_stylesheets}/newbiedoc-onehtml.dsl ${source}
	mv ${html_output_filename} ${html_new_filename}

html:
	sgmltools -b html -s ${path_to_stylesheets}/newbiedoc-html.dsl ${source}

# This one doesn't take care of images yet.  Haven't figured out a sane way to
# do that.  This also depends on the fact that sgmltools-lite generates
# multi-page files in a subdirectory named after the source file without the
# sgml extension.
tarball:
	mv ${tar_source}.${language_code} ${tar_source}
	tar czf ${tar_new_filename} ${tar_source}
	-rm -rf ${tar_source}

# I use this target to copy tarballs to the web cvs module.  It helps keep me 
# from making simple mistakes.  Be sure to test/proofread your tarballs/docs 
# before you use this or you may overwrite something you don't want to in your 
# local copy of the web cvs module.  This takes care of the mult-page html and
# tarballs by copying the tarball over, untarring it, and then moving the tarball
# into the directory it just created.  I haven't figured out how to deal with the
# single page html yet.  They have different subdirectories depending on which
# section they go in to.
copy2cvs_dir:
	# Because we are running a shell command we need this comment here or
	# it always defaults to "up to date" and won't do what we want.  You
	# can ignore this message ;-)
	$(shell mv ${current_path}/${tar_new_filename} ${newbiedoc_webcvs_path}/tutorials; \
	cd ${newbiedoc_webcvs_path}/tutorials/; \
	tar xzf ${tar_new_filename}; mv ${tar_new_filename} ${tar_source}/)
	
# I use edit so I can occasionally call make edit and proofread the doc.  It's
# easier to proofread the html rather than the sgml source.  This target does
# the same thing as onehtml but is shorter to type :)
edit:
	sgmltools -b onehtml -s ${path_to_stylesheets}/newbiedoc-onehtml.dsl ${source}
	mv ${html_output_filename} ${html_new_filename}
