#!/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 .
#
####################### Begin Samples
# optional environment variables. Set those in live/live.conf or in 
# your envirnment.
#
# LIVE_AST_LIBPRI_PATH:
# 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:
#LIVE_AST_LIBPRI_PATH="/path/to.checkout/of/libpri"
#
# LIVE_AST_ZAPTEL_PATH:
# If you copied Zaptel to this system with 'live_zap rsync':
#LIVE_AST_ZAPTEL_PATH="/tmp/live/usr"
# Otherwise you can use a trick similar to the above with a symlink to 
# 'zaptel'
#
# LIVE_AST_LD_PATH_EXTRA: 
# space-separated or colon-separated directories to add to 
# LD_LIBRARY_PATH. It is added before any existing components of 
# LD_LIBRARY_PATH.
#LIVE_AST_LD_PATH_EXTRA="$HOME/lib:$HOME/libtest /opt/testapp"
#
# LIVE_AST_CONFIGURE_PARAMS:
# Extra arameters to pass to ./configure.
#LIVE_AST_CONFIGURE_PARAMS="--without-gtk2"
#
# LIVE_AST_FORCE_DEF_CONF:
# If set to a non-empty value, always regenerate menuselect config.
# This avoids emenselect's nag about "configuration has changed" that 
# happens occasionally after an svn update.
#LIVE_AST_FORCE_DEF_CONF=yes
####################### End Samples

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

if [ -r "$LIVE_CONF" ]; then . "$LIVE_CONF"; fi

if [ "$LIVE_AST_LIBPRI_PATH" != '' ]; then
  LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-pri=$LIVE_AST_LIBPRI_PATH"
  LIVE_AST_LD_PATH_EXTRA="$LIVE_AST_LD_PATH_EXTRA $LIVE_AST_LIBPRI_PATH"
fi

if [ "$LIVE_AST_ZAPTEL_PATH" != '' ]; then
  LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-tonezone=$LIVE_AST_ZAPTEL_PATH"
  LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel=$LIVE_AST_ZAPTEL_PATH"
  LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel_transcode=$LIVE_AST_ZAPTEL_PATH"
  LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel_vldtmf=$LIVE_AST_ZAPTEL_PATH"
  LIVE_AST_LD_PATH_EXTRA="$LIVE_AST_LD_PATH_EXTRA $LIVE_AST_ZAPTEL_PATH"
fi

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

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

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


case "$1" in
configure)
  shift
  ./configure $LIVE_AST_CONFIGURE_PARAMS "$@"
  if [ "$LIVE_AST_FORCE_DEF_CONF" != '' ]; then
    rm -f menuselect.makeopts
  fi
  ;;
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"
  # Generate a sample config file for live_ast itself:
  sed -n -e '/^#* Begin Samples/,/^#* End Samples/p' "$0" >"$LIVE_CONF"
  ;;
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
