```bash
#!/bin/sh
# Define variables
APP_NAME="ipchanger"
APP_DIR="/usr/lib/lua/luci"
CONTROLLER_DIR="$APP_DIR/controller/$APP_NAME"
MODEL_DIR="$APP_DIR/model/cbi/$APP_NAME"
CONFIG_FILE="/etc/config/$APP_NAME"
CURRENT_THEME=$(uci get luci.themes.Theme_argon 2>/dev/null)
# Create directories
mkdir -p "$CONTROLLER_DIR"
mkdir -p "$MODEL_DIR"
# Create configuration file
if [ ! -f "$CONFIG_FILE" ]; then
cat <<EOF > "$CONFIG_FILE"
config ipchanger 'settings'
option new_ip ''
EOF
echo "Configuration file created: $CONFIG_FILE"
else
# Check if the configuration file has the correct content
if ! grep -q "config ipchanger 'settings'" "$CONFIG_FILE"; then
cat <<EOF > "$CONFIG_FILE"
config ipchanger 'settings'
option new_ip ''
EOF
echo "Configuration file fixed: $CONFIG_FILE"
else
echo "Configuration file already exists and is correct: $CONFIG_FILE"
fi
fi
# Set configuration file permissions
chmod 644 "$CONFIG_FILE"
echo "Configuration file permissions set to 644"
# Create controller file
cat <<EOF > "$CONTROLLER_DIR/index.lua"
module("luci.controller.$APP_NAME.index", package.seeall)
function index()
entry({"admin", "system", "$APP_NAME"}, cbi("$APP_NAME/ipchanger"), _("IP Changer"), 60)
end
EOF
echo "Controller file created: $CONTROLLER_DIR/index.lua"
# Create CBI model file
cat <<EOF > "$MODEL_DIR/ipchanger.lua"
local uci = require "luci.model.uci".cursor()
m = Map("$APP_NAME", translate("IP Changer"), translate("Change the IP address of the LAN interface."))
s = m:section(NamedSection, "settings", "ipchanger", "")
s.addremove = false
s.anonymous = true
-- Retrieve current IP address
local current_ip = uci:get("network", "lan", "ipaddr") or "192.168.1.1"
-- Field 1: Display current IP address
o = s:option(DummyValue, "current_ip", translate("Current IP Address"))
o.default = current_ip
o.readonly = true
-- Field 2: Input for new IP address
o = s:option(Value, "new_ip", translate("New IP Address"))
o.datatype = "ip4addr"
-- Logic for processing the submit button
function o.write(self, section, value)
if value ~= current_ip then
-- Use sed to replace all instances of the old IP with the new IP in /etc/config/network
os.execute("sed -i 's/" .. current_ip .. "/" .. value .. "/g' /etc/config/network")
-- Calculate the new gateway address (replace the last byte with 1)
local new_gateway = value:gsub("%d+$", "1")
-- Update gateway address
os.execute("sed -i '/option gateway/s/192.168.%d+.%d+/" .. new_gateway .. "/' /etc/config/network")
-- Replace all instances of the old IP with the new IP in /etc/config/dhcp
os.execute("sed -i 's/" .. current_ip .. "/" .. value .. "/g' /etc/config/dhcp")
-- Update ipchanger configuration
uci:set("$APP_NAME", "settings", "new_ip", value)
uci:commit("$APP_NAME")
-- Restart network services
os.execute("/etc/init.d/network restart")
end
end
return m
EOF
echo "CBI model file created: $MODEL_DIR/ipchanger.lua"
# Clear LuCI cache
rm -rf /tmp/luci-*
echo "LuCI cache cleared"
# Check the current theme
if [ -n "$CURRENT_THEME" ]; then
echo "The current theme is argon."
echo "Note: The argon theme may not fully support CBI components."
echo "For compatibility, it is recommended to switch to the default bootstrap theme."
echo "Would you like to switch to the bootstrap theme now? (y/n)"
read -r response
if [ "$response" = "y" ]; then
uci set luci.themes.Theme_argon="bootstrap"
uci commit luci
echo "Theme switched to bootstrap. Please refresh the web interface."
else
echo "You can manually switch to the bootstrap theme later via the web interface."
fi
fi
# Output completion message
echo "LuCI application '$APP_NAME' successfully installed."
echo "Access it via the OpenWRT web interface: 'Admin > System > IP Changer'."
```
No Comments