#! /bin/bash
. /usr/lib/envole-tools/e-ihm.sh

container_path_web=$(CreoleGet container_path_web non)
container_ip_mysql=$(CreoleGet adresse_ip_mysql non)
activer_themes=$(CreoleGet activer_themes non)
activer_dokuwiki=$(CreoleGet activer_dokuwiki non)
activer_wordpress=$(CreoleGet activer_wordpress non)
activer_opensondage=$(CreoleGet activer_opensondage non)
activer_piwigo=$(CreoleGet activer_piwigo non)
activer_dispatcher=$(CreoleGet dispatcher non)
nom_theme=$(CreoleGet nom_theme cloud)

themes_dir="/usr/share/envole/envole-themes"

www_dir="$container_path_web/var/www/html"
dokuwiki_dir="$www_dir/dokuwiki"
wordpress_dir="$www_dir/wordpress"
edispatcher_dir="$www_dir/edispatcher"
opensondage_dir="$www_dir/opensondage"
piwigo_dir="$www_dir/piwigo"
ead_dir="/usr/share/ead2"
sso_dir="/usr/share/sso"
poshprofil_dir="$www_dir/posh-profil"


RunCmd=CreoleRun


if [ "$activer_themes" = 'non' ]
then
    exit 0
fi

TitleSimple "ENVOLE-THEME"

# DOKUWIKI
if [[ "$activer_dokuwiki" = 'oui' && -d "$themes_dir/$nom_theme/dokuwiki" ]]
then
    EchoVert "  >> Dokuwiki"

    #Suppression de l'ancien theme
    rm -rf "$dokuwiki_dir/lib/tpl/$nom_theme"

    #Creation de l'arborescence
    mkdir -p "$dokuwiki_dir/lib/tpl/$nom_theme/images"

    #Copie des sources
    cp -r "$themes_dir/$nom_theme/dokuwiki" "$www_dir"

    #Permission
    chown -R root:www-data "$dokuwiki_dir/lib/tpl/$nom_theme"
    chmod -R 755 "$dokuwiki_dir/lib/tpl/$nom_theme"

    #Changer la conf
    nblig=`grep -n "'template" "$dokuwiki_dir/conf/dokuwiki.php" | cut -d: -f1`
    lbrep="\$conf['template']    = '$nom_theme';"
    sed ${nblig}s/.*/"$lbrep"/ $dokuwiki_dir/conf/dokuwiki.php > $dokuwiki_dir/conf/dokuwiki.php.tmp
    rm -rf $dokuwiki_dir/conf/dokuwiki.php
    mv $dokuwiki_dir/conf/dokuwiki.php.tmp $dokuwiki_dir/conf/dokuwiki.php
fi

# WORDPRESS
if [[ "$activer_wordpress" = 'oui' && -d "$themes_dir/$nom_theme/wordpress" ]]
then
    EchoVert "  >> Wordpress"
    . /usr/share/envole/eoledb/wordpress
    export MYSQL_PWD=$dbpassWORDPRESS

    #Suppression de l'ancien theme
    rm -rf "$wordpress_dir/wp-content/themes/$nom_theme"

    #Creation de l'arborescence
    mkdir -p "$wordpress_dir/wp-content/themes/$nom_theme/images"

    #Copie des sources
    cp -r "$themes_dir/$nom_theme/wordpress" "$www_dir"

    #Mise à jour de la base
    echo "UPDATE wp_options SET option_value='$nom_theme' WHERE option_name='template' OR option_name='stylesheet';" | mysql -uwordpress -h$dbhostWORDPRESS -P$dbportWORDPRESS wordpress
fi

# OPENSONDAGE
if [[ "$activer_opensondage" = 'oui' && -d "$themes_dir/$nom_theme/opensondage" ]]
then
    EchoVert "  >> Opensondage"

    #Suppression de l'ancien theme
    rm -rf "$opensondage_dir/webroot/css/$nom_theme.theme.css"

    #Copie des sources
    cp -r "$themes_dir/$nom_theme/opensondage" "$www_dir"

    #Permission
    chown -R root:www-data "$opensondage_dir/webroot/css/$nom_theme.theme.css"

    #Changement de la configuration
    nblig=`grep -n "CSStheme" "$opensondage_dir/config/conf.php" | cut -d: -f1`
    lbrep="    static \$CSStheme = '${nom_theme}';"
    sed ${nblig}s/.*/"$lbrep"/ $opensondage_dir/config/conf.php > $opensondage_dir/config/conf.php.tmp

    rm -rf $opensondage_dir/config/conf.php
    mv $opensondage_dir/config/conf.php.tmp $opensondage_dir/config/conf.php
