#!/bin/sh

#
# build-struct - script used in conjuction with Makefile of 'office'
#                Creates directory structure for typeseting Postscript
#                and PDF document from one *.tex file
#
# Usage: ./build-struct <directory>
#
# Developed by Lubomir Host 'rajo' <rajo AT platon.sk>
# Copyright (c) 2003 Platon SDG
# Licensed under terms of GNU General Public License.
# All rights reserved.
#
# Changelog:
# 30/01/2003 - created
#

# $Platon: Platon.SK/office/_scripts/build-struct,v 1.9 2005/05/30 21:58:20 rajo Exp $

usage()
{
	echo "Usage:"
	echo
	echo "$0 '<PostScript|PDF|HTML>'"
	echo
}

test_environ()
{
	for i in $*; do
		if [ -z "${!i}" ]; then
			echo "Empty \$$i, did you specify it in Makefile and export it?"
			exit 1
		fi
	done

}

make_symlinks()
{
	for i in $DEPEND_all; do
		rm -f "`basename $i`"
		ln -s -f "../$i" "`basename $i`" \
			|| cp "../$i" "`basename $i`"
	done
}

if [ "$#" -lt "1" ]; then
	usage;
fi

test_environ \
	DEPEND_all \
	OUTPUT \
	TOP_DIR

echo "Building $1"
if [ "X$1" = "XPostScript" ]; then
	
	test_environ ps__INPUT ps__OUTPUT
	test -d PostScript || mkdir -p PostScript
	cd PostScript \
		&& make_symlinks \
			&& for i in $ps__INPUT; do
				ln -s -f "../$i" "$i"
			done

elif [ "X$1" = "XPDF" ]; then

	test_environ pdf_INPUT pdf_OUTPUT
	test -d PDF || mkdir -p PDF
	cd PDF \
		&& make_symlinks \
			&& for i in $pdf_INPUT; do
				ln -s -f "../$i" "$i"
			done

elif [ "X$1" = "XHTML" ]; then

	test_environ htm_INPUT htm_OUTPUT
	test -d HTML || mkdir -p HTML
	cd HTML \
		&& make_symlinks \
			&& for i in $htm_INPUT; do
				ln -s -f "../$i" "$i"
			done

elif [ "X$1" = "XRTF" ]; then

	test_environ rtf_INPUT rtf_OUTPUT
	test -d RTF || mkdir -p RTF
	cd RTF \
		&& make_symlinks \
			&& for i in $rtf_INPUT; do
				ln -s -f "../$i" "$i"
			done

else
	echo "I don't know, what do you want with parameter '$1'..."
	exit 1
fi

