#!/bin/sh

MOODLE_PASS=`pwgen 16 1`
WIKI_PASS=`pwgen 16 1`
RUJEL_PASS=`pwgen 16 1`
NEXTCLOUD_PASS=`pwgen 16 1`

# MediaWiki access rights
init_wiki()
{
	mediawiki_config="/var/www/webapps/mediawiki/config/LocalSettings.php"
	rpm -q mediawiki &>/dev/null || return 0

	# Create database and db user with password
	echo "create database wikidb;" | mysql
	echo "GRANT ALL ON wikidb.* TO 'wikiuser'@'localhost.localdomain' IDENTIFIED BY '$WIKI_PASS';" | mysql wikidb
	echo "GRANT ALL ON wikidb.* TO 'wikiuser'@'localhost' IDENTIFIED BY '$WIKI_PASS';" | mysql wikidb
	echo "GRANT ALL ON wikidb.* TO 'wikiuser'@'%' IDENTIFIED BY '$WIKI_PASS';" | mysql wikidb

	# Remove config if it exist
	rm -f "$mediawiki_config"

	# Generate configuration by install script from mediawiki
	php /usr/share/mediawiki/maintenance/install.php \
	    --pass "$(pwgen)" \
            --dbname "wikidb" \
	    --dbuser "wikiuser" \
	    --dbpass "$WIKI_PASS" \
	    --scriptpath "/wiki" \
	    --with-extensions \
	    --lang "ru" \
	    "Wiki" "WikiSysop"

	# Move generated config file to webapps directory
	mv -f /usr/share/mediawiki/LocalSettings.php "$mediawiki_config"

	# Tune configuration
	subst  's/$wgEnableEmail.*/$wgEnableEmail = false;/;
		s/$wgEnableUserEmail.*/$wgEnableUserEmail = false;/;
		s/$wgEmailAuthentication.*/$wgEmailAuthentication = false;/;
		' "$mediawiki_config"

	# Enable LDAP authentication
	cp -f /usr/share/installed-db-office-server/LDAP.php /var/www/webapps/mediawiki/config/LocalSettings.d
	curl -s --insecure https://localhost/wiki &>/dev/null ||:
}

init_rujel()
{

	rpm -q rujel &>/dev/null || return 0
	echo "create database rujel;" | mysql
	echo "GRANT ALL PRIVILEGES ON \`Rujel%\`.* TO 'rujel'@'localhost' IDENTIFIED BY '$RUJEL_PASS';" | mysql mysql
	echo "GRANT ALL PRIVILEGES ON \`VseLists\`.* TO 'rujel'@'localhost';" | mysql mysql
	echo "GRANT ALL PRIVILEGES ON \`Contacts\`.* TO 'rujel'@'localhost';" | mysql mysql

	sed -i s/RUJELpassword/$RUJEL_PASS/ /etc/webobjects/rujel/modules/database.plist
}

init_nextcloud()
{
	rpm -q nextcloud &>/dev/null || return 0
	echo "drop database nextcloud;" | mysql
	echo "create database nextcloud;" | mysql
	echo "GRANT ALL ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY '$NEXTCLOUD_PASS';" | mysql nextcloud
	rm -f /var/www/webapps/nextcloud/config/config.php
	su - -c "php -d memory_limit=512M \
	        /var/www/webapps/nextcloud/occ maintenance:install \
		--database \"mysql\" \
		--database-name \"nextcloud\" \
		--database-user \"nextcloud\" \
		--database-pass \"$NEXTCLOUD_PASS\" \
		--database-host \"localhost\" \
		--admin-user \"root\" \
		--admin-pass \"$NEXTCLOUD_PASS\"" \
		-s /bin/bash \
		apache2
	subst "/0 =>/a \    1 => '$(hostname)'," /var/www/webapps/nextcloud/config/config.php
	curl -s --insecure https://localhost/nextcloud &>/dev/null ||:
}

init_moodle(){
	rpm -q moodle &>/dev/null || return 0
	rm -f /var/www/webapps/moodle/config.php
	echo "create database moodle1 character set utf8mb4 collate utf8mb4_unicode_ci;" | mysql
	echo "GRANT ALL ON moodle1.* to 'moodleuser'@'localhost' IDENTIFIED BY '$MOODLE_PASS';" | mysql
	pushd /var/www/webapps/moodle/admin/cli
	php install.php \
		--fullname="Moodle" \
		--shortname="Moodle" \
		--lang=ru \
		--dbtype=mariadb \
		--dbhost=localhost \
		--dbname=moodle1 \
		--dbuser=moodleuser \
		--adminpass="$MOODLE_PASS" \
		--dbpass="$MOODLE_PASS" \
		--wwwroot="https://$(hostname)/moodle" \
		--agree-license \
		--non-interactive
	popd
	curl -s --insecure https://$(hostname)/moodle &>/dev/null ||:
}

control MySQL-server nograntnonet
service mysqld start

PASS=`echo "select Password from user where user='root'" | mysql mysql | grep -v ^$ | grep -v ^Password`
echo "update user set Password = '' where user='root'" | mysql mysql

control MySQL-server grantnonet
service mysqld restart

echo 'select 1;' | mysql &>/dev/null
STATUS=$?
i=0
while [ $i -lt 20 ] && [ "$STATUS" != "0" ]; do
    echo "select 1" | mysql &>/dev/null
    STATUS=$?
    let i=i+1
    if [ "$STATUS" -ge "1" ]; then
	sleep 1
    fi
done

if [ "$i" == "20" ]; then
	echo "can't connect to MySQL server"
    exit 1
fi

init_wiki
init_rujel
init_nextcloud
init_moodle

echo "update user set Password = '$PASS' where user='root'" | mysql mysql

service mysqld restart 
chkconfig mysqld on

exit 0
