<?xml version='1.0' standalone='yes'?>

<!DOCTYPE PLUGIN [
<!ENTITY name      "dwttm">
<!ENTITY author    "desertwitch">
<!ENTITY version   "2025.04.13">
<!ENTITY launch    "Settings/dwTTM">
<!ENTITY gitURL    "https://raw.githubusercontent.com/&author;/TTM-unRAID/main">
<!ENTITY pluginURL "&gitURL;/plugin/&name;.plg">
<!ENTITY pkgURL    "&gitURL;/packages">
<!ENTITY plgPATH   "/boot/config/plugins/&name;">
<!ENTITY plgNAME   "dwttm-2025.04.13-x86_64-1">
<!ENTITY plgMD5    "5cbfb3f5c93b5349ba8b718e0cebc7af">
<!ENTITY emhttp    "/usr/local/emhttp/plugins/&name;">
]>

<PLUGIN name="&name;" author="&author;" version="&version;" launch="&launch;" pluginURL="&pluginURL;" support="https://forums.unraid.net/topic/183427-plugin-tmux-terminal-manager-ttm/" min="6.11.0" icon="tty">

<CHANGES>
## Tmux Terminal Manager (TTM)
###2025.04.13
- fix: major refactor of the ttmd backend with several improvements
###2025.03.26a
- new: added the reptyr command-line tool to the package (see below)
- reptyr allows moving already running (non-tmux) processes into tmux sessions
###2025.02.26
- fix: future-proofing changes to base plugin structure
###2025.01.24
- fix: addressed theoretical congestion with multiple sessions
###2025.01.22
- fix: resolved issue with transition between dashboard and settings
###2025.01.14
- new: distinguish between TTM(-created) and non-TTM sessions in GUI
- new: added a session grid with previews and management options to the popup
###2025.01.11
- new: added a link to a tmux cheat sheet (thanks Barry Staes)
- fix: changed default array stop behavior for new installations
- fix: changed escaping for some backend commands
###2025.01.10
- new: added setting to kill array-accessing sessions when array stops
- release note: the default is disabled (go to TTM settings to enable this)
###2025.01.08
- fix: improve popup handling (allow multiple windows)
###2025.01.07
- fix: service startup failure after reboot with custom tmux
###2024.12.30
- new: support for unicode (thanks hurui200320)
- fix: improved handling of processes and encountered errors
###2024.12.26
- release note: welcome to the first version of the plugin
- release note: please report any issues in the support thread on the forums
</CHANGES>

<!--
stop already running instances before doing anything
-->
<FILE Run="/bin/bash">
<INLINE>
if [ -x /etc/rc.d/rc.ttmd ]; then
    echo "Making sure all existing TTM services are stopped (before install/upgrade)..."
    if ! /etc/rc.d/rc.ttmd stop >/dev/null 2>&amp;1; then
        echo "WARNING:"
        echo "WARNING: The TTM installation script was not able to stop all TTM services."
        echo "WARNING: IN CASE OF PROBLEMS, please REBOOT YOUR SYSTEM to complete any upgrades."
        echo "WARNING:"
    fi
else
    killall ttmd >/dev/null 2>&amp;1
fi
killall dwttm-poller >/dev/null 2>&amp;1
exit 0
</INLINE>
</FILE>

<!--
install or upgrade dependency files
-->
<FILE Name="&plgPATH;/tmux-3.5a-x86_64-1_slack15.1.txz" Min="6.13.0">
<URL>&pkgURL;/tmux-3.5a-x86_64-1_slack15.1.txz</URL>
<MD5>a697e9a8aa8d4704ea8858914f2e2d7d</MD5>
</FILE>

<FILE Run="/bin/bash" Min="6.13.0">
<INLINE>
if [ -f /boot/config/plugins/unassigned.devices.preclear.plg ]; then
    echo ""
    echo "----------------------------------------------------------------"
    echo " Tmux is already installed from elsewhere, not reinstalling."
    echo " It is part of Unassigned Devices Preclear, so we'll use that."
    echo "----------------------------------------------------------------"
    echo ""
    touch &plgPATH;/custom
elif compgen -G "/boot/extra/tmux*" >/dev/null 2>&amp;1; then
    echo ""
    echo "----------------------------------------------------------------"
    echo " Tmux is already installed elsewhere, not reinstalling."
    echo " It was found in the /boot/extra/ folder, so we'll use that."
    echo "----------------------------------------------------------------"
    echo ""
    touch &plgPATH;/custom
else
    upgradepkg --install-new &plgPATH;/tmux-3.5a-x86_64-1_slack15.1.txz
    rm -f &plgPATH;/custom
fi
exit 0
</INLINE>
</FILE>

<FILE Name="&plgPATH;/tmux-3.5a-x86_64-1_slack15.0.txz" Min="6.11.0" Max="6.12.99">
<URL>&pkgURL;/tmux-3.5a-x86_64-1_slack15.0.txz</URL>
<MD5>337e6b7d531cb9ec11884b01dd58af8a</MD5>
</FILE>

<FILE Run="/bin/bash" Min="6.11.0" Max="6.12.99">
<INLINE>
if [ -f /boot/config/plugins/unassigned.devices.preclear.plg ]; then
    echo ""
    echo "----------------------------------------------------------------"
    echo " Tmux is already installed from elsewhere, not reinstalling."
    echo " It is part of Unassigned Devices Preclear, so we'll use that."
    echo "----------------------------------------------------------------"
    echo ""
    touch &plgPATH;/custom
