    RUTORRENT_VERSION="3.10"
    if [ ! -d "${APP_DIRECTORY}" ] ; then
        MKDIR "${APP_DIRECTORY}" || break
#        GIT_CLONE "https://github.com/Novik/ruTorrent" "${APP_DIRECTORY}" || break

        WGET "https://github.com/Novik/ruTorrent/archive/refs/tags/v${RUTORRENT_VERSION}.tar.gz" \
             "${TMP_DIR}/rutorrent.tar.gz" \
          || break
        TAR_ZXF "${TMP_DIR}/rutorrent.tar.gz" "${TMP_DIR}/" \
          || break

        RUN_LOG "mv -f '${TMP_DIR}/ruTorrent-${RUTORRENT_VERSION}'/* '${APP_DIRECTORY}'/"


        # Configure Screenshots plugin
        RUN_LOG "sed -i -e \"s|'exfrmcount'=>.*,|'exfrmcount'=>10,|\" \
            '${APP_DIRECTORY}/plugins/screenshots/ffmpeg.php'"
        RUN_LOG "sed -i -e \"s|'exfrminterval'=>.*,|'exfrminterval'=>90,|\" \
            '${APP_DIRECTORY}/plugins/screenshots/ffmpeg.php'"

        JOB_NAME="Creating '${APP_DIRECTORY}/.htaccess' file"
        JOB_START "${JOB_NAME}"
        cat <<-EOF > "${APP_DIRECTORY}/.htaccess" 2>> "${LOGFILE}"
		AuthType Basic
		AuthName "ruTorrent Login"
		AuthUserFile  "${APP_DIRECTORY}/.htpasswd"
		require valid-user
		EOF
        [ $? -ne 0 ] && ERROR "Error code: $?"
        JOB_END "${JOB_NAME}"

        # install rutorrent-thirdparty-plugins
        GIT_CLONE "https://github.com/nelu/rutorrent-thirdparty-plugins" \
                  "${TMP_DIR}/rutorrent-thirdparty-plugins"
        if [ $? -eq 0 ] ; then
            CP_DIR "${TMP_DIR}/rutorrent-thirdparty-plugins/." \
                   "${APP_DIRECTORY}/plugins/"
            #
            # -- Disable Screenshots plugin in File Manager (it is broken as of Aug-2016)
            cat <<-EOF > "${APP_DIRECTORY}/plugins/filemanager/scripts/screens" 2>> "${LOGFILE}"
		#!/bin/bash

		SCRIPT_VER="1.0"
		echo "FATAL ERROR: this plugin is broken." >> "\$2/log"
		echo "FATAL ERROR: Please use standard ruTorrent's Screenshot plugin." >> "\$2/log"
		exit
		EOF
            CHMOD_FILE 755 "${APP_DIRECTORY}/plugins/filemanager/scripts/screens"

            #
            # -- Disable context menu for Filemanager's Screenshots plugin
            RUN_LOG "sed -i -e 's|^\(\s*create_sub\.push(\[theUILang\.fcScreens.*\)$|//\1|' \
                '${APP_DIRECTORY}/plugins/filemanager/init.js' 2>&1"
            #
            # -- Do not show "hidden" files
            RUN_LOG "sed -i -e 's|^\(.*showhidden:\).*,.*$|\1 false,|' \
                '${APP_DIRECTORY}/plugins/filemanager/init.js' 2>&1"
            # -- Remove 'mediastream' plugin
            # this will remove 'View' context menu entry from 'filemanager' plugin
            RM_DIRS "${APP_DIRECTORY}/plugins/mediastream"

