#!/bin/bash

ACTION=$1

if [ -e '/etc/hwinit' ]; then
 	ACTIVE_CNT=`ls /etc/hwinit | wc -l`
 	if [ $ACTIVE_CNT -gt 0 ]; then	
         	ACTIVE=(/etc/hwinit/*)
 	else
 		ACTIVE=()
 	fi
else
    ACTIVE=()
fi

if [ -e '/usr/share/kses/hwinit' ]; then
 	UNIT_CNT=`ls /usr/share/kses/hwinit | wc -l`
 	if [ $UNIT_CNT -gt 0 ]; then	
         	UNITS=(/usr/share/kses/hwinit/*)
 	else
 		UNIT=()
 	fi
else
    UNIT=()
fi

help()
{
  echo "Usage hwctl <cmd> [unit]"
  echo "where <cmd> is one of list|enable|disable" 
}

enabled()
{
  for file in "${ACTIVE[*]}"; do
    if [[ "${file}" == "/etc/hwinit/$1" ]] ; then
       echo -n " enabled"
       return
    fi     
  done
  echo -n " disabled"
}
list_units()
{
echo
for file in "${UNITS[@]}"; do
 unit=$(basename $file)
 echo -n $unit
 enabled $unit
 echo 
done
echo
}

enable_unit()
{
  echo "Enabling $1"
  ln -sv "/usr/share/kses/hwinit/$1" "/etc/hwinit/$1"
}

disable_unit()
{
  echo "Disabling $1"
  rm -v "/etc/hwinit/$1"
}

case "$ACTION" in
	list)
	  list_units
	  ;;
	enable)
	  enable_unit $2
	  ;;
	disable)
	  disable_unit $2
	  ;;
	  *)
	  help
	  ;;
esac


