#!/bin/sh

sudo systemctl start mosquitto.service

topic='test/topic'


function prefix() {
    while read line
    do echo "$@" "$line"
    done
}


(
    echo "[mosquitto_sub] listening (qos=0)"
    mosquitto_sub -v -t $topic  -q 0 | prefix "(qos=0)"
) & listener_pid=$!

(
    echo "[mosquitto_sub] listening (qos=1)"
    mosquitto_sub -v -t $topic  -q 1 | prefix "(qos=1)"
) & listener_pid="$listener_pid $!"

(
    echo "[mosquitto_sub] listening (qos=2)"
    mosquitto_sub -v -t $topic  -q 2 | prefix "(qos=2)"
) & listener_pid="$listener_pid $!"


(
    echo "[mosquitto_pub] publishing (qos=0)"
    mosquitto_pub -t $topic -m "message (qos=0)" -q 0

    echo "[mosquitto_pub] publishing (qos=1)"
    mosquitto_pub -t $topic -m "message (qos=1)" -q 1

    echo "[mosquitto_pub] publishing (qos=2)"
    mosquitto_pub -t $topic -m "message (qos=2)" -q 2
)

sleep 1s
kill $listener_pid


sudo systemctl stop mosquitto.service
