#!/bin/sh

# let's expose problems:
set -e

ZAP_VER=1.4.12.9.svn.r4649
PRI_VER=1.4.3
AST_VER=1.4.25
ADD_VER=1.4.8

usage() {
	echo "$0: Build/install Asterisk&Co. from source"
	echo ""
	echo "Usage: $0 [options]"
	echo ""
	echo "Options:"
	echo "  -l    Also download the iLBC codec."
	echo "  -q    Patch using quilt."
	echo "  -h    Just this screen."
	echo ""
}

# if the need arises: backup the old directory before extracting a
# tarball:
move_dir() {
  olddir="$1"
  if [ ! -d "$olddir" ]; then return; fi

  mkdir -p old
  parent=`mktemp -d "old/$olddir-XXXXXX"` || exit 1
  mv "$olddir" "$parent/"
}

# Save/restore a directory before patching it
backup_dir() {
  dir=$1
  backup=backup_$dir

  if [ ! -d $dir ]; then return; fi # don't add mess

  if [ -d $backup ]; then
    # it was run. Restore before patching $dir:
    rm -rf $dir
    cp -a $backup $dir
  else
    # ./compile.sh was not run before. save:
    cp -a $dir $backup
  fi
}

quilt_push_all() {
	./quilt.sh "$1" push -a
}

BRISTUFF_PATCH_CMD="${BRISTUFF_PATCH_CMD:-./apply-patches.sh}"
BRISTUFF_ILBC="${BRISTUFF_ILBC:-no}"

# A simple 'wget -c' won't work with Digium, as they keep providing a
# newer timestamp on files.
# 
# So we'll get the sha1 file
digium_download() {
	url="$1"
	file=`echo $url | rev | cut -d/ -f1 | rev`
	wget -c $url.sha1
	if [ ! -f "$file" ]; then
		wget -c "$url"
	fi

	# we have a file. Let's check if it is valid
	if sha1sum -c --status $file.sha1; then return; fi

	# If we're still here: bad download
	wget -c "$url"
}

while getopts 'cd:Dhilm:qt:' arg; do
	case "$arg" in
		c|d|D|i|m|t)
			# ignored (to allow running both from
			# ./install.sh)
			:;;
		l) BRISTUFF_ILBC="yes";;
		q) BRISTUFF_PATCH_CMD="quilt_push_all";;
		?) usage; exit 1 ;;
	esac
done

wget -c http://updates.xorcom.com/astribank/src/zaptel-${ZAP_VER}.tar.gz
#wget -c http://astimax.de/mirror/zaptel/zaptel-${ZAP_VER}.tar.gz
#digium_download http://downloads.asterisk.org/pub/telephony/zaptel/releases/zaptel-${ZAP_VER}.tar.gz
#wget -c http://astimax.de/mirror/libpri/libpri-${PRI_VER}.tar.gz
digium_download http://downloads.asterisk.org/pub/telephony/libpri/releases/libpri-${PRI_VER}.tar.gz
#wget -c http://astimax.de/mirror/asterisk-1.4/asterisk-${AST_VER}.tar.gz
digium_download http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-${AST_VER}.tar.gz
#wget -c http://astimax.de/mirror/asterisk-addons/asterisk-addons-${ADD_VER}.tar.gz
digium_download  http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-addons-${ADD_VER}.tar.gz

move_dir zaptel-${ZAP_VER}
move_dir libpri-${PRI_VER}
move_dir asterisk-${AST_VER}
move_dir asterisk-addons-${ADD_VER}
backup_dir cwain
backup_dir qozap
backup_dir zaphfc
backup_dir ztgsm

tar -xzf zaptel-${ZAP_VER}.tar.gz
tar -xzf libpri-${PRI_VER}.tar.gz
tar -xzf asterisk-${AST_VER}.tar.gz
tar -xzf asterisk-addons-${ADD_VER}.tar.gz

# removing old symlinks, if any:
rm -f zaptel libpri asterisk libgsmat
ln -s zaptel-${ZAP_VER} zaptel
ln -s libpri-${PRI_VER} libpri
ln -s asterisk-${AST_VER} asterisk
ln -s asterisk-addons-${ADD_VER} asterisk-addons
ln -s libgsmat-0.0.4 libgsmat


$BRISTUFF_PATCH_CMD zaptel patches/zaptel
$BRISTUFF_PATCH_CMD libpri patches/libpri
$BRISTUFF_PATCH_CMD asterisk patches/asterisk
$BRISTUFF_PATCH_CMD cwain patches/cwain
$BRISTUFF_PATCH_CMD qozap patches/qozap
$BRISTUFF_PATCH_CMD zaphfc patches/zaphfc
$BRISTUFF_PATCH_CMD ztgsm patches/ztgsm

if [ "$BRISTUFF_ILBC" = 'yes' ]; then
	cd asterisk
	./contrib/scripts/get_ilbc_source.sh
	cd ..
fi

if [ -x live_ast ]; then
	cd asterisk
	ln -sf ../live_ast .
	./live_ast conf-file
	cd ..
fi

# pretend those drivers are in the zaptel modules directory:
for mod in cwain qozap zaphfc ztgsm; do
	ln -sf ../../$mod/$mod.c ../../$mod/$mod.h zaptel/kernel/
done

echo "****************************************************"
echo "         Downloading and patching finished."
echo "****************************************************"
