#!/bin/sh -e

echo "Creating /usr/lib/gutenbach/config/gutenbach-filter-config.pl..." >&2
if [ -e "/usr/lib/gutenbach/config/gutenbach-filter-config.pl" ]; then
    rm /usr/lib/gutenbach/config/gutenbach-filter-config.pl
fi

failed=
if [ -e /usr/lib/gutenbach/config/printername ]; then
    printername=$(cat /usr/lib/gutenbach/config/printername)
else
    failed="yes"
fi
if [ -e /usr/lib/gutenbach/config/hostname ]; then
    hostname=$(cat /usr/lib/gutenbach/config/hostname)
else
    failed="yes"
fi
if [ -e /usr/lib/gutenbach/config/zephyrclass ]; then
    zephyrclass=$(cat /usr/lib/gutenbach/config/zephyrclass)
else
    failed="yes"
fi
if [ -e /usr/lib/gutenbach/remctl/config/mixer ]; then
    mixer=$(cat /usr/lib/gutenbach/remctl/config/mixer)
else
    mixer="PCM"
fi
if [ -e /usr/lib/gutenbach/remctl/config/channel ]; then
    channel=$(cat /usr/lib/gutenbach/remctl/config/channel)
else
    channel="Front Left"
fi



config_file="/usr/lib/gutenbach/config/gutenbach-filter-config.pl"

if [ -z "$failed" ]; then
    echo "\$zephyr_class = \""$zephyrclass"\";" >> "$config_file"
    echo "\$host = \""$hostname"\";" >> "$config_file"
    echo "\$queue = \""$printername"\";" >> "$config_file"
    echo "\$mixer = \""$mixer"\";" >> "$config_file"
    echo "\$channel = \""$channel"\";" >> "$config_file"
    echo "1;" >> "$config_file"
else
    echo "Error: One of /usr/lib/gutenbach/config/{printername,hostname,zephyrclass} does not exist!" >&2
    exit 1
fi
