#!/bin/sh
[ -n "${IPKG_INSTROOT}" ] && exit 0
mkdir -p /etc/tachyon /usr/share/tachyon-core 2>/dev/null || true
if [ -L /usr/bin/sing-box ]; then
    target="$(readlink /usr/bin/sing-box 2>/dev/null || true)"
    case "$target" in
        *tachyon-core*) rm -f /usr/bin/sing-box ;;
    esac
fi
if [ -L /etc/init.d/sing-box ]; then
    target="$(readlink /etc/init.d/sing-box 2>/dev/null || true)"
    case "$target" in
        *tachyon-core*) rm -f /etc/init.d/sing-box ;;
    esac
fi
# Always disable standalone rc.d autostart so tachyon (/etc/init.d/tachyon)
# exclusively controls the lifecycle of tachyon-core without boot races.
if [ -x /etc/init.d/tachyon-core ]; then
    /etc/init.d/tachyon-core disable >/dev/null 2>&1 || true
fi
rm -f /etc/rc.d/S*tachyon-core /etc/rc.d/K*tachyon-core 2>/dev/null || true
echo "tachyon-core" > /etc/tachyon/tachyon-core-variant 2>/dev/null || true
if [ -x /usr/bin/tachyon-core ]; then
    ver="$(/usr/bin/tachyon-core version 2>/dev/null | awk 'NR==1 {print $NF}')"
    [ -n "$ver" ] && echo "$ver" > /etc/tachyon/tachyon-core-version 2>/dev/null || true
fi
if [ -x /etc/init.d/tachyon ] && /etc/init.d/tachyon running >/dev/null 2>&1; then
    /etc/init.d/tachyon-core restart >/dev/null 2>&1 || /etc/init.d/tachyon restart >/dev/null 2>&1 || true
elif [ -x /etc/init.d/tachyon-core ] && /etc/init.d/tachyon-core running >/dev/null 2>&1; then
    /etc/init.d/tachyon-core restart >/dev/null 2>&1 || true
fi
exit 0
