#! /bin/sh

list="$QUERY_STRING"

device_type=0

while [ "$list" != "" ]
do
	item=${list%%&*}
	list=${list#*&}

	if [ "$item" = "$list" ]; then list=""; fi

	case "$item" in
		type=*) device_type="${item#*=}" ;;
	esac
done


printf "Content-Type: text/plain\r\n\r\n"
cd /data/sequences
for i in *.sqc
do
	if [ "$i" = "*.sqc" ]; then break; fi

	if [ "${device_type}" = "0" ]
	then
		printf "$i%%"
		continue
	fi

	if [ "${device_type}" = "1" ]
	then
		if grep -i " att01" "$i" > /dev/null 2>&1
		then
			printf "$i%%"
		fi

		if grep -i "DEVICE TYPE: attenuator" "$i" >/dev/null 2>&1
		then
			printf "$i%%"
		fi

		continue
	fi

	if [ "${device_type}" = "2" ]
	then
		if grep -i "DEVICE TYPE: switch" "$i" >/dev/null 2>&1
		then
			printf "$i%%"
		fi
		continue
	fi

	if [ "${device_type}" = "4" ]
	then
		if grep -i "DEVICE TYPE: spst" "$i" >/dev/null 2>&1
		then
			printf "$i%%"
		fi
		continue
	fi
done