fi

# EOLE-DISPATCHER dispatcher
if [[ "$activer_dispatcher" = 'oui' && -d "$themes_dir/$nom_theme/edispatcher" ]]
then
    EchoVert "  >> EDispatcher"

	#Suppression de l'ancien theme
	rm -rf "$edispatcher_dir/images/*"
	rm -rf "$edispatcher_dir/css/*"

	#Copie des sources
	cp -r "$themes_dir/$nom_theme/edispatcher" "$www_dir"
fi

#PIWIGO
if [[ "$activer_piwigo" = 'oui' && -d "$themes_dir/$nom_theme/piwigo" ]]
then
    EchoVert "  >> Piwigo"
    . /usr/share/envole/eoledb/piwigo
    export MYSQL_PWD=$dbpassPIWIGO

     #Suppression de l'ancien theme
    rm -rf "$piwigo_dir/themes/$nom_theme"

    #Copie des sources
    cp -r "$themes_dir/$nom_theme/piwigo" "$www_dir"
    theme_version=$(grep Version $piwigo_dir/themes/${nom_theme}/themeconf.inc.php | awk -F ": " '{print $2}')
    ptheme_name=$(grep "Theme Name" $piwigo_dir/themes/${nom_theme}/themeconf.inc.php | awk -F ": " '{print $2}')

    #Mise à jour de la base
    echo "UPDATE piwigo_user_infos SET theme='$nom_theme' WHERE status IN ('webmaster','guest','normal');" | mysql -upiwigo -h$dbhostPIWIGO -P$dbportPIWIGO piwigo
    echo "UPDATE piwigo_config SET value='$nom_theme' WHERE param='mail_theme';" | mysql -upiwigo -h$dbhostPIWIGO -P$dbportPIWIGO piwigo
    echo "UPDATE piwigo_config SET value='$nom_theme' WHERE param='mobile_theme';" | mysql -upiwigo -h$dbhostPIWIGO -P$dbportPIWIGO piwigo
    echo "DELETE FROM piwigo_themes WHERE id='$nom_theme';" | mysql -upiwigo -h$dbhostPIWIGO -P$dbportPIWIGO piwigo
    echo "INSERT INTO piwigo_themes (id, version, name) VALUES ('$nom_theme','$theme_version','$ptheme_name');" | mysql -upiwigo -h$dbhostPIWIGO -P$dbportPIWIGO piwigo

    #Permission
    chown -R root:www-data "$piwigo_dir/themes/$nom_theme"
fi

#POSHPROFIL
if [[ -d "$themes_dir/$nom_theme/posh-profil" ]]
then
    EchoVert "  >> PoshProfil"

    rm -rf "$poshprofil_dir/styles/$nom_theme"

    cp -r "$themes_dir/$nom_theme/posh-profil/$nom_theme" "$poshprofil_dir/styles"
    sed -i "s/styles\/.*\/css/styles\/${nom_theme}\/css/" "$poshprofil_dir/include/envole.php"
    sed -i "s/styles\/.*\/tpl/styles\/${nom_theme}\/tpl/" "$poshprofil_dir/include/envole.php"

    chgrp -R www-data "$poshprofil_dir/styles/$nom_theme"
fi

# EOLE-EAD
if [[ -d "$themes_dir/$nom_theme/ead/frontend" ]]
then
    EchoVert "  >> EAD"

	#Suppression de l'ancien theme
	rm -rf "$ead_dir/frontend/web/static/style/*.css"

	#Copie des sources
	cp -r "$themes_dir/$nom_theme/ead/frontend" "$ead_dir"
fi

# EOLE-SSO
if [[ -d "$themes_dir/$nom_theme/eole-sso/interface" ]]
then
    EchoVert "  >> EoleSSO"

	#Suppression de l'ancien theme
	rm -rf "$sso_dir/interface/theme/style/*"
	rm -rf "$sso_dir/interface/theme/image/*"

	#Copie des sources
	cp -r "$themes_dir/$nom_theme/eole-sso/interface" "$sso_dir"
fi

EchoVert ""
EchoVert ""  