#            # -- Remove 'fileupload' plugin
#            # !!! BROKEN as of 2022/01/11 !!!
#            RM_DIRS "${APP_DIRECTORY}/plugins/fileupload"

            # -- Remove '_cloudflare' plugin
            RM_DIRS "${APP_DIRECTORY}/plugins/_cloudflare"
            #
            # -- Fix `fileshare` plugin
            LN "/var/www/rutorrent/plugins/fileshare/share.php" \
               "/var/www/html/share.php" \
             || break
            RUN_LOG "sed -i -e 's|^\\\$downloadpath.*|\\\$downloadpath = \"https://\".\\\$_SERVER['\\''HTTP_X_FORWARDED_HOST'\\''].\"/share.php\";|' '${APP_DIRECTORY}/plugins/fileshare/conf.php'" \
             || break

        fi

        # install ratiocolor plugin
        GIT_CLONE "https://github.com/Gyran/rutorrent-ratiocolor" "${APP_DIRECTORY}/plugins/ratiocolor"

        # install rutorrent logoff plugin
        # !!! BROKEN as of 2022/01/11 !!!
        WGET \
            "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/rutorrent-logoff/logoff-1.3.tar.gz" \
            "${TMP_DIR}/logoff.tar.gz"
        TAR_ZXF "${TMP_DIR}/logoff.tar.gz" "${APP_DIRECTORY}/plugins/"

        # install `mobile` plugin
        GIT_CLONE "https://github.com/xombiemp/rutorrentMobile" \
                  "${APP_DIRECTORY}/plugins/mobile"
        # remove `ipad` plugin - it conflicts with the `mobile` plugin
        [ -d "${APP_DIRECTORY}/plugins/ipad" ] \
            && RM_DIRS "${APP_DIRECTORY}/plugins/ipad"


        # 20190904: Install ruTorrent themes
        # https://github.com/artyuum/ruTorrent-Themes
        for theme in \
          "Agent34" \
          "Agent46" \
          "club-quad" \
          "club-Swizards" \
          "QuickBox-Dark" \
          "MaterialDesign" \
          "FlatUI_Dark" \
          "FlatUI_Light" \
          "FlatUI_Material" \
        ; do
            WGET \
                "${SUPPORT}/rutorrent/${theme}.tar.gz" \
              || break 2
            TAR_ZXF "${TMP_DIR}/${theme}.tar.gz" "${APP_DIRECTORY}/plugins/theme/themes/" \
              || break 2
        done
