cmd=$(fc -nl -1)
#!/bin/bash
source $TOOLS/script/ENVS64.bash
source $TOOLS/script/use_python3.sh

fac=$(echo $FACILITY_ROOT | awk -F'/' '{print $NF}')

IOC_NAME="*"
ERR_OUT="0"
RET_IOC="0"

usage(){
	echo "Usage:" 1>&2
	echo "	invalidPVs-Drive.bash [ -e ] [ -i ] [ -a IOC_NAME ]" 1>&2
	echo "" 1>&2
	echo "Options:" 1>&2
	echo "	-h          Show this message and exit" 1>&2
	echo "	-e          Print PV name error making it invalid" 1>&2
	echo "	-i          Print IOC containing incorrect PV name" 1>&2
	echo "	-a IOC_NAME Search all PVs located in in this IOC" 1>&2
}
exit_abnormal(){
	usage
	exit 1
}

while getopts ":eia:" options; do
	case "${options}" in
		e) ERR_OUT="1";;
		i) RET_IOC="1";;
		a) IOC_NAME=${OPTARG};;
		:) echo "Error: -${OPTARG} requires an argument." 1>&2
			exit_abnormal;;
		*) exit_abnormal;;
	esac
done

echo $cmd
sed -r 's/(.*),.*/\1/' $IOC_DATA/$IOC_NAME/iocInfo/IOC.pvlist | sort | \
        (cd $TOOLS/script/ValidatePVs \
        && python invalidPVs.py \
        input_files/devices.json \
        input_files/${fac}_areas.json \
        $ERR_OUT \
        $RET_IOC)
exit 0
