#!/bin/bash
#

. /usr/share/envole/eoledb/moodle

container_path_web=$(CreoleGet container_path_web)
web_url=$(CreoleGet web_url)
activer_moodle=$(CreoleGet activer_moodle)
activer_moodlevisiteur=$(CreoleGet activer_moodlevisiteur non)
moodle_apikey=$(CreoleGet moodle_apikey "secretkeymoodle")
adresse_ip_web=$(CreoleGet adresse_ip_web)
eolesso_adresse=$(CreoleGet eolesso_adresse)
eolesso_port=$(CreoleGet eolesso_port)
eolesso_cas_folder=$(CreoleGet eolesso_cas_folder "")
adresse_ip_mysql=$(CreoleGet adresse_ip_mysql)
adresse_ip_ldap=$(CreoleGet adresse_ip_ldap)
ldap_base_dn=$(CreoleGet ldap_base_dn)
activer_addadmin=$(CreoleGet activer_addadmin)
uid_addadmin=$(CreoleGet uid_addadmin null)
proxy_client_adresse=$(CreoleGet proxy_client_adresse null)
proxy_client_port=$(CreoleGet proxy_client_port null)
default_admin_email=admin@$(CreoleGet domaine_messagerie_etab)

RunCmd=CreoleRun
RunMysql=${container_path_web}/usr/bin/mysql

declare APACHE_USER="www-data"
declare BIN_PHP="/usr/bin/php7.4"
declare BIN_SU="/bin/su"
declare INSTALL_SCRIPT="admin/cli/install.php"
declare UPGRADE_SCRIPT="admin/cli/upgrade.php"

declare INSTALL_DIR="/var/www/html/moodle"
declare MOOSH_DIR="/var/www/html/moodle/moosh"
declare DATA_DIR="/home/www-data/var/www/html/moodle"

declare LANG="fr"
declare LOCK="${INSTALL_DIR}/.config.lock"
declare MOODLE_CFG_FILE="${INSTALL_DIR}/config.php"
declare DBUSER="moodle"

export MYSQL_PWD=$dbpassMOODLE

