#!/bin/sh

# Check if we are running on a supported linux distribution.
if [ -f /etc/redhat-release -a -f /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 ]
then
  DIST=CentOS`rpm -q centos-release | cut -d- -f3`
else
  echo Distribution is not supported - Exit
fi
export DIST

echo "run depmod -a, 2b sure that zaptel loads" 
depmod -a

# Update pear channel data.
if [ -x /usr/bin/pear ]
then
        /usr/bin/pear channel-update pear.php.net
fi

echo -ne "Running setup.sh scripts..." 
for i in `ls /usr/local/astium/setup/config|sort`
do
        OLDPWD=`pwd`
        echo "$i "
        cd /usr/local/astium/setup/config/$i
        ./setup.sh -y
        echo $i
        cd $OLDPWD
done 

echo

#
# Install astium database.
#
echo "Install Astium Databases..."
su -c /usr/local/astium/setup/db/scripts/setup-db.sh postgres
# must restart httpd now, because it uses persistent connections, and the database went down for a moment..
service httpd restart

# set current version of the database
DBVERSION=`rpm --qf "%{RELEASE}\n" -q astium-db2`
QUERY="update astglobals set value = '$DBVERSION' where name = 'dbversion'"
echo $QUERY
echo $QUERY | psql -t -U astium

echo "create key for company number 1."
/usr/local/astium/sbin/astium_set_company_key 1

echo "create key for company number 3."
/usr/local/astium/sbin/astium_set_company_key 3

# Create all phonetypes in database - do it this way since the new way doesn't work yet
/usr/local/astium/setup/db/scripts/new-phonetype.php

# Scan for Telephony cards and insert them into the database
/sbin/lspci -n | awk '{ print $3; }' | while read pciid
do
  #echo pciid=$pciid
  QUERY=""
  case $pciid in
  1396:08b4)  QUERY="insert into card_quadbri (name) values ('Junghanns Quad BRI')"  ;;
  1396:16b8)  QUERY="insert into card_octobri (name) values ('Junghanns Octo BRI')"  ;;
  d161:0120)  QUERY="insert into card_te120p (name) values ('TE120P ISDN-30')"  ;;
  d161:0205)  QUERY="insert into card_te205p (name) values ('TE205P Dual ISDN-30 5.0V')"  ;;
  d161:0210)  QUERY="insert into card_te210p (name) values ('TE210P Dual ISDN-30 3.3V')" ;;
  d161:0220)  QUERY="insert into card_te210p (name) values ('TE220P Dual ISDN-30 PCIe')" ;;
  d161:0405)  QUERY="insert into card_te405p (name) values ('TE405P Quad ISDN-30 5.0V')"  ;;
  d161:0406)  QUERY="" ;;
  d161:0410)  QUERY="insert into card_te410p (name) values ('TE410P Quad ISDN-30 3.3V')"  ;;
  d161:0411)  QUERY="" ;;
  d161:0420)  QUERY="insert into card_te410p (name) values ('TE410P Quad ISDN-30 PCIe')"  ;;
  d161:0800)  QUERY="insert into card_tdm800p (name) values ('TDM800P 8-port analog')"  ;;
  d161:2400)  QUERY="insert into card_tdm2400p (name) values ('TDM2400P 24-port analog')"  ;;
  d161:3400)  QUERY="";;  # Wildcard TC400P transcoder base card
  d161:8000)  QUERY="insert into card_te120p (name) values ('TE121 ISDN-30 PCIe')"  ;;
  d161:8001)  QUERY="insert into card_te120p (name) values ('TE122 ISDN-30')"  ;;
  d161:8002)  QUERY="insert into card_tdm800p (name) values ('AEX800 8p analog PCIe)')"  ;;
  d161:8003)  QUERY="";;
  d161:8005)  QUERY="insert into card_tdm400p (name) values ('TDM400P 4p analog card')"  ;;
  d161:8006)  QUERY="";;
  d161:b410)  QUERY="";;  # Wildcard B410 quad-BRI card
  esac
  #if [ ! -z "$QUERY" ]
  #then
  #  echo $QUERY
  #  echo $QUERY | psql -t -U astium
  #fi
done

# guess the values of public_ip and register_ip
. /usr/local/astium/bin/examine-network-setup

# Try to gues the register and public adres during astium install.
register_ip=`echo $IP_ALIASES | awk '{print $1;}'`

if [ -z "$register_ip" ]
then
  register_ip=`echo $IP_INTF_INTERNAL | awk '{print $1;}'`
  public_ip=`echo $IP_INTF_EXTERNAL | awk '{print $1; }'`
fi

if [ -z "$register_ip" ]
then 
  register_ip=`echo $IP_INTF_EXTERNAL | awk '{print $1;}'`
fi

if [ "$register_ip" = "$public_ip" ]
then 
  public_ip=""
fi

# If we have a register_ip ipaddress insert it into the database.
if [ ! -z "$register_ip" ]
then
  QUERY="update astglobals set value = '$register_ip' where name = 'register_ip'"
  echo $QUERY
  echo $QUERY | psql -t -U astium
fi

# If we have a public_ip ipaddress insert it into the database.
if [ ! -z "$public_ip" ]
then
  QUERY="update astglobals set value = '$public_ip' where name = 'public_ip'"
  echo $QUERY
  echo $QUERY | psql -t -U astium
fi

# create the configuration (including zaptel)
QUERY="insert into activate(action, type, which, status) values('module', 'all', '0', 'QUEUED')"
echo $QUERY
echo $QUERY | psql -t -U astium
QUERY="insert into activate(action, type, which, status) values('module', 'zaptel', '0', 'QUEUED')"
echo $QUERY
echo $QUERY | psql -t -U astium

# Now start it all up
service astiumd restart
service asterisk restart
service astcfgd restart

#EOF

