#!/bin/bash

prefix=/usr/local
texmf_prefix=

usage() {
	cat <<EOF
usage: $(basename "$0") [options]
  --prefix PREFIX       use PREFIX as prefix path for installation
  --texmf-prefix TDS    configure LaTeX-Make to be installed into the
                        TeX Directory Standard 'TDS'

EOF
}

error() {
	echo 1>&2 "Error: $1"
	echo 1>&2
	usage 1>&2
	exit 1
}


while [ $# != 0 ]; do
	case "$1" in
	--prefix=*) prefix="${1#--prefix=}" ;;
	--prefix) prefix="$2" ; shift;;
	--texmf-prefix=*) texmf_prefix="${1#--texmf-prefix=}" ;;
	--texmf-prefix) texmf_prefix="$2" ; shift;;
	--) break ;;
	--help|-h) usage ; exit 0 ;;
	-*) error "Unknown option '$1'" ;;
	*) error "Invalid argument '$1'" ;;
	esac
	shift
done

if [ $# != 0 ]; then
	error "Invalid argument '$1'"
fi


print-TDS() { # 1:starttoken
	declare -a loc
	declare -a home
	declare -a mid
	declare -a end
	declare -a all
	declare -a defs
	
	while read dir; do
		dir="$dir/"
		motif="//"
		rep="/"
		dir2="${dir/$motif/$rep}"
		while test "$dir2" != "$dir"; do
			dir="$dir2"
			dir2=${dir/$motif/$rep}
		done
		motif="/./"
		rep="/"
		dir2="${dir/$motif/$rep}"
		while test "$dir2" != "$dir"; do
			dir="$dir2"
			dir2=${dir/$motif/$rep}
		done
		dir2="$(echo "$dir" | sed -e 's,[^/]\+/\.\./,,')"
		while test "$dir2" != "$dir"; do
			dir="$dir2"
			dir2="$(echo "$dir" | sed -e 's,[^/]\+/\.\./,,')"
		done
		dir="${dir%/}"
		case "$dir" in
		"") ;;
		*"/."*) end=("${end[@]}" "$dir");;
		*"/local/"*|*"/opt/"*) loc=("${loc[@]}" "$dir");;
		*"/etc/"*) end=("${end[@]}" "$dir");;
		*"/var/"*) end=("${end[@]}" "$dir");;
		"${HOME}"*) home=("${home[@]}" "$dir");;
		*) mid=("${mid[@]}" "$dir");;
		esac
	done < <(kpsepath tex \
		| tr ':' '\n' \
		| sed -e 's/^!!//' -e 's,/*$,,' \
		| sed -e 's,/tex$,,p;d')
	
	trie() {
		local -a var
		eval 'var=( "${'"$1"'[@]}" )'
		for d in "${var[@]}"; do
			echo $d
		done | sort
	}
	
	OLD_IFS="$IFS"
	IFS="
	"
	all=( $(trie loc) $(trie mid) $(trie home) $(trie end) )
	defs=( $(trie loc) $(trie mid) )
	IFS="$OLD_IFS"
	
	TDS_default=
	for dir in "${defs[@]}"; do
		case "$dir" in
		*-local)
			if [ -z "$TDS_default" ]; then
				TDS_default="$dir"
			else
				if [ "$(echo "$dir" | wc -c)" -lt "$(echo "$TDS_default" | wc -c)" ]; then
					TDS_default="$dir"
				fi
			fi ;;
		esac
	done
	if [ -z "$TDS_default" ]; then
		TDS_default="${texmf_prefix:-$prefix/share/texmf}"
	fi
	if [ -z "$texmf_prefix" ]; then
		texmf_prefix="$TDS_default"
	fi
	local flag def
	for dir in "${all[@]}"; do
		flag=""
		def=""
		if [ "$dir" == "$TDS_default" ]; then
			def="*"
		fi
		if [ -e "$dir" ]; then
			if [ ! -w "$dir" ]; then
				flag="[R]"
			fi
		else
			flag="[U]"
		fi
		printf "%s%1s %3s %s\n" "$1" "$def" "$flag" "$dir"
	done 
	echo "### Default configuration" > config.mk
	echo "prefix=$prefix" >> config.mk
	echo "texmf_prefix=$TDS_default" >> config.mk
}

cat <<EOF
When installing LaTeX-Make, two paths must be chosen:
* prefix:
  The 'LaTeX.mk' file will be installed into \${prefix}/include. If you use a
  directory where your GNU-Make look for included files, you will be able to
  just put 'include LaTeX.mk' into your Makfiles.
    By default, LaTeX-Make use the '/usr/local' prefix, so 'LaTeX.mk' will
  be installed into '/usr/local/include'.

* texmf_prefix:
  This is the base TeX tree (TeX Directory Structure) in which all LaTeX files
  will be installed. By default, this is '\${prefix}/share/texmf' but a list
  of such directories configurated on this system is provided below.
  One of them should probably be used.

Valid texmf_prefix values on this system:
=========================================
EOF
print-TDS "  "
cat <<EOF

Flags meaning:
  [U]: unexistant but configurated TDS, will be created if choosen
  [R]: read-only TDS. Use 'sudo' for 'make install' is choosen
   * : default auto choice (not always the best one...)

You can now run:
================
    make
    make check #optional
    [sudo] make install

with PREFIX and TDS correctly choosen on the command line
or in the config.mk file

EOF
test ! -f config.mk || cat config.mk
echo