#
# Crée la configuration de moodle pour les fresh install
# Tout se fait en base via du SQL
#
function make_config()
{

    if [[ -n ${eolesso_port} ]]
    then
        SSO_PORT=${eolesso_port}
    else
        SSO_PORT=8443
    fi

    if [[ -n ${eolesso_adresse} ]]
    then
        SSO_ADDR=${eolesso_adresse}
    else
        SSO_ADDR=${adresse_ip_web}
    fi

    if [[ -n ${web_url} ]]
    then
        EMAIL="noreply@${web_url}"
        CHAT_SRV_HOST=${web_url}
    fi

    $RunMysql -h ${dbhostMOODLE} -u ${DBUSER} <<__EOF__
    \r moodle
    DELETE FROM \`mdl_config_plugins\` WHERE \`plugin\`='auth_cas' AND \`name\`='field_updateremote_firstnamephonetic ';
    
    INSERT IGNORE INTO \`mdl_config_plugins\` (\`plugin\`, \`name\`, \`value\`) VALUES
    ('auth_cas', 'hostname', '${SSO_ADDR}'),
    ('auth_cas', 'port', '${SSO_PORT}'),
    ('auth_cas', 'start_tls', '0'),
    ('auth_cas', 'casversion', '2.0'),
    ('auth_cas', 'baseuri', ''),
    ('auth_cas', 'language', 'french'),
    ('auth_cas', 'proxycas', '0'),
    ('auth_cas', 'logoutcas', '1'),
    ('auth_cas', 'multiauth', '0'),
    ('auth_cas', 'certificate_check', '0'),
    ('auth_cas', 'certificate_path', ''),
    ('auth_cas', 'host_url', 'ldap://${adresse_ip_ldap}'),
    ('auth_cas', 'ldapencoding', 'utf-8'),
    ('auth_cas', 'contexts', '${ldap_base_dn}'),
    ('auth_cas', 'user_type', 'default'),
    ('auth_cas', 'user_attribute', 'uid'),
    ('auth_cas', 'search_sub', '1'),
    ('auth_cas', 'opt_deref', '0'),
    ('auth_cas', 'bind_dn', ''),
    ('auth_cas', 'bind_pw', ''),
    ('auth_cas', 'ldap_version', '2'),
    ('auth_cas', 'objectclass', '(&(objectclass=inetOrgPerson)(!(description=Computer)))'),
    ('auth_cas', 'memberattribute', 'memberuid'),
    ('auth_cas', 'memberattribute_isdn', ''),
    ('auth_cas', 'attrcreators', 'ENTPersonProfils=enseignant'),
    ('auth_cas', 'groupecreators', 'ENTPersonProfils=enseignant'),
    ('auth_cas', 'removeuser', '2'),
    ('auth_cas', 'field_map_firstname', 'givenName'),
    ('auth_cas', 'field_updatelocal_firstname', 'oncreate'),
    ('auth_cas', 'field_updateremote_firstname', '0'),
    ('auth_cas', 'field_lock_firstname', 'unlocked'),
    ('auth_cas', 'field_map_lastname', 'sn'),
    ('auth_cas', 'field_updatelocal_lastname', 'oncreate'),
    ('auth_cas', 'field_updateremote_lastname', '0'),
    ('auth_cas', 'field_lock_lastname', 'unlocked'),
    ('auth_cas', 'field_map_email', 'mail'),
    ('auth_cas', 'field_updatelocal_email', 'oncreate'),
    ('auth_cas', 'field_updateremote_email', '0'),
    ('auth_cas', 'field_lock_email', 'unlocked'),
    ('auth_cas', 'field_map_city', ''),
    ('auth_cas', 'field_updatelocal_city', 'oncreate'),
    ('auth_cas', 'field_updateremote_city', '0'),
    ('auth_cas', 'field_lock_city', 'unlocked'),
    ('auth_cas', 'field_map_country', ''),
    ('auth_cas', 'field_updatelocal_country', 'oncreate'),
    ('auth_cas', 'field_updateremote_country', '0'),
    ('auth_cas', 'field_lock_country', 'unlocked'),
    ('auth_cas', 'field_map_lang', ''),
    ('auth_cas', 'field_updatelocal_lang', 'oncreate'),
    ('auth_cas', 'field_updateremote_lang', '0'),
    ('auth_cas', 'field_lock_lang', 'unlocked'),
    ('auth_cas', 'field_map_description', ''),
    ('auth_cas', 'field_updatelocal_description', 'oncreate'),
    ('auth_cas', 'field_updateremote_description', '0'),
    ('auth_cas', 'field_lock_description', 'unlocked'),
    ('auth_cas', 'field_map_url', ''),
    ('auth_cas', 'field_updatelocal_url', 'oncreate'),
    ('auth_cas', 'field_updateremote_url', '0'),
    ('auth_cas', 'field_lock_url', 'unlocked'),
    ('auth_cas', 'field_map_idnumber', ''),
    ('auth_cas', 'field_updatelocal_idnumber', 'oncreate'),
    ('auth_cas', 'field_updateremote_idnumber', '0'),
    ('auth_cas', 'field_lock_idnumber', 'unlocked'),
    ('auth_cas', 'field_map_institution', ''),
    ('auth_cas', 'field_updatelocal_institution', 'oncreate'),
    ('auth_cas', 'field_updateremote_institution', '0'),
    ('auth_cas', 'field_lock_institution', 'unlocked'),
    ('auth_cas', 'field_map_department', ''),
    ('auth_cas', 'field_updatelocal_department', 'oncreate'),
    ('auth_cas', 'field_updateremote_department', '0'),
    ('auth_cas', 'field_lock_department', 'unlocked'),
    ('auth_cas', 'field_map_phone1', ''),
    ('auth_cas', 'field_updatelocal_phone1', 'oncreate'),
    ('auth_cas', 'field_updateremote_phone1', '0'),
    ('auth_cas', 'field_lock_phone1', 'unlocked'),
    ('auth_cas', 'field_map_phone2', ''),
    ('auth_cas', 'field_updatelocal_phone2', 'oncreate'),
    ('auth_cas', 'field_updateremote_phone2', '0'),
    ('auth_cas', 'field_lock_phone2', 'unlocked'),
    ('auth_cas', 'field_map_address', ''),
    ('auth_cas', 'field_updatelocal_address', 'oncreate'),
    ('auth_cas', 'field_updateremote_address', '0'),
    ('auth_cas', 'field_lock_address', 'unlocked'),
    ('auth_cas', 'field_map_alternatename', ''),
    ('auth_cas', 'field_updatelocal_alternatename', 'oncreate'),
    ('auth_cas', 'field_updateremote_alternatename', '0'),
    ('auth_cas', 'field_lock_alternatename', 'unlocked'),
    ('auth_cas', 'field_map_firstnamephonetic', ''),
    ('auth_cas', 'field_updatelocal_firstnamephonetic', 'oncreate'),
    ('auth_cas', 'field_updateremote_firstnamephonetic', '0'),
    ('auth_cas', 'field_lock_firstnamephonetic', 'unlocked'),
    ('auth_cas', 'field_map_lastnamephonetic', ''),
    ('auth_cas', 'field_updatelocal_lastnamephonetic', 'oncreate'),
    ('auth_cas', 'field_updateremote_lastnamephonetic', '0'),
    ('auth_cas', 'field_lock_lastnamephonetic', 'unlocked'),
    ('auth_cas', 'field_map_middlename', ''),
    ('auth_cas', 'field_updatelocal_middlename', 'oncreate'),
    ('auth_cas', 'field_updateremote_middlename', '0'),
    ('auth_cas', 'field_lock_middlename', 'unlocked'),
    ('auth_cas', 'logout_return_url', ''),
    ('auth_cas', 'curl_ssl_version', ''),
    ('auth_cas', 'pagesize', '250'),
    ('auth_cas', 'version', '2017051500');

    INSERT IGNORE INTO mdl_config (name, value) VALUES ('siteadmins','');
    INSERT IGNORE INTO mdl_config (name, value) VALUES ('proxyhost','');
    INSERT IGNORE INTO mdl_config (name, value) VALUES ('proxyport','');

__EOF__

    # Mode visiteur
    if [[ "${activer_moodlevisiteur}" = "oui" ]]
    then
$RunMysql -h ${dbhostMOODLE} -u ${DBUSER} <<__EOF__
\r moodle
        UPDATE \`mdl_config\` SET value = 0 WHERE name = 'forcelogin';
__EOF__
    else
$RunMysql -h ${dbhostMOODLE} -u ${DBUSER} <<__EOF__
\r moodle
        UPDATE \`mdl_config\` SET value = 1 WHERE name = 'forcelogin';
__EOF__
    fi

    # Configuration du proxy
    if [[ -z "${proxy_client_adresse}" ]]
    then
$RunMysql -h ${dbhostMOODLE} -u ${DBUSER} <<__EOF__
\r moodle
    UPDATE mdl_config SET value = '' WHERE name = 'proxyhost';
    UPDATE mdl_config SET value = '' WHERE name = 'proxyport';
__EOF__
    else
$RunMysql -h ${dbhostMOODLE} -u ${DBUSER} <<__EOF__
\r moodle
    UPDATE mdl_config SET value = '${proxy_client_adresse}' WHERE name = 'proxyhost';
    UPDATE mdl_config SET value = '${proxy_client_port}' WHERE name = 'proxyport';
__EOF__
    fi
}

#
# Met à jour des paramètres de configuration qui peuvent changer
# cette fonction est lancée a chaque reconfigure.
#
function update_config()
{
	make_config

    if [[ -n ${eolesso_port} ]]
    then
        SSO_PORT=${eolesso_port}
    else
        SSO_PORT=8443
    fi

    if [[ -n ${eolesso_adresse} ]]
    then
        SSO_ADDR=${eolesso_adresse}
    else
        SSO_ADDR=${adresse_ip_web}
    fi

    if [[ -n ${web_url} ]]
    then
        EMAIL="noreply@${web_url}"
        CHAT_SRV_HOST=${web_url}
    fi

    $RunMysql -h ${dbhostMOODLE} -u ${DBUSER} <<__EOF__
\r moodle
UPDATE \`mdl_user\`   SET auth = 'cas', password = 'not cached' WHERE username = 'admin';
UPDATE \`mdl_config\` SET value = 'cas' WHERE name = 'auth';
UPDATE \`mdl_config\` SET value = CASE
		WHEN value = '' THEN '${EMAIL}'
		ELSE value
		END
		WHERE name = 'noreplyaddress';
UPDATE \`mdl_config\` SET value = IF(value IS NULL OR value = '', '${default_admin_email}', value) WHERE name = 'supportemail';
UPDATE \`mdl_config\` SET value = '${CHAT_SRV_HOST}' WHERE name = 'chat_serverhost';
UPDATE \`mdl_config\` SET value='0' WHERE name='dbsessions';
UPDATE \`mdl_config\` SET value='rest,xmlrpc' WHERE name='webserviceprotocols';

UPDATE \`mdl_config_plugins\` SET value='35'  WHERE plugin='logstore_standard' AND name='loglifetime';

UPDATE \`mdl_config_plugins\` SET value = 'ldap://${adresse_ip_ldap}' WHERE plugin = 'auth_cas' AND name = 'host_url';
UPDATE \`mdl_config_plugins\` SET value = '${ldap_base_dn}' WHERE plugin = 'auth_cas' AND name = 'contexts';
UPDATE \`mdl_config_plugins\` SET value = '${SSO_ADDR}' WHERE plugin = 'auth_cas' AND name = 'hostname';
UPDATE \`mdl_config_plugins\` SET value = '${SSO_PORT}'  WHERE plugin = 'auth_cas' AND name = 'port';
UPDATE \`mdl_config_plugins\` SET value = '${eolesso_cas_folder}'  WHERE plugin = 'auth_cas' AND name = 'baseuri';
UPDATE \`mdl_config_plugins\` SET value = '2.0' WHERE plugin = 'auth_cas' AND name = 'casversion';
UPDATE \`mdl_config_plugins\` SET value = '2017051500'  WHERE plugin = 'auth_cas' AND name = 'version';

UPDATE \`mdl_config_plugins\` SET value = '1'  WHERE plugin = 'auth_cas' AND name = 'search_sub';
UPDATE \`mdl_config_plugins\` SET value = '2'  WHERE plugin = 'auth_cas' AND name = 'ldap_version';
UPDATE \`mdl_config_plugins\` SET value = '1'  WHERE plugin = 'auth_cas' AND name = 'logoutcas';

UPDATE \`mdl_config_plugins\` SET value = '(&(objectclass=inetOrgPerson)(!(description=Computer)))'  WHERE plugin = 'auth_cas' AND name = 'objectclass';
UPDATE \`mdl_config_plugins\` SET value = 'memberuid'  WHERE plugin = 'auth_cas' AND name = 'memberattribute';
UPDATE \`mdl_config_plugins\` SET value = 'ENTPersonProfils'  WHERE plugin = 'auth_cas' AND name = 'attrcreators';
UPDATE \`mdl_config_plugins\` SET value = 'ENTPersonProfils=enseignant'  WHERE plugin = 'auth_cas' AND name = 'groupecreators';
UPDATE \`mdl_config_plugins\` SET value = '2'  WHERE plugin = 'auth_cas' AND name = 'removeuser';
UPDATE \`mdl_config_plugins\` SET value = 'uid'  WHERE plugin = 'auth_cas' AND name = 'user_attribute';

UPDATE \`mdl_config_plugins\` SET value = 'givenName'  WHERE plugin = 'auth_cas' AND name = 'field_map_firstname';
UPDATE \`mdl_config_plugins\` SET value = 'onlogin'  WHERE plugin = 'auth_cas' AND name = 'field_updatelocal_firstname';

UPDATE \`mdl_config_plugins\` SET value = 'sn'  WHERE plugin = 'auth_cas' AND name = 'field_map_lastname';
UPDATE \`mdl_config_plugins\` SET value = 'onlogin'  WHERE plugin = 'auth_cas' AND name = 'field_updatelocal_lastname';

UPDATE \`mdl_config_plugins\` SET value = 'mail'  WHERE plugin = 'auth_cas' AND name = 'field_map_email';
UPDATE \`mdl_config_plugins\` SET value = 'onlogin'  WHERE plugin = 'auth_cas' AND name = 'field_updatelocal_email';
UPDATE \`mdl_config_plugins\` SET value = 'CAS_Languages_French'  WHERE plugin = 'auth_cas' AND name = 'language';

INSERT IGNORE INTO mdl_external_services (id, name, enabled, requiredcapability, restrictedusers, component, timecreated, timemodified, shortname, downloadfiles, uploadfiles) VALUES ('-100','WebService',	1,	'',	1,	NULL,	1519718832,	NULL,	'',	0,	0);
DELETE FROM mdl_external_services_functions WHERE externalserviceid=-100;
INSERT IGNORE INTO mdl_external_services_functions (externalserviceid, functionname) VALUES (-100, 'core_user_get_users'), (-100, 'core_enrol_get_users_courses'), (-100, 'core_course_get_courses'), (-100, 'core_enrol_get_enrolled_users'), (-100, 'core_enrol_get_enrolled_users_with_capability');
DELETE FROM mdl_external_services_users WHERE externalserviceid=-100; 
INSERT IGNORE INTO mdl_external_services_users (externalserviceid, userid, iprestriction, validuntil, timecreated) VALUES (-100,	(SELECT id FROM mdl_user WHERE username='admin'),	NULL,	NULL,	1519718973);
DELETE FROM mdl_external_tokens WHERE id=-100;
INSERT IGNORE INTO mdl_external_tokens (id, token, privatetoken, tokentype, userid, externalserviceid, sid, contextid, creatorid, iprestriction, validuntil, timecreated, lastaccess) VALUES (-100,	'montoken',	NULL,	0,	(SELECT id FROM mdl_user WHERE username='admin'),	-100,	NULL,	1,	(SELECT id FROM mdl_user WHERE username='admin'),	NULL,	0,	1519718995,	NULL);

__EOF__

ret1=$($RunMysql -h ${dbhostMOODLE} -u ${DBUSER} --batch --skip-column-names -Dmoodle -e "SELECT value FROM mdl_config WHERE name = 'siteadmins'")
ret2=$($RunMysql -h ${dbhostMOODLE} -u ${DBUSER} --batch --skip-column-names -Dmoodle -e "SELECT id FROM mdl_user WHERE username='admin' OR username='${uid_addadmin}' OR id IN("$ret1") GROUP BY id")
ret3=`echo $ret2 | sed 's/ /,/g'`
ret4=$($RunMysql -h ${dbhostMOODLE} -u ${DBUSER} --batch --skip-column-names -Dmoodle -e "UPDATE mdl_config SET value = '$ret3' WHERE name = 'siteadmins'")

ret5=$($RunMysql -h ${dbhostMOODLE} -u ${DBUSER} --batch --skip-column-names -Dmoodle -e "UPDATE mdl_external_tokens SET token = '$moodle_apikey' WHERE  id = -100")
}

#
# Sauvegarde le fichier de configuration de moodle
#
function backup_moodle_conf
{

   if [[ -f "${container_path_web}/${MOODLE_CFG_FILE}" ]]
   then
      $RunCmd "mv ${MOODLE_CFG_FILE} ${MOODLE_CFG_FILE}.bck" web
   fi
}

#
# Refresh du cache
#
function refresh_cache
{
   # if [[ -d "${container_path_web}/${DATA_DIR}" ]]
   # then
      # $RunCmd "rm -rf ${DATA_DIR}/cache/*" web
   # fi
   $RunCmd "$BIN_PHP /var/www/html/moodle/admin/cli/purge_caches.php" web
}


#
# Lance l'installation de moodle
# ceci est appellé lors des fresh install
#
function install_moodle
{

   backup_moodle_conf

   if [[ -n ${web_url} ]]
   then
       URL="https://${web_url}/moodle"
   else
       URL="https://${adresse_ip_web}/moodle"
   fi

   DATA_ROOT="/home/www-data/var/www/html/moodle"

   CMD="cd ${INSTALL_DIR} ; ${BIN_PHP} ${INSTALL_SCRIPT}        \
       --lang=${LANG}  --wwwroot=${URL} --dataroot=${DATA_ROOT} \
       --dbtype=mysqli --dbhost=${dbhostMOODLE}             \
       --dbname=moodle --dbuser=${DBUSER} --dbpass='${dbpassMOODLE}'    \
       --prefix=mdl_ --shortname=envole --fullname=Envole       \
       --adminuser=admin --adminpass=test --non-interactive --agree-license \
       --chmod=2750"

   $RunCmd "/bin/bash -c \"${CMD}\"" web

   make_config

   $RunCmd "touch ${LOCK}" web
   $RunCmd "chmod 600 ${LOCK}" web

}

#
# Lance la mise à jour de la base de moodle
#
function update_moodle
{
    echo "    >> Conversion utf8mb4"
    $RunCmd "$BIN_PHP /var/www/html/moodle/admin/cli/mysql_collation.php --collation=utf8mb4_unicode_ci > /tmp/moodle-convert.log" web
    CMD="cd ${INSTALL_DIR} ; ${BIN_PHP} ${UPGRADE_SCRIPT} --non-interactive"

    $RunCmd "/bin/bash -c \"${CMD}\"" web
    RET=${?}

    $RunCmd "touch ${LOCK}" web
    $RunCmd "chmod 600 ${LOCK}" web

    return ${RET}
}

function droits_moodle
{
    # attribution des droits minimaux
    $RunCmd "chown -R root:www-data ${INSTALL_DIR}" web
    $RunCmd "chmod -R 640 ${INSTALL_DIR}" web
    $RunCmd "chmod -R ug+X ${INSTALL_DIR}" web
    $RunCmd "chmod +x ${INSTALL_DIR}/filter/tex/mimetex.linux" web

    if [[ -d "${MOOSH_DIR}" ]]
    then
        $RunCmd "chown -R root:root ${MOOSH_DIR}" web
    fi

    $RunCmd "chown -R root:www-data ${DATA_DIR}" web
    $RunCmd "chmod -R 770 ${DATA_DIR}" web

    file_path="var/www/html/moodle/writeable_folders"
    con_path=${container_path_web}/${file_path}
    for i in `cat ${con_path}`;
    do
        if [[ -d "${i}" ]]
        then
            $RunCmd "chmod -R g+w ${i}" web
        fi
    done    
}

function delTheme 
{
    if [[ "$theme" != "$1" ]]
    then
        $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-uninstall theme_$1 > /tmp/null" web
    fi

}

if [[ "${activer_moodle}" = "oui" ]]
then

    # Suppression des themes incompatible
    $RunCmd "rm -rf ${INSTALL_DIR}/theme/cloud" web
    $RunCmd "rm -rf ${INSTALL_DIR}/theme/aardvark" web
    $RunCmd "rm -rf ${INSTALL_DIR}/theme/essential" web

    # Refresh du cache
    echo "    >> Refresh du cache"
    refresh_cache

    # On lance la mise à jour de la base de moodle
    echo "    >> Mise à jour de Moodle"
    update_moodle
    if [[ ${?} -eq 1 ]]
    then
        # En cas de problème on lance l'installation complète
        echo "    >> Moodle n'est pas installé"
        echo "    >> Installation de Moodle"
        install_moodle
        update_moodle
    fi

    # Mise à jour des paramètres configurations de moodle
    echo "    >> Mise à jour de la configuration"
    update_config
    
    $RunCmd "chgrp www-data ${MOODLE_CFG_FILE}" web
    $RunCmd "chown -R root:www-data ${DATA_DIR}" web
    $RunCmd "chmod -R 770 ${DATA_DIR}" web

    # Installation des plugins
    echo "    >> Installation Plugins"
    $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} config-set converter_plugins_sortorder unoconv > /tmp/null" web

    if [[ ! -d ${container_path_web}${INSTALL_DIR}/mod/hvp ]]
    then
        $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-list > /tmp/null" web
        $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-install -d mod_geogebra > /tmp/null" web
        $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-install -d mod_hvp > /tmp/null" web
        $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-install -d mod_questionnaire > /tmp/null" web
        $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-install -d qtype_ddmatch > /tmp/null" web
        $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-install -d tinymce_clozeeditor > /tmp/null" web

        $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-uninstall filter_nanogong > /tmp/null" web

    fi

    # Suppression des plugins obsolètes
    # echo "    >> Suppression des plugins obsolètes"
    # $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-uninstall mod_bigbluebuttonbn > /tmp/null" web
    # $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-uninstall qtype_poodllrecording > /tmp/null" web
    # $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-uninstall filter_poodll > /tmp/null" web
    # $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-uninstall tinymce_poodll > /tmp/null" web
    # $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-uninstall repository_poodll > /tmp/null" web
    # $RunCmd "$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} plugin-uninstall local_envole > /tmp/null" web
    # $RunCmd "rm -rf ${INSTALL_DIR}/cache/stores/memcache" web

    # Suppression des plugins obsolètes
    echo "    >> Suppression des themes obsolètes"
    theme=`$BIN_PHP /${MOOSH_DIR}/moosh.php -p=${INSTALL_DIR} config-get core theme`
    # delTheme afterburner
    # delTheme anomaly
    # delTheme arialist
    # delTheme binarius
    # delTheme boxxie
    # delTheme brick
    # delTheme chameleon
    # delTheme cornflower
    # delTheme custom_corners
    # delTheme envole
    # delTheme formal_white
    # delTheme formfactor
    # delTheme fusion
    # delTheme leatherbound
    # delTheme magazine
    # delTheme metal
    # delTheme nimble
    # delTheme nonzero
    # delTheme oceanblue
    # delTheme orangewhite
    # delTheme orangewhitepda
    # delTheme overlay
    # delTheme serenity
    # delTheme sky_high
    # delTheme splash
    # delTheme standard
    # delTheme standardblue
    # delTheme standardgreen
    # delTheme standardlogo
    # delTheme standardold
    # delTheme standardred
    # delTheme standardwhite
    # delTheme wood

    # Refresh du cache
    echo "    >> Refresh du cache"
    refresh_cache

    echo "    >> Mise à jour permissions"
    droits_moodle

    echo 
fi
