#!/bin/bash

set -e

# Source debconf library.
. /usr/share/debconf/confmodule

APP='gnusocial'
GNUSOCIAL_DOMAIN_NAME="${APP}.example"
ADMIN_PASS="${APP}"

# get the domain name to be used
db_get ${APP}/domain
GNUSOCIAL_DOMAIN_NAME_STR=$RET
if [ $GNUSOCIAL_DOMAIN_NAME_STR ]; then
    GNUSOCIAL_DOMAIN_NAME=$GNUSOCIAL_DOMAIN_NAME_STR
fi

SCRDIR=$(mktemp -dt "$(basename $0).XXXXXXXX")
if [ ! "$?" = "0" ]; then
    exit 1
fi
SQL_FILE=$SCRDIR/${APP}.sql
INSTALLDIR=/etc/share/${APP}

# link into /var/www
if [ ! -L /var/www/${APP} ]; then
    ln -s $INSTALLDIR /var/www/${APP}
fi

# configure nginx
if [ -d /etc/nginx ]; then
    install -m 644 $INSTALLDIR/webservers/nginx /etc/nginx/sites-available/${APP}
    sed -i "s|gnusocial.example|${GNUSOCIAL_DOMAIN_NAME}|g" /etc/nginx/sites-available/${APP}
    # Don't enable the site by default because the host files may need to be edited
    #if [ ! -L /etc/nginx/sites-enabled/${APP} ]; then
    #   ln -s /etc/nginx/sites-available/${APP} /etc/nginx/sites-enabled/
    #fi
fi

# configure apache
if [ -d /etc/apache2/sites-available ]; then
    install -m 644 $INSTALLDIR/webservers/apache2 /etc/apache2/sites-available/${APP}
    sed -i "s|${APP}.example|${GNUSOCIAL_DOMAIN_NAME}|g" /etc/apache2/sites-available/${APP}
    # Don't enable the site by default because the host files may need to be edited
    #if [ ! -L /etc/nginx/sites-enabled/${APP} ]; then
    #    a2ensite ${APP}
    #fi
fi

# get password for admin of the gnusocial database
db_get ${APP}/admin_password
GNUSOCIAL_ADMIN_PASSWORD=$RET
if [ $GNUSOCIAL_ADMIN_PASSWORD ]; then
    ADMIN_PASS=$GNUSOCIAL_ADMIN_PASSWORD
fi

# get root password for mysql/MariaDB
MYSQL_ROOT_PASS=
db_get mariadb-server/root_password
MYSQL_ROOT_PASSWORD=$RET
if [ $MYSQL_ROOT_PASSWORD ]; then
    MYSQL_ROOT_PASS=$MYSQL_ROOT_PASSWORD
fi

# create the database
if [ $MYSQL_ROOT_PASS ]; then
    # create a database
    echo "create database ${APP};
CREATE USER '${APP}admin'@'localhost' IDENTIFIED BY '$ADMIN_PASS';
GRANT ALL PRIVILEGES ON ${APP}.* TO '${APP}admin'@'localhost';
quit" > $SQL_FILE
    chmod 600 $SQL_FILE
    mysql -u root --password="$MYSQL_ROOT_PASS" < $SQL_FILE
    if [ ! "$?" = "0" ]; then
        echo $"Unable to create database for ${APP}"
    fi
    rm $SQL_FILE
fi
rm -rf $SCRDIR

CONFIG_FILE=$INSTALLDIR/config.php
if [ ! -f $CONFIG_FILE ]; then
    echo $'No config.php file found'
    exit 2
fi
echo '' >> $CONFIG_FILE
echo "// Recommended GNU social settings" >> $CONFIG_FILE
echo "\$config['thumbnail']['maxsize'] = 3000;" >> $CONFIG_FILE
echo "\$config['profile']['delete'] = true;" >> $CONFIG_FILE
echo "\$config['profile']['changenick'] = true;" >> $CONFIG_FILE
echo "\$config['public']['localonly'] = false;" >> $CONFIG_FILE
echo "addPlugin('StoreRemoteMedia');" >> $CONFIG_FILE
echo "\$config['queue']['enabled'] = true;" >> $CONFIG_FILE
echo "\$config['queue']['daemon'] = true;" >> $CONFIG_FILE

# set permissions
chmod a+w $INSTALLDIR
chown www-data:www-data $INSTALLDIR
chmod +x $INSTALLDIR/scripts/maildaemon.php

exit 0
