#!/bin/sh

set -e

VERSION=`date +%Y%m%d%H%M%S%N`
AMPLEDIR=$AUTOPKGTEST_TMP/tmp/ample-$VERSION

# create tmp dirs
mkdir -p $AMPLEDIR/mp3

# create temporary conf file
> $AMPLEDIR/ample.conf
cat << EOF >> $AMPLEDIR/ample.conf
port = 1234
order = true
clients = 5
logfile = $AMPLEDIR/ample.log
mp3path = $AMPLEDIR/mp3
recursive = true
servername = Ample
EOF

# create mp3 fake file
> $AMPLEDIR/mp3/test.mp3
cat << EOF >> $AMPLEDIR/mp3/test.mp3
fake mp3 file just to run ample daemon
EOF

# execute the daemon
/usr/bin/ample -c 10 -f $AMPLEDIR/ample.conf -i $AMPLEDIR/ample.pid

# check if the daemon is running
ps ax | egrep '(ample.*ample.pid)' | grep -v grep

# check is the daemon is listening
ss -tlndp | egrep -e '(tcp.*LISTEN.*:1234.*ample)'

# clean up everything
kill -9 `cat $AMPLEDIR/ample.pid`
