#!/usr/bin/env bash

set -ex

. ${0%/*}/_functions.sh

WWWDIR="/opt/www/webmail"
mkdir -p /opt/www

cd /root

ok "* roundcube installation..."

# DOWNLOAD_URL=`curl -s https://api.github.com/repos/roundcube/roundcubemail/releases/latest | grep browser_download_url | cut -d '"' -f 4 | grep -v framework | grep complete | grep 'tar.gz$'`
DOWNLOAD_URL='https://github.com/roundcube/roundcubemail/releases/download/1.6.8/roundcubemail-1.6.8-complete.tar.gz'

wget -q $DOWNLOAD_URL -O - | tar xz

mv /root/roundcubemail* "$WWWDIR"
rm -r "$WWWDIR/installer"

chown www-data:www-data "$WWWDIR/logs" "$WWWDIR/temp"

sed -i '31i<roundcube:button name="administration" label="Administration" type="link" class="settings" innerClass="inner" onclick="document.location='\''..\/admin\/'\''" \/>' /opt/www/webmail/skins/elastic/templates/includes/menu.html
change '/\<roundcube:endif \/>/a &nbsp;&bull;&nbsp; <a href="..\/..\/..\/..\/admin\/">Administration<\/a>' /opt/www/webmail/skins/elastic/templates/login.html

cp /opt/admin/public/favicon.ico /opt/www/webmail/skins/elastic/images/favicon.ico
cp /opt/admin/public/logo.svg /opt/www/webmail/skins/elastic/images/logo.svg