#        # Set "club-Swizards" theme for rutorrent
#        perl -pi -e "s/\$defaultTheme \= \"\"\;/\$defaultTheme \= \"club-Swizards\"\;/g" \
#               "${APP_DIRECTORY}/plugins/theme/conf.php" \
#            >> "${LOGFILE}" 2>&1

        #
        # Set rutorrent directory owner & permissions
        CHOWN_DIR "${APACHE_USER}":"${APACHE_GROUP}" "${APP_DIRECTORY}"
        RUN_LOG "find '${APP_DIRECTORY}' -type d -exec chmod 0755 {} \; 2>&1"

        # Configure session directory
        [ ! -e "${APP_SESSION_DIRECTORY}" ] \
            && MKDIR "${APP_SESSION_DIRECTORY}"
        CHOWN_DIR "${APACHE_USER}":"${APACHE_GROUP}" "${APP_SESSION_DIRECTORY}"
        CHMOD_FILE 775 "${APP_SESSION_DIRECTORY}"

        if [ ! -f "/etc/apache2/sites-available/rutorrent-8080.conf" ] ; then
            [ -e "/etc/apache2/sites-available/rutorrent-8080.conf" ] \
                && RM_FILES "/etc/apache2/sites-available/rutorrent-8080.conf"
            WGET \
                "${SUPPORT}/rutorrent/rutorrent-8080.conf" \
                "/etc/apache2/sites-available/rutorrent-8080.conf"
            if [ -f "/etc/apache2/sites-available/rutorrent-8080.conf" ] ; then
                INFO "Enabling Apache2 site rutorrent-8080.conf"
                RUN_LOG "${A2ENSITE} rutorrent-8080.conf 2>&1"
            fi

            case "${LSB_VENDOR}" in
              "CentOS"|"CentOSStream")
                  ;;
              "Debian"|"Ubuntu")
                  RUN_LOG "a2enmod proxy 2>&1"
                  RUN_LOG "a2enmod proxy_http 2>&1"
                  RUN_LOG "a2enmod headers 2>&1"
                  RUN_LOG "a2enmod rewrite 2>&1"
                  ;;
            esac

        fi

    fi

    INFO "Configuring rtorrent environment for '${USERNAME}'"

    RUN_LOG "usermod -a -G ${APACHE_GROUP} ${USERNAME} 2>&1"

    _htpasswd_flags=""
    [ ! -f "${APP_DIRECTORY}/.htpasswd" ] && _htpasswd_flags="-c"
    RUN_LOG "echo '${USERPASSWORD}' | \
          htpasswd -i ${_htpasswd_flags} '${APP_DIRECTORY}/.htpasswd' ${USERNAME} 2>&1"

    MKDIR "${APP_SESSION_DIRECTORY}/${USERNAME}"
    CHOWN_DIR "${APACHE_USER}":"${APACHE_GROUP}" "${APP_SESSION_DIRECTORY}/${USERNAME}"
    CHMOD_FILE 775 "${APP_SESSION_DIRECTORY}/${USERNAME}"

    #
    MKDIR "${APP_DIRECTORY}/conf/users/${USERNAME}"
    if [ $? -eq 0 ] ; then
        CP_FILE "${APP_DIRECTORY}/conf/config.php" \
              "${APP_DIRECTORY}/conf/users/${USERNAME}/" 2>&1
        RUN_LOG "sed -i -e \"s|\\\$topDirectory\s*=\s*'.*'|\\\$topDirectory = '###USERHOME###'|\" \
            '${APP_DIRECTORY}/conf/users/${USERNAME}/config.php' 2>&1"
        RUN_LOG "sed -i -e \"s|\\\$XMLRPCMountPoint\s*=\s*\\\".*\\\"|\\\$XMLRPCMountPoint = '###RPC_MOUNT_POINT###'|\" \
            '${APP_DIRECTORY}/conf/users/${USERNAME}/config.php' 2>&1"
        RUN_LOG "sed -i -e \"s|\\\$scgi_port\s*=\s*.*;|\\\$scgi_port = ###RTORRENT_SCGI_PORT###;|\" \
            '${APP_DIRECTORY}/conf/users/${USERNAME}/config.php' 2>&1"
        RUN_LOG "echo \"s|###USERHOME###|${USERHOME}|g\" | \
            sed -f - -i '${APP_DIRECTORY}/conf/users/${USERNAME}/config.php' 2>&1"
        RUN_LOG "echo \"s|###RPC_MOUNT_POINT###|${RPC_MOUNT_POINT}|g\" | \
            sed -f - -i '${APP_DIRECTORY}/conf/users/${USERNAME}/config.php' 2>&1"
        RUN_LOG "echo \"s|###RTORRENT_SCGI_PORT###|${RTORRENT_SCGI_PORT}|g\" | \
            sed -f - -i '${APP_DIRECTORY}/conf/users/${USERNAME}/config.php' 2>&1"
    fi

    for _dir in \
       "/etc/apache2/sites-available" \
       "/etc/apache2/sites-enabled" \
       "/etc/apache2/passwords-available" \
       "/etc/apache2/passwords-enabled" \
       "/etc/apache2/userconf-enabled" \
    ; do
        [ ! -d "${_dir}" ] && MKDIR "${_dir}"
    done

    [ -e "/etc/apache2/sites-available/rtorrentscgi.conf" ] \
        && RM_FILES "/etc/apache2/sites-available/rtorrentscgi.conf"
    case "${LSB_VENDOR}" in
      "CentOS"|"CentOSStream")
          WGET \
              "${SUPPORT}/rutorrent/rtorrentscgi-rh.conf" \
              "/etc/apache2/sites-available/rtorrentscgi.conf"
          ;;
      "Debian"|"Ubuntu")
          WGET \
              "${SUPPORT}/rutorrent/rtorrentscgi-deb.conf" \
              "/etc/apache2/sites-available/rtorrentscgi.conf"
          ;;
    esac
    [ -e "/etc/apache2/passwords-available/rtorrentscgi.conf" ] \
        && RM_FILES "/etc/apache2/passwords-available/rtorrentscgi.conf"
    WGET \
        "${SUPPORT}/rutorrent/passwords-rtorrentscgi.conf" \
        "/etc/apache2/passwords-available/rtorrentscgi.conf"
    CP_FILE "/etc/apache2/sites-available/rtorrentscgi.conf" \
        "/etc/apache2/sites-available/rtorrentscgi-${USERNAME}.conf"
    RUN_LOG "echo \"s|###USERNAME###|${USERNAME}|g\" | \
        sed -f - -i '/etc/apache2/sites-available/rtorrentscgi-${USERNAME}.conf' 2>&1"
    RUN_LOG "echo \"s|###RTORRENT_SCGI_PORT###|${RTORRENT_SCGI_PORT}|g\" | \
        sed -f - -i '/etc/apache2/sites-available/rtorrentscgi-${USERNAME}.conf' 2>&1"
    RUN_LOG "${A2ENSITE} rtorrentscgi-${USERNAME}.conf 2>&1"
    CP_FILE "/etc/apache2/passwords-available/rtorrentscgi.conf" \
        "/etc/apache2/passwords-available/rtorrentscgi-${USERNAME}.conf"
    RUN_LOG "echo \"s|^user:|${USERNAME}:|g\" | \
        sed -f - -i '/etc/apache2/passwords-available/rtorrentscgi-${USERNAME}.conf' 2>&1"
    RUN_LOG "ln -svf '/etc/apache2/passwords-available/rtorrentscgi-${USERNAME}.conf' \
        '/etc/apache2/passwords-enabled/rtorrentscgi-${USERNAME}.conf' 2>&1"

    if [ ${USERUID} -eq ${FIRST_UID} ] ; then
        cat <<-EOF > "/etc/apache2/userconf-enabled/rutorrent-${USERNAME}.conf"
	<IfModule mod_proxy.c>

	    ProxyRequests off
	    ProxyPreserveHost On

	    RewriteEngine on
	    RewriteRule ^/rutorrent$ /rutorrent/ [R=permanent]
	    RewriteRule ^/rutorrent-${USERNAME}$ /rutorrent-${USERNAME}/ [R=permanent]

	    <Location "/rutorrent/">
	        Options +Indexes +Includes +FollowSymLinks +MultiViews
	        AllowOverride None
	        AuthType Basic
	        AuthName "ruTorrent"
	        AuthBasicProvider file
	        AuthUserFile  "/var/www/rutorrent/.htpasswd"
	        Require user ${USERNAME}
	        ProxyPass http://127.0.0.1:8080/
	        ProxyPassReverse http://127.0.0.1:8080/
	    </Location>

	    <Location "/rutorrent-${USERNAME}/">
	        Options +Indexes +Includes +FollowSymLinks +MultiViews
	        AllowOverride None
	        AuthType Basic
	        AuthName "ruTorrent"
	        AuthBasicProvider file
	        AuthUserFile  "/var/www/rutorrent/.htpasswd"
	        Require user ${USERNAME}
	        ProxyPass http://127.0.0.1:8080/
	        ProxyPassReverse http://127.0.0.1:8080/
	    </Location>

	</IfModule>
	EOF
    else
        cat <<-EOF > "/etc/apache2/userconf-enabled/rutorrent-${USERNAME}.conf"
	<IfModule mod_proxy.c>

	    ProxyRequests off
	    ProxyPreserveHost On

	    RewriteEngine on
	    RewriteRule ^/rutorrent-${USERNAME}$ /rutorrent-${USERNAME}/ [R=permanent]

	    <Location "/rutorrent-${USERNAME}/">
	        Options +Indexes +Includes +FollowSymLinks +MultiViews
	        AllowOverride None
	        AuthType Basic
	        AuthName "ruTorrent"
	        AuthBasicProvider file
	        AuthUserFile  "/var/www/rutorrent/.htpasswd"
	        Require user ${USERNAME}
	        ProxyPass http://127.0.0.1:8080/
	        ProxyPassReverse http://127.0.0.1:8080/
	    </Location>

	</IfModule>
	EOF
    fi

    # Reload Apache service
    SVC_RELOAD "${APACHE_SVC}"
