#!/bin/bash

#
# Copyright (C) 2017 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer 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 3 of the License,
# or any later version.
#
# NethServer 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 NethServer.  If not, see COPYING.
#


su - postgres -c "psql -lqt | cut -d \| -f 1 | grep -q -w webtop5"
if [ $? -eq 1 ]; then # database do not exists

    # create db and users
    tmp_sql=`mktemp`
    chmod a+r $tmp_sql
    password=`perl -e "use NethServer::Password; print NethServer::Password::store('webtop5');"`
cat << EOF > $tmp_sql
CREATE database webtop5;
CREATE USER sonicle WITH PASSWORD '$password';
ALTER USER sonicle WITH SUPERUSER;
GRANT ALL PRIVILEGES ON DATABASE webtop5 to sonicle;
EOF
    su - postgres  -c "psql < $tmp_sql" >/dev/null
    rm -f $tmp_sql

    # initialize postgres requisites
    for psql in `find /usr/share/webtop/sql/postgres -name \*.sql | sort`
    do
	    echo "Loading: $psql ..."
	    su - postgres  -c "psql webtop5 < $psql" >/dev/null
    done

    # initialize db schema in the following order
    # 1. core
    # 2. config
    # 3. calendar
    # 4. contacts
    # 5. mail
    # 6. tasks
    # 7. vfs
    #
    # init-public.sql is obsolete
    for sql in core config calendar contacts mail tasks vfs
    do
        echo "Loading: $sql ..."
        su - postgres  -c "psql webtop5 < /usr/share/webtop/sql/schema/init-$sql.sql" >/dev/null
    done

    # initialize db data
    for dsql in `find /usr/share/webtop/sql/data/ -name \*.sql | sort`
    do
        echo "Loading: $dsql ..."
        su - postgres  -c "psql webtop5 < $dsql" >/dev/null
    done


    # fix table owner
    tmp_sql=`mktemp`
    chmod a+r $tmp_sql
    for tbl in `su - postgres  -c "psql -qAt -c \"select tablename from pg_tables where schemaname = 'public';\" webtop5"` \
               `su - postgres  -c "psql -qAt -c \"select sequence_name from information_schema.sequences where sequence_schema = 'public';\" webtop5"` \
               `su - postgres  -c "psql -qAt -c \"select table_name from information_schema.views where table_schema = 'public';\" webtop5"` ;
    do
        echo "alter table \"$tbl\" owner to sonicle;" >> $tmp_sql
    done
    su - postgres  -c "psql webtop5 < $tmp_sql" >/dev/null
    rm -f $tmp_sql
else

    # initialize postgres requisites
    for psql in `find /usr/share/webtop/sql/postgres -name \*.sql | sort`
    do
	    echo "Loading: $psql ..."
	    su - postgres  -c "psql webtop5 < $psql" >/dev/null
    done

    for sql in `find /usr/share/webtop/updates/pre -name \*.sql`
    do
        su - postgres -c "psql webtop5 < $sql" >/dev/null
    done
fi


# Make sure old tomcat instance has been stopped
systemctl stop tomcat@webtop 2>/dev/null || true
config delete tomcat@webtop 2>/dev/null || true


# NethServer/dev#5731 - Fix existing installations where core.file_types table is empty
lines=$(su - postgres -c 'psql webtop5 -t -c "select * from \"core\".\"file_types\""')
if [ -z "$lines" ]; then
    su - postgres -c 'grep "INSERT INTO \"core\".\"file_types\"" /usr/share/webtop/sql/data/init-data-core.sql | psql webtop5'
fi
