#!/bin/sh
#
#  Script for launching applications with libjackasyn LD_PRELOAD
#
#


usage()
{
   echo Usage: jacklaunch command [OPTION] ...
   echo "   Launch application and redirect audio I/O to JACK server"
   echo
   exit 0
}



if test "x_$1" = "x_"
then
  usage
fi 

# Look if jackd is started

jackcnt=`ps -e | grep -c jackd`

PREFIX=@prefix@

LD_PRELOAD_OLD=""

if test "$LD_PRELOAD" != ""
then
 LD_PRELOAD_OLD=$LD_PRELOAD
 PRELOAD=$LD_PRELOAD:$PREFIX/lib/libjackasyn.so.0
else
 PRELOAD=$PREFIX/lib/libjackasyn.so.0
fi


if test -f $PREFIX/lib/libjackasyn.so.0 -a "$jackcnt" != "0"
then 
  export LD_PRELOAD=$PRELOAD
fi

# Check if the program is suid 

PROGNAME=`which $1`
if test "$PROGNAME" = ""; then
 echo
 echo jacklaunch: $1 no such command
 echo
 exit -1
fi

if test -u $PROGNAME
then
  echo 
  echo "                   SUID bit set on $1"
  echo 
  echo Because of security issues jacklaunch will not work with programs that
  echo have the SUID bit set. You can change that with \"chmod -s programname\".
  echo
  exit
fi

# Run the program

$@


if test "$LD_PRELOAD_OLD" != "" 
then
  export LD_PRELOAD=$LD_PRELOAD_OLD
else
  unset LD_PRELOAD
fi



