#!/usr/bin/bash

# Created by Sensei on Feb 15, 2018
# Last modified on Feb 10, 2019


# RUN+="/usr/share/ncid/sys/udev-action CometUSB|HoltekUSB|ModemUSB"

[ $# -ne 1 ] && \
{
    echo "Usage: $0 CometUSB|HoltekUSB|ModemUSB" >&2
    exit 1
}

devmodel="$1"

sysrunchk=`systemctl is-system-running 2> /dev/null`
if [ ! $sysrunchk ]; then
    exit 1
fi

if [ $devmodel = ModemUSB ]; then
    cidinput=`sed '/^ *set  *cidinput *= *[0123]/!d;s/ *set  *cidinput *= *\([0123]\).*/\1/' /etc/ncid/ncidd.conf`
    if [[ $sysrunchk = initializing || $sysrunchk = starting || $cidinput != 2 ]]; then
        exit 1
    else
        systemctl restart ncidd.service
        exit 0
    fi
elif [[ $devmodel = CometUSB || $devmodel = HoltekUSB ]]; then
    if [[ $sysrunchk = initializing || $sysrunchk = starting ]]; then
        exit 1
    else
        systemctl start xdmf2ncid.service
        exit 0
    fi
else
    exit 1
fi

