+#!/bin/sh
+
+set -e
+
+[ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
+
+case "$1" in
+    configure)
+    # continue below
+    ;;
+
+    abort-upgrade|abort-remove|abort-deconfigure)
+    exit 0
+    ;;
+
+    *)
+    echo "postinst called with unknown argument \`$1'" >&2
+    exit 0
+    ;;
+esac
+
+
+# Load debconf
+. /usr/share/debconf/confmodule
+
+# Include CARNet functions
+. /usr/share/carnet-tools/functions.sh
+
+PKG="php7.0-cn"
+A2MODEDIR="/etc/apache2/mods-enabled"
+### mysql je vec ukljucen u wheezyu
+### EXTENSIONS="mysql"
+EXTENSIONS=""
+PHP70DIR="/etc/php/7.0"
+PHP70MODADIR="$PHP70DIR/mods-available"
+PHP70CONFD="/etc/php/7.0/conf.d"
+PHP5DIR="/etc/php5"
+
+need_restart=0
+
+
+# phpmemlimit ()
+#
+#   Calculate memory size for PHP memory_limit option.
+#
+phpmemlimit () {
+
+    local memtotal memlimit
+    memtotal=`awk 'tolower($1) == "memtotal:" { printf("%i", $2/1024); }' /proc/meminfo`
+
+    if [ $memtotal -lt 512 ]; then memlimit=16; fi
+    if [ $memtotal -ge 512 ]; then memlimit=32; fi
+    if [ $memtotal -gt 1024 ]; then memlimit=64; fi
+
+    echo $memlimit
+}
+
+# phpinivalidate ()
+#
+#   Validate php.ini values.
+#
+phpinivalidate () {
+
+    local ini_file
+    ini_file="$1"
+
+    awk -F'[[:space:]]*=[[:space:]]*' \
+        '/^[[:space:]]*[[:alpha:]_]+[[:space:]]*=[[:space:]]*[[:digit:]]+[MGK]B/ {print $1" = "$2}' \
+        "$ini_file"
+}
+
+
+# Disable PHP5 module and enable Apache2 PHP7 module.
+#
+if [ -e /etc/apache2/apache2.conf ]; then
+    if [ -e "$A2MODEDIR/php5.load" ]; then
+        cp_echo "CN: Disabling PHP5 module for Apache2 web server"
+        a2dismod php5 >/dev/null || true
+        need_restart=1
+    fi
+    if [ ! -e "$A2MODEDIR/php7.0.load" ] || [ ! -e "$A2MODEDIR/php7.0.conf" ]; then
+        cp_echo "CN: Enabling PHP7 module for Apache2 web server"
+        a2enmod php7.0 >/dev/null || true
+
+        if [ -e "$A2MODEDIR/php7.0.conf" ]; then
+            if egrep -qi "^[[:space:]]*php_admin_value engine Off" "$A2MODEDIR/php7.0.conf"; then
+                db_fset php7.0-cn/userdir seen false
+                db_title php7.0-cn - konfiguracija
+                db_subst php7.0-cn/userdir php70_conf "$A2MODEDIR/php5.conf"
+                db_input critical php7.0-cn/userdir || true
+                db_go || true
+            fi
+        fi
+
+        need_restart=1
+    fi
+fi
+
+
+# Check for PHP7 SAPI directories and configuration files.
+#
+for SAPI in apache2 cli; do
+
+    if [ ! -d "$PHP70DIR/$SAPI" ]; then
+        cp_echo "CN: Creating configuration directory $PHP70DIR/$SAPI/"
+        mkdir -p $PHP70DIR/$SAPI/
+    fi
+
+    ini_file="$PHP70DIR/$SAPI/php.ini"
+
+    if [ ! -f "$ini_file" ]; then
+
+        cp_echo "CN: Generating configuration file $ini_file"
+
+        ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
+        if [ "$SAPI" = "cli" ]; then
+            if [ -f "/usr/lib/php/7.0/php.ini-production.cli" ]; then
+                cat /usr/lib/php/7.0/php.ini-production.cli > $ini_file_tmp
+            fi
+        else
+            if [ -f "/usr/lib/php/7.0/php.ini-production" ]; then
+                cat /usr/lib/php/7.0/php.ini-production > $ini_file_tmp
+            fi
+        fi
+        cp_mv $ini_file_tmp $ini_file
+        need_restart=1
+    fi
+    chmod 644 $ini_file
+done
+
+
+# Check for /etc/php/7.0/conf.d/ directory.
+#
+if [ ! -d "$PHP70CONFD" ]; then
+    cp_echo "CN: Creating configuration directory $PHP70CONFD"
+    mkdir -p $PHP70CONFD/
+fi
+
+# Brisanje obsolete datoteke mysql.ini u $PHP70CONFD
+#
+if [ -f "$PHP70CONFD/mysql.ini" ]; then
+       # just to make sure mysql is included in _new-style_ configuration
+       if [ -f "$PHP70MODADIR/mysql.ini" ]; then
+               cp_echo "CN: Removing obsolete $PHP70CONFD/mysql.ini"
+               rm -f $PHP70CONFD/mysql.ini
+       else
+               cp_echo "CN: WARNING: Obsolete $PHP70CONFD/mysql.ini present, and no $PHP70MODADIR/mysql.ini!"
+       fi
+fi
+
+# Check if PHP7 extensions are enabled.
+#
+for php70ext in $EXTENSIONS; do
+
+    php70ext_re="^[[:space:]]*extension[[:space:]]*=[[:space:]]*$php70ext\.so"
+
+    # Remove extension entry from /etc/php/7.0/(apache2|cli).ini
+    # configuration files.
+    for SAPI in apache2 cli; do
+
+        ini_file="$PHP70DIR/$SAPI/php.ini"
+
+        if [ -f "$ini_file" ]; then
+
+            if egrep -q "$php70ext_re" "$ini_file"; then
+                cp_echo "CN: Removing $php70ext extension from file $ini_file"
+            fi
+
+            cp_check_and_sed "$php70ext_re" \
+                "/$php70ext_re/d" \
+                "$ini_file" && need_restart=1 || true
+        fi
+    done
+
+    # Check extension configuration in /etc/php/7.0/conf.d/ directory.
+    if [ ! -f "$PHP70CONFD/$php70ext.ini" ]; then
+
+        cp_echo "CN: Creating configuration file $PHP70CONFD/$php70ext.ini"
+
+        php5ext_up=`echo $php70ext | tr [:lower:] [:upper:] | sed 's/Y/y/'`
+        INITMP=`mktemp $PHP70CONFD/$php70ext.ini.tmp.XXXXXX`
+
+        cp_echo "CN: Adding $php70ext extension to file $PHP70CONFD/$php70ext.ini"
+
+        printf "# configuration for php %s module\nextension=%s.so\n" "${php70ext_up}" "${php5ext}" >> "$INITMP"
+        cp_mv "$INITMP" "$PHP70CONFD/$php70ext.ini"
+
+        need_restart=1
+    else
+
+        if ! grep -q "$php70ext_re" "$PHP70CONFD/$php70ext.ini"; then
+
+            cp_echo "CN: Adding $php70ext extension to file $PHP70CONFD/$php70ext.ini"
+
+            INITMP=`mktemp $PHP70CONFD/$php70ext.ini.tmp.XXXXXX`
+            cat "$PHP70CONFD/$php70ext.ini" > "$INITMP"
+            echo "extension=$php70ext.so" >> "$INITMP"
+            cp_mv "$INITMP" "$PHP70CONFD/$php70ext.ini"
+
+            need_restart=1
+        fi
+    fi
+    chmod 644 $PHP70CONFD/$php70ext.ini
+done
+
+
+# Enable some PHP7 tweaks for Apache2 web server (/etc/php/7.0/apache2/php.ini).
+#
+#   * upload_max_filesize = 256M
+#   * post_max_size, memory_limit = depends on system memory, we are using
+#     phpmemlimit() function.
+#   * error_reporting = E_ERROR
+#
+for SAPI in apache2 cli; do
+
+    if [ ! -d "$PHP70DIR/$SAPI" ]; then
+        continue
+    fi
+
+    ini_file="$PHP70DIR/$SAPI/php.ini"
+    db_get php7.0-cn/${SAPI} || true
+    if [ "$RET" = "true" ]; then
+
+        cp_echo "CN: Checking and enabling some specific parameters in file $ini_file"
+
+        phplimit="$(phpmemlimit)M"
+
+        if [ -f "$ini_file" ]; then
+
+            cp_check_and_sed "^[[:space:]]*upload_max_filesize[[:space:]]*=" \
+                's/^[[:space:]]*upload_max_filesize[[:space:]]*=.*/upload_max_filesize = 256M/' \
+                "$ini_file" && need_restart=1 || true
+
+            cp_check_and_sed "^[[:space:]]*post_max_size[[:space:]]*=" \
+                "s/^[[:space:]]*post_max_size[[:space:]]*=.*/post_max_size = ${phplimit}/" \
+                "$ini_file" && need_restart=1 || true
+
+            cp_check_and_sed "^[[:space:]]*memory_limit[[:space:]]*=" \
+                "s/^[[:space:]]*memory_limit[[:space:]]*=.*/memory_limit = ${phplimit}/" \
+                "$ini_file" && need_restart=1 || true
+
+            cp_check_and_sed "^[[:space:]]*error_reporting[[:space:]]*=" \
+                "s/^[[:space:]]*error_reporting[[:space:]]*=.*/error_reporting = E_ERROR/" \
+                "$ini_file" && need_restart=1 || true
+        fi
+
+        ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
+        if [ -f "$ini_file" ]; then
+            cat $ini_file > $ini_file_tmp
+        fi
+
+        if ! egrep -q "^[[:space:]]*upload_max_filesize[[:space:]]*=" $ini_file_tmp; then
+            echo "upload_max_filesize = 256M" >> "$ini_file_tmp"
+            need_restart=1
+        fi
+        if ! egrep -q "^[[:space:]]*post_max_size[[:space:]]*=" $ini_file_tmp; then
+            echo "post_max_size = ${phplimit}" >> "$ini_file_tmp"
+            need_restart=1
+        fi
+        if ! egrep -q "^[[:space:]]*memory_limit[[:space:]]*=" $ini_file_tmp; then
+            echo "memory_limit = ${phplimit}" >> "$ini_file_tmp"
+            need_restart=1
+        fi
+        if ! egrep -q "^[[:space:]]*error_reporting[[:space:]]*=" $ini_file_tmp; then
+            echo "error_reporting = E_ERROR" >> "$ini_file_tmp"
+            need_restart=1
+        fi
+        cp_mv "$ini_file_tmp" "$ini_file"
+        chmod 644 "$ini_file"
+
+        if [ -e "$ini_file_tmp" ]; then
+            rm -f "$ini_file_tmp"
+        fi
+    fi
+
+    # Validate php.ini values.
+    if [ -f "$ini_file" ]; then
+        php70_inivalues="$(phpinivalidate "$ini_file")"
+        if [ -n "$php70_inivalues" ]; then
+            db_fset php7.0-cn/inivalues seen false
+            db_title php7.0-cn - konfiguracija za $(echo ${SAPI} | sed 's/a/A/;s/cli/CLI/')
+            db_subst php7.0-cn/inivalues php70_sapi $(echo ${SAPI} | sed 's/a/A/;s/cli/CLI/')
+            db_subst php7.0-cn/inivalues ini_file "$ini_file"
+            db_capb escape
+            db_subst php7.0-cn/inivalues php70_inivalues "$(echo -n "$php70_inivalues" | debconf-escape -e)"
+            db_input critical php7.0-cn/inivalues || true
+            db_go || true
+        fi
+    fi
+done
+
+
+# Compare active PHP7 extensions with PHP5 ones, if PHP5 configuration still exists.
+#
+if [ -d "${PHP5DIR}" ] && [ -f "${PHP5DIR}/apache2/php.ini" ]; then
+
+    phpext_re='^[[:space:]]*extension[[:space:]]*=[[:space:]]*["]{0,1}(.*)\.so["]{0,1}'
+
+    # Get all active PHP5 extensions.
+    php5_ext="$(find ${PHP5DIR} -type f -name *.ini | 
+         egrep "\/(conf\.d|apache2|cli)\/" | 
+         xargs sed -rn 's/'$phpext_re'/\1/Ip')" || true
+
+    # Get all active PHP7 extensions.
+    php70_ext=""
+    if [ -d "${PHP5DIR}" ]; then
+        php70_ext="$(find ${PHP70DIR} -type f -name *.ini | 
+             egrep "\/(conf\.d|apache2|cli)\/" | 
+             xargs sed -rn 's/'$phpext_re'/\1/Ip')" || true
+    fi
+
+    # Compare PHP7 <-> PHP5 extensions.
+    php70_ext_mis=""
+    for ext in $php5_ext; do
+        if [ "$(echo $php70_ext | egrep $ext)" = "" ]; then
+            php70_ext_mis="\n${ext}${php70_ext_mis}"
+        fi
+    done
+
+    # Some PHP7 extensions are not active.
+    if [ -n "$php70_ext_mis" ]; then
+        db_fset php7.0-cn/extensions seen false
+        db_title php7.0-cn - konfiguracija
+        db_capb escape
+        db_subst php7.0-cn/extensions php70_extensions "${php70_ext_mis#*\n}"
+        db_input critical php7.0-cn/extensions || true
+        db_go || true
+    fi
+fi
+
+
+db_stop || true
+
+
+# Restart Apache2 web server if needed.
+#
+if [ $need_restart -eq 1 ]; then
+
+    # Check Apache2 web server configuration.
+    if apache2ctl configtest 2>/dev/null; then
+        invoke-rc.d apache2 force-reload || true
+    else
+        # Something is broken.
+        cp_echo "CN: Your Apache2 configuration is broken."
+        cp_echo "CN: Please, check the service after the installation finishes!"
+    fi
+fi
+
+
+# Mail root
+#
+cp_mail "$PKG"
+
+
+
+exit 0