#! /bin/sh

# A little wrapper script for 'fdesign -convert' that enables fdesign
# to be run from a directory other than that containing the .fd file
# and which deposits the generated .[ch] files in a user-specified directory.

test $# -eq 3 || {
    echo "Usage: `basename $0` <fdesign> <your_file.fd> <destdir>"
    exit 1
}


FDESIGN=$1
FDFILE=$2
DESTDIR=$3


test -x  ${FDESIGN} || {
    echo "Must first build fdesign."
    exit 1
}

test -r ${FDFILE} || {
    echo "Unable to find \"${FDFILE}\"."
    exit 1
}

test -d ${DESTDIR} -a -w ${DESTDIR} || {
    echo "Unable to write to directory \"${DESTDIR}\"."
    exit 1
}

# If the path to ${FDESIGN} is relative, make it absolute.
# Ie, if the path does not begin with '/', prepend it with '${PWD}/'.
FDESIGN=`echo ${FDESIGN} | sed "s,^\([^/]\),${PWD}/\1,"`

# Ditto for the path to ${DESTDIR}
DESTDIR=`echo ${DESTDIR} | sed "s,^\([^/]\),${PWD}/\1,"`

# ${FDESIGN} must be run from the directory containing ${FDFILE}.
SRCDIR=`dirname ${FDFILE}`
FDFILE=`basename ${FDFILE}`

cd ${SRCDIR}
${FDESIGN} -convert ${FDFILE}

# If ${SRCDIR} != ${DESTDIR} then
# move the generated .[ch] files to ${DESTDIR}.
mv -f `basename ${FDFILE} .fd`.[ch] ${DESTDIR}

# The end
