#!/usr/bin/env bash

if [ ! -f /data/roundcube/roundcube.db ]; then
    mkdir /data/roundcube 2> /dev/null
    echo -e "\t\t\033[1;33m* initalizing new Roundcube database\033[0m"
    cat "/opt/www/webmail/SQL/sqlite.initial.sql" | sqlite3 /data/roundcube/roundcube.db
fi

chown www-data:www-data /data/roundcube/roundcube.db
chown www-data:www-data /data/roundcube

if [ ! -d /data/roundcube/enigma ]; then
  mkdir -p /data/roundcube/enigma 2> /dev/null
fi

if [ ! -d /data/roundcube/logs ]; then
  mkdir -p /data/roundcube/logs 2> /dev/null
fi

rm -Rf /opt/www/webmail/logs
ln -s /data/roundcube/logs /opt/www/webmail/logs

chown www-data:www-data -R /data/roundcube/logs
chown www-data:www-data -R /data/roundcube/enigma

# Update Roundcube
/opt/www/webmail/bin/update.sh -v "?" -y > /dev/null