Web app, uhttpd shell CGI backend, installer, build script and dev server. README merged with the repository's project description. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
207 lines
6.7 KiB
Bash
207 lines
6.7 KiB
Bash
#!/bin/sh
|
|
# Cabin Chat @@VERSION@@ - self-contained installer for OpenWrt / GL.iNet routers.
|
|
# Uses only what the firmware already ships (uhttpd, busybox, uci).
|
|
#
|
|
# sh install-cabinchat.sh install, or upgrade an existing install
|
|
# (keeps messages, photos and the port)
|
|
# sh install-cabinchat.sh -p 8090 install/upgrade on a specific port
|
|
# sh install-cabinchat.sh wipe delete all messages and photos
|
|
# sh install-cabinchat.sh uninstall remove Cabin Chat completely
|
|
#
|
|
# Generated by build.sh - edit www/ and installer.sh.in instead of this file.
|
|
|
|
VERSION="@@VERSION@@"
|
|
APP=/www-chat
|
|
WEB=$APP/htdocs
|
|
DATA=$APP/data
|
|
RUN=/tmp/cabinchat
|
|
CLOCK=/etc/cabinchat.clock
|
|
DNSNAME=chat.lan
|
|
DEFAULT_PORT=8088
|
|
STAGE=/tmp/cabinchat-stage.$$
|
|
|
|
info() { echo ">> $*"; }
|
|
warn() { echo "WARNING: $*" >&2; }
|
|
die() { echo "ERROR: $*" >&2; rm -rf "$STAGE"; exit 1; }
|
|
usage() { sed -n '4,9p' "$0" | sed 's/^# \{0,1\}//'; }
|
|
|
|
listening() { netstat -tln 2>/dev/null | grep -qE "[:.]$1[[:space:]]"; }
|
|
|
|
configured_port() {
|
|
uci -q get uhttpd.chat.listen_http | tr ' ' '\n' | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -n 1
|
|
}
|
|
|
|
remove_config() {
|
|
if uci -q get uhttpd.chat >/dev/null; then
|
|
uci delete uhttpd.chat
|
|
uci commit uhttpd
|
|
fi
|
|
for a in $(uci -q get dhcp.@dnsmasq[0].address); do
|
|
case $a in "/$DNSNAME/"*) uci del_list dhcp.@dnsmasq[0].address="$a" ;; esac
|
|
done
|
|
uci commit dhcp
|
|
[ -f /etc/sysupgrade.conf ] && sed -i "\#^$APP/\$#d" /etc/sysupgrade.conf
|
|
}
|
|
|
|
extract_payload() {
|
|
rm -rf "$STAGE"
|
|
mkdir -p "$STAGE" || die "cannot create $STAGE"
|
|
@@PAYLOAD@@
|
|
[ -f "$STAGE/index.html" ] && [ -f "$STAGE/cgi-bin/chat" ] && [ -f "$STAGE/VERSION" ] \
|
|
|| die "installer payload is incomplete"
|
|
}
|
|
|
|
# version 2 lines (6 fields) -> version 3 (8 fields: + recipient "*" and no quote)
|
|
migrate_messages() {
|
|
[ -s "$DATA/messages" ] || return 0
|
|
awk -F '\t' 'BEGIN { OFS = "\t" }
|
|
NF == 6 { print $1, $2, $3, $4, "*", 0, $5, $6; next }
|
|
NF == 8 { print }' "$DATA/messages" > "$DATA/messages.new" &&
|
|
mv "$DATA/messages.new" "$DATA/messages"
|
|
}
|
|
|
|
# ------------------------------------------------------------------ install
|
|
do_install() {
|
|
[ -x /usr/sbin/uhttpd ] || die "uhttpd is not installed.
|
|
With internet access run: opkg update && opkg install uhttpd
|
|
then run this installer again."
|
|
|
|
LANIP=$(uci -q get network.lan.ipaddr | cut -d/ -f1)
|
|
[ -n "$LANIP" ] || die "could not read the LAN IP (network.lan.ipaddr)"
|
|
|
|
OLDVER=
|
|
if [ -f "$APP/VERSION" ]; then
|
|
OLDVER=$(cat "$APP/VERSION")
|
|
elif [ -e "$APP/index.html" ] || uci -q get uhttpd.chat >/dev/null; then
|
|
OLDVER="1.x"
|
|
fi
|
|
OLDPORT=$(configured_port)
|
|
[ -n "$PORT" ] || PORT=${OLDPORT:-$DEFAULT_PORT}
|
|
case $PORT in ''|*[!0-9]*) die "invalid port: $PORT" ;; esac
|
|
if [ "$PORT" != "$OLDPORT" ] && listening "$PORT"; then
|
|
die "port $PORT is already in use - choose another one with -p <port>"
|
|
fi
|
|
|
|
if [ -n "$OLDVER" ]; then
|
|
info "upgrading Cabin Chat $OLDVER -> $VERSION"
|
|
else
|
|
info "installing Cabin Chat $VERSION"
|
|
fi
|
|
|
|
# unpack first: if anything is wrong with this file, nothing has been touched yet
|
|
extract_payload
|
|
|
|
if [ -n "$OLDVER" ]; then
|
|
info "removing the previous version (messages and photos are kept)"
|
|
fi
|
|
remove_config
|
|
mkdir -p "$APP" || die "cannot create $APP"
|
|
mkdir -p "$DATA"
|
|
# keep the photos aside (version 2 kept them in data/img, version 3 in htdocs/img)
|
|
KEEPIMG=$APP/.img-keep
|
|
mkdir -p "$KEEPIMG"
|
|
for d in "$WEB/img" "$DATA/img"; do
|
|
[ -d "$d" ] && mv "$d"/* "$KEEPIMG"/ 2>/dev/null
|
|
rm -rf "$d"
|
|
done
|
|
for f in "$APP"/* "$APP"/.[!.]*; do
|
|
[ -e "$f" ] || continue
|
|
case $f in "$DATA"|"$KEEPIMG") continue ;; esac
|
|
rm -rf "$f"
|
|
done
|
|
rm -rf /tmp/chat.log /tmp/chat.lock /tmp/cabinchat.lock # leftovers from versions 1 and 2
|
|
migrate_messages
|
|
|
|
info "installing files to $WEB"
|
|
mkdir -p "$WEB"
|
|
cp -R "$STAGE"/* "$WEB"/ || die "copying files failed"
|
|
rm -rf "$STAGE"
|
|
chmod 755 "$WEB"/cgi-bin/*
|
|
mkdir -p "$WEB/img"
|
|
mv "$KEEPIMG"/* "$WEB/img"/ 2>/dev/null
|
|
rm -rf "$KEEPIMG"
|
|
[ -f "$CLOCK" ] || date +%s > "$CLOCK"
|
|
|
|
info "configuring web server on $LANIP:$PORT"
|
|
uci set uhttpd.chat=uhttpd
|
|
uci add_list uhttpd.chat.listen_http="$LANIP:$PORT"
|
|
uci set uhttpd.chat.home="$WEB"
|
|
uci set uhttpd.chat.cgi_prefix='/cgi-bin'
|
|
uci set uhttpd.chat.index_page='index.html'
|
|
uci set uhttpd.chat.no_dirlists='1'
|
|
# long polling keeps one request per open chat waiting for up to 25 s
|
|
uci set uhttpd.chat.script_timeout='60'
|
|
uci set uhttpd.chat.network_timeout='60'
|
|
uci set uhttpd.chat.max_requests='100'
|
|
uci set uhttpd.chat.max_connections='100'
|
|
uci commit uhttpd
|
|
/etc/init.d/uhttpd enable
|
|
/etc/init.d/uhttpd restart
|
|
|
|
info "adding DNS name $DNSNAME"
|
|
uci add_list dhcp.@dnsmasq[0].address="/$DNSNAME/$LANIP"
|
|
uci commit dhcp
|
|
/etc/init.d/dnsmasq restart >/dev/null 2>&1
|
|
|
|
touch /etc/sysupgrade.conf
|
|
grep -qx "$APP/" /etc/sysupgrade.conf || echo "$APP/" >> /etc/sysupgrade.conf
|
|
|
|
sleep 1
|
|
listening "$PORT" || die "uhttpd is not listening on port $PORT - check: logread | grep uhttpd"
|
|
if command -v wget >/dev/null; then
|
|
case $(wget -q -O - "http://$LANIP:$PORT/cgi-bin/chat?k=00000000000000000000000000000000&since=0" 2>/dev/null | head -n 1) in
|
|
@*) info "self-test passed" ;;
|
|
*) warn "the chat backend did not answer as expected - check: logread | grep uhttpd" ;;
|
|
esac
|
|
fi
|
|
|
|
n=$(grep -c . "$DATA/messages" 2>/dev/null); n=${n:-0}
|
|
echo
|
|
info "Cabin Chat $VERSION is running ($n messages from the last 24 h kept)"
|
|
echo " http://$LANIP:$PORT"
|
|
echo " http://$DNSNAME:$PORT"
|
|
}
|
|
|
|
# ------------------------------------------------------------------ uninstall
|
|
do_uninstall() {
|
|
info "removing Cabin Chat"
|
|
remove_config
|
|
rm -rf "$APP" "$CLOCK" "$RUN" /tmp/chat.log /tmp/chat.lock /tmp/cabinchat.lock
|
|
/etc/init.d/uhttpd restart >/dev/null 2>&1
|
|
/etc/init.d/dnsmasq restart >/dev/null 2>&1
|
|
info "done"
|
|
}
|
|
|
|
do_wipe() {
|
|
[ -d "$APP" ] || die "Cabin Chat is not installed"
|
|
rm -rf "$DATA" "$WEB/img"
|
|
mkdir -p "$DATA" "$WEB/img"
|
|
# wake up waiting browsers so they notice the empty chat
|
|
[ -d "$RUN" ] && echo "wipe.$$" > "$RUN/ev"
|
|
info "all messages and photos deleted"
|
|
}
|
|
|
|
# ------------------------------------------------------------------ main
|
|
CMD=install
|
|
PORT=
|
|
while [ $# -gt 0 ]; do
|
|
case $1 in
|
|
-p|--port) [ -n "$2" ] || die "-p needs a port number"; PORT=$2; shift 2 ;;
|
|
[0-9]*) PORT=$1; shift ;;
|
|
install|upgrade) CMD=install; shift ;;
|
|
uninstall|remove) CMD=uninstall; shift ;;
|
|
wipe) CMD=wipe; shift ;;
|
|
-h|--help|help) usage; exit 0 ;;
|
|
*) usage; die "unknown argument: $1" ;;
|
|
esac
|
|
done
|
|
|
|
[ "$(id -u)" = "0" ] || die "please run as root"
|
|
command -v uci >/dev/null || die "uci not found - is this OpenWrt?"
|
|
|
|
case $CMD in
|
|
install) do_install ;;
|
|
uninstall) do_uninstall ;;
|
|
wipe) do_wipe ;;
|
|
esac
|