#!/bin/bash
custom_log="/tmp/ophub_start_service.log"
log_message() {
echo "[$(date +"%Y.%m.%d.%H:%M:%S")] $1" >>"${custom_log}"
}
log_message "Start the custom service..."
dmesg -n 1 >/dev/null 2>&1 || true
log_message "Kernel console logging level set to 1 (Panic only)."
ophub_release_file="/etc/ophub-release"
FDT_FILE=""
[[ -f "${ophub_release_file}" ]] && { FDT_FILE="$(grep -oE 'meson.*dtb' "${ophub_release_file}" || true)"; }
[[ -z "${FDT_FILE}" && -f "/boot/uEnv.txt" ]] && { FDT_FILE="$(grep -E '^FDT=.*\.dtb$' /boot/uEnv.txt | sed -E 's#.*/##' || true)"; }
[[ -z "${FDT_FILE}" && -f "/boot/extlinux/extlinux.conf" ]] && { FDT_FILE="$(grep -E '/dtb/.*\.dtb$' /boot/extlinux/extlinux.conf | sed -E 's#.*/##' || true)"; }
[[ -z "${FDT_FILE}" && -f "/boot/armbianEnv.txt" ]] && { FDT_FILE="$(grep -E '^fdtfile=.*\.dtb$' /boot/armbianEnv.txt | sed -E 's#.*/##' || true)"; }
log_message "Detected FDT file: ${FDT_FILE:-'not found'}"
if [[ "${FDT_FILE}" == "meson-sm1-skyworth-lb2004-a4091.dtb" ]]; then
modprobe btmtksdio >/dev/null 2>&1 &
log_message "Attempted to load btmtksdio module for Tencent-Aurora-3Pro."
fi
if [[ "${FDT_FILE}" == "rk3568-swan1-w28.dtb" ]]; then
(
gpioset 0 21=1 >/dev/null 2>&1
gpioset 3 20=1 >/dev/null 2>&1
gpioset 4 21=1 >/dev/null 2>&1
gpioset 4 22=1 >/dev/null 2>&1
) &
log_message "USB power control GPIOs set for Swan1-w28."
fi
if [[ "${FDT_FILE}" =~ ^(rk3588-smart-am60\.dtb|rk3588s-orangepi-5b\.dtb)$ ]]; then
(
rfkill block all
chmod a+x /lib/firmware/ap6276p/brcm_patchram_plus1 >/dev/null 2>&1
sleep 2
rfkill unblock all
/lib/firmware/ap6276p/brcm_patchram_plus1 --enable_hci --no2bytes --use_baudrate_for_download --tosleep 200000 --baudrate 1500000 --patchram /lib/firmware/ap6275p/BCM4362A2.hcd /dev/ttyS9 &
) &
log_message "Bluetooth firmware download process started for Smart-am60/orangepi-5b."
fi
if [[ "${FDT_FILE}" =~ ^(rk3568-nsy-g16-plus\.dtb|rk3568-nsy-g68-plus\.dtb|rk3568-bdy-g18-pro\.dtb)$ ]]; then
(
sleep 10
set_mtu() {
[[ -d "/sys/class/net/${1}" ]] && ip link set "${1}" mtu 1500 >/dev/null 2>&1
}
set_mtu eth0
set_mtu br0
if [[ -d "/sys/class/net/eth0" ]] && command -v ethtool >/dev/null 2>&1; then
ethtool -K eth0 tso off gso off gro off tx off rx off >/dev/null 2>&1
fi
) &
log_message "Network optimizations for ${FDT_FILE} applied."
fi
mkdir -p -m0755 /var/run/sshd >/dev/null 2>&1 || true
if [[ -f "/etc/init.d/ssh" ]]; then
(sleep 5 && /etc/init.d/ssh restart >/dev/null 2>&1) &
log_message "SSH service restart attempted."
fi
if [[ -x "/usr/sbin/balethirq.pl" ]]; then
(perl /usr/sbin/balethirq.pl >/dev/null 2>&1) &
log_message "Network optimization service (balethirq.pl) execution attempted."
fi
openvfd_enable="no"
openvfd_boxid="15"
openvfd_restart="no"
if [[ "${openvfd_boxid}" != "0" && "${FDT_FILE}" =~ ^meson- ]]; then
(
[[ "${openvfd_enable}" == "yes" ]] && armbian-openvfd "${openvfd_boxid}" >/dev/null 2>&1
[[ "${openvfd_restart}" == "yes" ]] && {
armbian-openvfd "0" >/dev/null 2>&1
sleep 3
armbian-openvfd "${openvfd_boxid}" >/dev/null 2>&1
}
log_message "OpenVFD service execution attempted."
) &
fi
if [[ -x "/usr/bin/rgb-vplus" ]]; then
rgb-vplus --RedName=RED --GreenName=GREEN --BlueName=BLUE >/dev/null 2>&1 &
log_message "Vplus RGB LED service started in background."
fi
if [[ -x "/usr/bin/pwm-fan.pl" ]]; then
perl /usr/bin/pwm-fan.pl >/dev/null 2>&1 &
log_message "Fan control service (pwm-fan.pl) started in background."
fi
if [[ -x "/usr/bin/oes_sata_leds.sh" ]]; then
/usr/bin/oes_sata_leds.sh >/var/log/oes-sata-leds.log 2>&1 &
log_message "SATA status check service (oes_sata_leds.sh) started in background."
fi
if [[ -n "$(dpkg -l | awk '{print $2}' | grep -w "^pve-manager$" || true)" ]]; then
(systemctl restart pveproxy >/dev/null 2>&1) &
log_message "PVE proxy service restart attempted."
fi
todo_rootfs_resize="/root/.no_rootfs_resize"
[[ -f "${todo_rootfs_resize}" && "$(cat ${todo_rootfs_resize} 2>/dev/null | xargs)" == "yes" ]] && {
armbian-tf >/dev/null 2>&1 &
log_message "Root partition resized successfully."
}
log_message "All custom services have been processed."