#!/bin/sh

set -e

[ "$1" = "configure" ] || exit 0
[ "$CARNET_SCRIPT_DEBUG" ] && set -vx

# Load CARNet functions
. /usr/share/carnet-tools/functions.sh

# uzmi realm i suffix
cp_get_ldap_realm || true
realm="$RET"
cp_get_ldap_suffix || true
suffix="$RET"

# ako je promjena suffixa, napravi novi config.php
if [ -f /etc/aosi-www/config.php ]; then
  if ! grep -q "$suffix" /etc/aosi-www/config.php; then
      echo "AAI: Backing up old configuration file /etc/aosi-www/config.php to /var/backups/aai/"
      cp_backup_conffile -p -d aai /etc/aosi-www/config.php aosi-config.php
      rm -f /etc/aosi-www/config.php
  fi
fi

# ako je upgrade sa verzije manje od 1:2.0.1, onda napravi skroz novi config.php
if dpkg --compare-versions "$2" lt 1:2.0.1 ; then
  if [ -f /etc/aosi-www/config.php ]; then
    echo "AAI: Backing up old configuration file /etc/aosi-www/config.php to /var/backups/aai/"
    cp_backup_conffile -p -d aai /etc/aosi-www/config.php aosi-config.php
    rm -f /etc/aosi-www/config.php
  fi
fi

if [ ! -f /etc/aosi-www/config.php ]; then
  cp -a /usr/share/aosi-www/templates/config.php /etc/aosi-www/
fi

if [ ! -L /etc/apache2/conf-available/aosi-www-aai.conf -a ! -f /etc/apache2/conf-available/aosi-www-aai.conf ]; then
  ln -sf /etc/aosi-www/apache.conf /etc/apache2/conf-available/aosi-www-aai.conf
fi

a2enconf aosi-www-aai --quiet

# sredi ldap realm
cp_check_and_sed "@REALM@" \
                 "s#@REALM@#$realm#g" \
                 /etc/aosi-www/config.php || true

# sredi ldap suffix
cp_check_and_sed "@SUFFIX@" \
                 "s#@SUFFIX@#$suffix#g" \
                 /etc/aosi-www/config.php || true

# sredi hostname
hostname="`hostname --fqdn`"
cp_check_and_sed "@HOSTNAME@" \
                 "s#@HOSTNAME@#$hostname#g" \
                 /etc/aosi-www/config.php || true

# sredi da wsdl ide sa diska
cp_check_and_sed "http://$hostname/aosi/aosi.wsdl" \
                 "s#http://$hostname/aosi/aosi.wsdl#/var/lib/aosi/www/aosi.wsdl#g" \
                 /etc/aosi-www/config.php || true

dpkg-statoverride --remove /var/lib/aosi-www/schema > /dev/null 2> /dev/null || true
dpkg-statoverride --remove /var/lib/aosi-www/schema/sifrarnici > /dev/null 2> /dev/null || true
dpkg-statoverride --remove /var/lib/aosi-www/inst_img > /dev/null 2> /dev/null || true
dpkg-statoverride --update --add www-data www-data 750 /var/lib/aosi-www/schema
dpkg-statoverride --update --add www-data www-data 750 /var/lib/aosi-www/schema/sifrarnici
dpkg-statoverride --update --add www-data www-data 750 /var/lib/aosi-www/inst_img

# schema fajlovi prebaceni u templates, da sve bude po deb.policy
schemadir=/var/lib/aosi-www/schema
templatesdir=/usr/share/aosi-www/templates/schema

# ubij stare xml fajlove ako je upgrade sa verzije starije od 1:2.10.0
if dpkg --compare-versions "$2" lt 1:2.10.0 ; then
  rm -f $schemadir/hrEduOrg.xml
  rm -f $schemadir/hrEduPerson.xml
  rm -f $schemadir/sifrarnici/hrEdu*.xml
fi

if [ ! -f $schemadir/hrEduPerson.xml ]; then
  cp -a $templatesdir/hrEduPerson.xml $schemadir/
fi
if [ ! -f $schemadir/hrEduOrg.xml ]; then
  cp -a $templatesdir/hrEduOrg.xml $schemadir/
fi
for i in 001 002 003 004 005 006 007 008 009 010 011; do
  if [ ! -f $schemadir/sifrarnici/hrEdu${i}.xml ]; then
    cp -a $templatesdir/sifrarnici/hrEdu${i}.xml $schemadir/sifrarnici/
  fi
done

# ubij obj fajlove ako je upgrade sa verzije starije od 1:2.12.0
if dpkg --compare-versions "$2" lt 1:2.12.0 ; then
  if [ -f $schemadir/schema.obj ]; then
    rm -f $schemadir/schema.obj
  fi
  if [ -f $schemadir/org_schema.obj ]; then
    rm -f $schemadir/org_schema.obj
  fi
  for i in 001 002 003 004 005 006 007 008 009 010 011; do
    if [ -f $schemadir/sifrarnici/hrEdu${i}.obj ]; then
      rm -f $schemadir/sifrarnici/hrEdu${i}.obj
    fi
  done
fi

if [ -d /var/lib/aosi-www/schema ]; then
  chown -R www-data:www-data /var/lib/aosi-www/schema
fi
if [ -d /var/lib/aosi-www/inst_img ]; then
  chown -R www-data:www-data /var/lib/aosi-www/inst_img
fi

# ako je starija verzije od 1:1.6.0, ubij aosi_proxy.php
# od verzije 1:1.6.0 se vise ne koristi
if dpkg --compare-versions "$2" lt 1:1.6.0 ; then
  if [ -f /usr/share/aosi-www/class/aosi_proxy/aosi_proxy.php ]; then
    rm -f /usr/share/aosi-www/class/aosi_proxy/aosi_proxy.php
  fi
  if [ -f /var/lib/aosi-www/aosi_proxy.php ]; then
    rm -f /var/lib/aosi-www/aosi_proxy.php
  fi
fi

# makni stari softlink
if [ -L /etc/apache2/conf.d/aosi-www-aai.conf ]; then
  rm -f /etc/apache2/conf.d/aosi-www-aai.conf
fi

# makni stari postrm, jer u slucaju purgeanja paketa aosi-www-aai-cn ne
# mora nuzno raditi ovaj paket...
if [ -f /var/lib/dpkg/info/aosi-www-aai-cn.postrm ]; then
  rm -f /var/lib/dpkg/info/aosi-www-aai-cn.postrm
fi

# stavi www-data u grupu aosi, zbog citanja action.log
addgroup aosi www-data || true

# reload apache2 
invoke-rc.d apache2 restart || true

#DEBHELPER#
