#!/bin/sh

# live_ast: run asterisk from a newly-built copy with minimal changes.

# Copyright (C) 2007 Tzafrir Cohen <tzafrir.cohen@xorcom.com>
#
# 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

# This script allows you to install Asterisk into a subdirectory of 
# your source distribution and run it from there.
#
# Example usage:
#   ./configure
#   make
#   contrib/scripts/live_ast install
#   contrib/scripts/live_ast samples
#   contrib/scripts/live_ast run
#   contrib/scripts/live_ast run -r
#
# A standard debugging cycle of a code in a module:
#   # edit apps/app_skel.c
#   contrib/scripts/live_ast install
#   contrib/scripts/live_ast run -r 
#   # and in the CLI:
#   module unload app_skel.so
#   module load app_skel.so
#
# If you have external scripts that run asterisk, use the script 
# live/asterisk that is generated by the 'samples' command. In this case you
# should probably also rem-out the line 'astvarrundir' and maybe also 
# 'astetcdir' in live/etc/asterisk.conf .

BASE_DIR=$PWD/live
AST_CONF=$BASE_DIR/etc/asterisk/asterisk.conf
AST_BIN=$BASE_DIR/usr/sbin/asterisk
GDB_INIT=$BASE_DIR/gdbinit

if [ -r $BASE_DIR/live.conf ]; then . $BASE_DIR/live.conf; fi

# To use a libpri SVN directory (without running 'make install': make include
# a symlink to the current directory:
#ln -s . /path/to/checkout/of/libpri/include
# Be sure to run there 'make'. Then set in live.conf:
#CUSTOM_LIBPRI_PATH="/path/to.checkout/of/libpri"
if [ "$CUSTOM_LIBPRI_PATH" != '' ]; then
  CONFIGURE_PARAMS="$CONFIGURE_PARAMS --with-pri=$CUSTOM_LIBPRI_PATH"
  LD_PATH_EXTRA="$LD_PATH_EXTRA $CUSTOM_LIBPRI_PATH"
fi

# If you copied Zaptel to this system with 'live_zap rsync':
#CUSTOM_ZAPTEL_PATH="/tmp/live/usr"
# Otherwise you can use a trick similar to the above with a symlink to 
# 'zaptel'
if [ "$CUSTOM_ZAPTEL_PATH" != '' ]; then
  CONFIGURE_PARAMS="$CONFIGURE_PARAMS --with-tonezone=$CUSTOM_ZAPTEL_PATH"
  CONFIGURE_PARAMS="$CONFIGURE_PARAMS --with-zaptel=$CUSTOM_ZAPTEL_PATH"
  CONFIGURE_PARAMS="$CONFIGURE_PARAMS --with-zaptel_transcode=$CUSTOM_ZAPTEL_PATH"
  CONFIGURE_PARAMS="$CONFIGURE_PARAMS --with-zaptel_vldtmf=$CUSTOM_ZAPTEL_PATH"
  LD_PATH_EXTRA="$LD_PATH_EXTRA $CUSTOM_ZAPTEL_PATH"
fi

# gets rid of excessive spaces. Leves nothing if there were only white spaces:
LD_PATH_EXTRA=`echo $LD_PATH_EXTRA | tr ' ' :`

set_ld_env() {
  if [ "$LD_PATH_EXTRA$LD_LIBRARY_PATH" = '' ]; then return; fi

  LD_LIBRARY_PATH=`echo $LD_PATH_EXTRA $LD_PATH_EXTRA | tr ' ' :`
  export LD_LIBRARY_PATH
}


case "$1" in
configure)
  shift
  ./configure $CONFIGURE_PARAMS "$@"
  ;;
install)
  make install DESTDIR=$BASE_DIR
  ;;
samples)
  make samples DESTDIR=$BASE_DIR
  cat <<EOF >$AST_CONF
[directories]
; rem-out any of the following to use Asterisk's defaults:
astetcdir    => $BASE_DIR/etc/asterisk
astmoddir    => $BASE_DIR/usr/lib/asterisk/modules
astvarlibdir => $BASE_DIR/var/lib/asterisk
astdatadir   => $BASE_DIR/var/lib/asterisk
astagidir    => $BASE_DIR/var/lib/asterisk/agi
astrundir    => $BASE_DIR/var/run
astspooldir  => $BASE_DIR/var/spool/asterisk
astlogdir    => $BASE_DIR/var/log/asterisk
EOF
  cat <<EOF >$GDB_INIT
set args -C $AST_CONF -c
EOF
cat <<EOF >$BASE_DIR/asterisk
#!/bin/sh
# a wrapper to run asterisk from the "live" copy:
cd $PWD
exec $0 run "\$@"
EOF
  chmod +x $BASE_DIR/asterisk
  ;;
run)
  shift
  set_ld_env
  $AST_BIN -C $AST_CONF "$@"
  ;;
gdb)
  set_ld_env
  gdb -x $GDB_INIT $AST_BIN
  ;;
*)
  echo "$0: Usage:       	Equivalent of:"
  echo "$0 configure [params]	./configure [params]"
  echo "$0 install        	make install"
  echo "$0 samples        	make samples"
  echo "$0 run [params]   	asterisk [params]"
  echo "$0 gdb            	gdb asterisk"
  exit 1
  ;;
esac
