#!/bin/bash

# Usage: bioctl status|enable|disable

CONFIG_DIR=/etc/biometric-auth
CONFIG_FILE=$CONFIG_DIR/ukui-biometric.conf

function test_privilege()
{
	if [ `whoami` != 'root' ]; then
		echo $(gettext "Permission denied, please run by root")
		exit 1;
	fi
}


if [ ! -d $CONFIG_DIR ]; then
	mkdir -p $CONFIG_DIR
fi

if [ ! -f $CONFIG_FILE ]; then
	touch $CONFIG_FILE
fi

contain_key=`grep -c "^EnableAuth=" $CONFIG_FILE`

if [ "$1" = "enable" ]; then
	test_privilege
	pam-auth-update --package pam-biometric
	if [ "$contain_key" = "1" ]; then
		sed -i 's/^EnableAuth=[a-zA-Z0-9]*/EnableAuth=true/g' $CONFIG_FILE
	else
		echo "EnableAuth=true" >> $CONFIG_FILE
	fi
elif [ "$1" = "disable" ]; then
	test_privilege
	if [ "$contain_key" = "1" ]; then
		sed -i 's/^EnableAuth=[a-zA-Z0-9]*/EnableAuth=false/g' $CONFIG_FILE
	else
		echo "EnableAuth=false" >> $CONFIG_FILE
	fi
elif [ "$1" = "status" ]; then
	cur_status=`sed '/^EnableAuth/!d;s/.*=//' $CONFIG_FILE`
    if [ "$cur_status" = "true" ]; then
        echo "enable"
	else
        echo "disable"
	fi
else
	echo "Usage: bioctl status|enable|disable"
fi

exit 0
