#!/bin/sh # Define constants BASE_URL="https://raw.githubusercontent.com/xchwarze/frieren-release/master/packages/openwrt" PACKAGE_NAME="frieren" PACKAGE_DIST="frieren_1.2-r1_all.ipk" # Get force install option from command line (if provided) FORCE_INSTALL=$1 # Logger function log() { echo "$(date '+%Y-%m-%d %H:%M:%S') [$2] $1" } # Error handler function handle_error() { log "Error: $2 (Exit Code: $1)" "ERROR" if [ "$1" -eq 1 ] && [ -z "$FORCE_INSTALL" ]; then log "Tip: Run the script with '-f' to force the installation if you encounter file clashes." "INFO" fi exit "$1" } # Function to check OpenWRT version and return package URL # Frieren is developed and tested for OpenWrt 24.x get_package_url() { local version="$1" local package_url="" if [ "$version" -ge 20 ]; then package_url="${BASE_URL}/latest/${PACKAGE_DIST}" fi echo "$package_url" } # Function to uninstall old package uninstall_old_package() { if opkg list-installed | grep -q "$PACKAGE_NAME"; then log "Removing old package $PACKAGE_NAME..." "INFO" opkg remove "$PACKAGE_NAME" || handle_error 1 "Failed to remove old package $PACKAGE_NAME" fi } # Main installation function install_package() { local version="$(awk -F"'" '/DISTRIB_RELEASE/{print $2}' /etc/openwrt_release | cut -d'.' -f1)" local package_url="$(get_package_url "$version")" if [ -z "$package_url" ]; then handle_error 1 "OpenWrt version $version is not supported. Frieren requires OpenWrt 20 or later (developed for OpenWrt 24.x)" fi log "Updating package lists..." "INFO" opkg update || handle_error 1 "Failed to update package lists" log "Downloading and installing package for OpenWRT $version..." "INFO" wget -qO /tmp/package.ipk "$package_url" && { if [ "$FORCE_INSTALL" = "-f" ]; then opkg install --force-overwrite /tmp/package.ipk || handle_error 1 "Package installation failed, even with force-overwrite" else opkg install /tmp/package.ipk || handle_error 1 "Package installation failed" fi } log "Package installation completed successfully" "SUCCESS" } # Display panel URL display_access_url() { local ip_address="$(ip -4 addr show br-lan | awk '/inet/ {print $2}' | cut -d'/' -f1)" if [ -z "$ip_address" ]; then log "Could not detect br-lan IP address. The OpenWrt version may not be supported." "WARNING" log "Verify that your device has a valid network configuration and try accessing the panel manually on port 5000." "INFO" else log "To access the Frieren web interface, open a web browser and navigate to: http://$ip_address:5000/" "INFO" fi } # Restart necessary services restart_services() { log "Restarting PHP-FPM and NGINX..." "INFO" /etc/init.d/nginx restart /etc/init.d/php8-fpm restart } # Ensure the script is running on OpenWRT if [ -f "/etc/openwrt_release" ]; then log "OpenWrt system detected, proceeding with installation..." "INFO" log "Note: Frieren is developed and tested for OpenWrt 24.x" "INFO" log "--------------------------------------------------------" "INFO" uninstall_old_package install_package restart_services display_access_url else log "This script is only supported on OpenWRT systems." "ERROR" exit 1 fi