#!/bin/sh
# This script is supposed to check lyrionmusiserver a little bit. At least each page and
# each link is tried.
#
# Inspired by the cacti version :)

set -euo pipefail

SCAN_LOG=/var/log/squeezeboxserver/scanner.log
SERV_LOG=/var/log/squeezeboxserver/server.log
SERV_PREF=/var/lib/squeezeboxserver/prefs/server.prefs

save_log_files() {
    echo "Copying /var/log/squeezeboxserver/*.log to artifacts"
    cp $SCAN_LOG $SERV_LOG ${AUTOPKGTEST_ARTIFACTS}
}

# To make sure that the autopkgtest/CI sites store the information
if [ -n "${AUTOPKGTEST_ARTIFACTS}" ] ; then
    trap save_log_files 0
fi

# We don't want to see the intial page, let's make sure we're passed that
# And let's make sure there's some media
# Squeezebox-* to avoid warnings about invalid cookie headers (until I confirm it should be fixed)
# HTTP.pm line 997 adding " // 0"
sudo tee $SERV_PREF > /dev/null <<EOF
mediadirs:
- /usr/share/squeezeboxserver/HTML/EN/html
- /usr/share/games/c-evo-dh/Sounds
playlistdir: /usr/share/games/c-evo-dh/Sounds
wizardDone: 1
Squeezebox-albumView: '1'
Squeezebox-expandPlayerControl: 'true'
Squeezebox-expanded-FAVORITES: '1'
Squeezebox-expanded-MY_MUSIC: '1'
Squeezebox-expanded-PLUGINS: '1'
Squeezebox-expanded-PLUGIN_MY_APPS_MODULE_NAME: '1'
Squeezebox-expanded-RADIO: '1'
EOF

sudo chown _lyrionmusicserver: $SERV_PREF

sudo systemctl start lyrionmusicserver

sleep 5
grep -q "Starting Lyrion Music Server" $SERV_LOG

# Loop over all the available links
# http://localhost:9000/stream.mp3  extracted from javascript string
# "html/softqueeze/index"           extracted from javascript string
# .mp3 loops and fills the disk
# status_header.html    HTTP request sent, awaiting response... Read error (Connection timed out) in headers
# anyurl                HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
# html/*images  looks like a race condition
# clixmlbrowser/ # ridiculous amount
# .#.            # ridiculous amount
# level=5 (default) too much
wget --reject-regex=".*(http://localhost:9000/stream\.mp3|html/softsqueeze/index|\.mp3$|/status_header.html|/anyurl\?|/html.*/images/|/clixmlbrowser/|\.2\.|\.3\.|\.4\.|\.5\.|\.6\.|\.7\.|\.8\.|\.9\.).*" --read-timeout=10 --tries=1 --recursive --level=2 --execute=robots=off http://127.0.0.1:9000/ || res=$?

# Finally check the log for unexpected items
FILTERED_LOG="$(grep -v \
     -e "^$" \
     -e "^\s*frame [0-9]*: " \
     -e " Starting Lyrion Music Server " \
     -e " Server done init: http:" \
     -e " Slim::Schema::throw_exception ([0-9]*) Backtrace:$" \
     -e " Slim::Schema::throw_exception ([0-9]*) Error: Unable to satisfy requested constraint '.*search', missing values for column(s): '.*search'$" \
     -e " Slim::Utils::Misc::msg ([0-9]*) Warning: \[" \
     -e " Slim::Web::Template::SkinManager::_fillTemplate ([0-9]*) Error: file error - : not found$" \
     -e " Warning: Schema updated or no media found in the database, initiating scan.$" \
     $SERV_LOG)" || true

if [ -n "${FILTERED_LOG}" ] ; then
    echo "Unexpected output in logs:"
    echo "${FILTERED_LOG}"
else
    echo "No unexpected output in /var/log/squuezeboxserver/"
fi

echo The wget command exited with status ${res:-0}
if [ -n "${FILTERED_LOG}" ] ; then
    return 1976
else
    return ${res:-0}
fi