elif compgen -G "/boot/extra/tmux*" >/dev/null 2>&amp;1; then
    echo ""
    echo "----------------------------------------------------------------"
    echo " Tmux is already installed from elsewhere, not reinstalling."
    echo " It was found in the /boot/extra/ folder, so we'll use that."
    echo "----------------------------------------------------------------"
    echo ""
    touch &plgPATH;/custom
else
    upgradepkg --install-new &plgPATH;/tmux-3.5a-x86_64-1_slack15.0.txz
    rm -f &plgPATH;/custom
fi
exit 0
</INLINE>
</FILE>

<FILE Name="&plgPATH;/reptyr-0.10.0-x86_64-1_slack15.0.txz" Min="6.11.0" Run="upgradepkg --install-new">
<URL>&pkgURL;/reptyr-0.10.0-x86_64-1_slack15.0.txz</URL>
<MD5>9a58bc6f78e7d46bca8ad4c1fd1ad94c</MD5>
</FILE>

<!--
install or upgrade the plugin itself
-->
<FILE Name="&plgPATH;/&plgNAME;.txz" Min="6.11.0" Run="upgradepkg --install-new">
<URL>&gitURL;/archive/&plgNAME;.txz</URL>
<MD5>&plgMD5;</MD5>
</FILE>

<!--
run the post-installation scripts
-->
<FILE Run="/bin/bash">
<INLINE>
CONFIG=&plgPATH;/&name;.cfg

echo "Reading configuration..."
if [ -e "$CONFIG" ]; then
    source "$CONFIG"
fi

if [ "$SERVICE" == "enable" ]; then
    if type tmux >/dev/null 2>&amp;1 &amp;&amp; tmux -V >/dev/null 2>&amp;1; then
        echo "Starting TTM service..."
        /etc/rc.d/rc.ttmd start 2>&amp;1 | logger -t "rc.ttmd"
        if [ "${PIPESTATUS[0]}" -eq 0 ]; then
            echo "TTM service has been started with success."
        else
            echo "WARNING: TTM service failed to start, check SYSLOG for more information."
        fi
    else
        if [ -f &plgPATH;/custom ]; then
            echo "Delaying TTM service start... (waiting on Tmux from another plugin/package)"
            /etc/rc.d/rc.ttmd delaystart >/dev/null 2>&amp;1 &amp;
        else
            echo "Starting TTM service..."
            echo "Error: Unable to start 'ttmd' - Tmux not found or not functional."
            echo "Error: Please refer to the 'TTM Settings' for more information on this issue."
        fi
    fi
else
    /etc/rc.d/rc.ttmd configure 2>&amp;1 | logger -t "rc.ttmd"
    echo ""
    echo "****************************************************************"
    echo " The service is disabled, please go to the TTM Settings"
    echo " page in order to configure and enable the TTM Service."
    echo "****************************************************************"
fi

echo ""
echo "----------------------------------------------------------------"
echo " Tmux Terminal Manager (TTM) has been installed."
echo " Version: &version; / Plugin Maintainer: &author;"
echo "----------------------------------------------------------------"
echo ""
</INLINE>
</FILE>

<!--
definition for uninstallation of the plugin
-->
<FILE Run="/bin/bash" Method="remove">
<INLINE>
if [ -x /etc/rc.d/rc.ttmd ]; then
    echo "Making sure all existing TTM services are stopped (before uninstall)..."
    if ! /etc/rc.d/rc.ttmd stop >/dev/null 2>&amp;1; then
        echo "WARNING:"
        echo "WARNING: The TTM uninstallation script was not able to stop all TTM services."
        echo "WARNING: IN CASE OF PROBLEMS, please REBOOT YOUR SYSTEM to remove any remaining packages."
        echo "WARNING:"
    fi
else
    killall ttmd >/dev/null 2>&amp;1
fi
killall dwttm-poller >/dev/null 2>&amp;1

if [ -f /boot/config/plugins/unassigned.devices.preclear.plg ]; then
    echo ""
    echo "----------------------------------------------------------------"
    echo " Tmux was installed from elsewhere, not removing it here."
    echo " It is part of Unassigned Devices Preclear, so we'll keep that."
    echo "----------------------------------------------------------------"
    echo ""
    rm -f &plgPATH;/tmux*.txz
elif compgen -G "/boot/extra/tmux*" >/dev/null 2>&amp;1; then
    echo ""
    echo "----------------------------------------------------------------"
    echo " Tmux was installed from elsewhere, not removing it here."
    echo " It was found in the /boot/extra/ folder, so we'll keep that."
    echo "----------------------------------------------------------------"
    echo ""
    rm -f &plgPATH;/tmux*.txz
fi
removepkg &plgPATH;/*.txz

rm -rf &plgPATH;
rm -rf &emhttp;
rm -f /var/log/ttmd.log
rm -f /etc/cron.daily/dwttm-poller >/dev/null 2>&amp;1

echo ""
echo "----------------------------------------------------------------"
echo " Tmux Terminal Manager (TTM) has been removed."
echo " Version: &version; / Plugin Maintainer: &author;"
echo "----------------------------------------------------------------"
echo ""
</INLINE>
</FILE>

</PLUGIN>