#! /bin/sh
# user-scripts/tao.  Generated from tao.in by configure.
#
# Tao - A software package for sound synthesis with physical models
# Copyright (C) 1993-1999 Mark Pearson
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# tao - compile and execute a Tao script
#
# (C) Mark Pearson 1998-99
#
# This shell script takes the name of a Tao script (minus the .tao suffix) as
# its argument and translates it into a valid C++ program which is then
# compiled and linked with the Tao library `libtao'. If the input file is
# called `infile.tao' then the executable is placed in a file called
# `infile.exe'. If translation, compilation and linking all succeed the
# executable is launched with the -g option which turns graphics mode on
# and causes Tao's instrument visualisation window to open. The synthesis
# proceeds as soon as the right cursor key is pressed. This is because the
# system starts off in `pause' mode so that the image can be rotated and
# zoomed before everything starts happening. See the User Manual for more
# details.

TAODIR=/usr/lib/x86_64-linux-gnu/taopm
CXX=g++
CPPFLAGS="-I/usr/lib/x86_64-linux-gnu/taopm/include"
LDFLAGS=" -lX11 -lXmu -lXi -lGL -lGLU -lpthread -lglut -lm"

echo "========================================"
echo "|    Tao (c) 1996-2000 Mark Pearson    |"
echo "| Sound Synthesis with Physical Models |"
echo "========================================"
echo

if test -z ${1}
then
    echo 'Usage: tao <name>   (compiles and launches script <name>.tao)'
    exit 1;
fi

if ! test -e ${1}.tao
then
    echo "Unable to find file \"${1}.tao\""
    exit 0
fi

echo "Parsing \"${1}.tao\""

taoparse ${1}.tao > ${1}.cc;

if grep PARSE_FAILED ${1}.cc >& /dev/null
then
    echo
    echo "Parsing failed for \"${1}.tao\""
    exit 1
else
    {
    echo "Compiling \"${1}.cc\" to make \"${1}.exe\"";
    if test -e ${1}.exe;
    then
	    rm -f ${1}.exe;
    fi

    if $CXX -O2 $CPPFLAGS -DSCRIPTNAME=\"${1}\" \
	-o ${1}.exe ${1}.cc -ltao $LDFLAGS 2> /tmp/tao.c++errors; then
	errors=no
    else
        errors=yes
    fi

# Find the root directory of the distribution.
    
    distdir=`tao-config --distdir`   

# If there were C++ compiler errors parse them with the given sed script
# to make them more comprehensible to the Tao user.

    if test "$errors" = "yes"; then
	echo
	sed -f $distdir/user-scripts/error.parse /tmp/tao.c++errors
	echo
	echo "Compilation failed for \"${1}.cc\""
        exit 1

# Otherwise remove the temporary C++ file generated by Tao and execute
# the generated executable.

    else 
        {
	rm -f ${1}.cc;
	echo "Launching \"${1}.exe\""
	echo
	exec ./${1}.exe -g;
	exit 0
	}
    fi
    }
fi



