#!/bin/sh

SOURCES_LIST="/etc/apt/sources.list"

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-apt-sources" "$1"
}

#turn off auto expansion
set -f

. /usr/share/alterator/build/backend3.sh

on_message()
{
	case "$in_action" in
		constraints)
			echo '('
			if [ "$in_orig_action" = "new" ]; then
				req="#t"
			else
				req="#f"
			fi
			printf 'type (required %s label "%s")' "$req" "$(_ "Type")"
			printf 'sign (required #f label "%s")' "$(_ "Signature")"
			printf 'uri (required %s label "%s")' "$req" "$(_ "URI")"
			printf 'arch (required %s label "%s")' "$req" "$(_ "Architecture")"
			printf 'comp (required %s label "%s")' "$req" "$(_ "Components")"
			echo ')'
			;;
		list)
			case "$in__objects" in
				/)
					local line=

					echo '('
					while read line; do
						line="$(echo " $line " | sed -e 's,^[[:space:]]\+,,; s,[[:space:]]\+$,,; s,[[:space:]]\+, ,g;')"
						[ -n "${line%#*}" ] || continue
						echo "$line" | sed -e 's,^[[:space:]]*\(rpm\(-src\)\?\)[[:space:]]\+\[\([^]]\+\)\][[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+\(.\+\)[[:space:]]*$,("'"$line"'" type "\1" sign "\3" uri "\4" arch "\5" comp "\6"),' \
										   -e 's,^[[:space:]]*\(rpm\(-src\)\?\)[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+\(.\+\)[[:space:]]*$,("'"$line"'" type "\1" uri "\3" arch "\4" comp "\5"),'
					done < "$SOURCES_LIST"
					echo ')'
					;;
				avail_signs)
					local f

					echo '('
					printf '("" label "%s")' "$(_ "No signature")"
					find /etc/apt/vendors.list /etc/apt/vendors.list.d -type f -name '*.list' | while read f ; do
						[ -f "$f" ] || continue
						awk 'BEGIN{ RS="simple-key"; } $1 != "" && /Name/ && ! /Group/ { print "(\""gensub(/"/, "", "g", $1) "\" label \"" gensub(/.+Name[[:blank:]]+"([^"]+)".+$/, "\\1", "", $0) "\")"; }' < "$f"
					done
					echo ')'
					;;
				avail_types)
					echo '('
					printf '("rpm" label "%s")' "$(_ "RPMs")"
					printf '("rpm-src" label "%s")' "$(_ "Source RPMs")"
					echo ')'
					;;
				*)
					echo '(#f)'
				esac
			;;
		read)
			echo '()'
			;;
		write)
			echo '()'
			;;
		new)
			local err= newline=

			if [ -n "$in_sign" ]; then
				newline="$(printf '%s [%s] %s %s %s' "$in_type" "$in_sign" "$in_uri" "$in_arch" "$in_comp")"
			else
				newline="$(printf '%s %s %s %s' "$in_type" "$in_uri" "$in_arch" "$in_comp")"
			fi
			newline="$(echo " $newline " | sed -e 's,^[[:space:]]\+,,; s,[[:space:]]\+$,,; s,[[:space:]]\+, ,g;')"
			while read line; do
				line="$(echo " $line " | sed -e 's,^[[:space:]]\+,,; s,[[:space:]]\+$,,; s,[[:space:]]\+, ,g;')"
				[ -n "${line%#*}" ] || continue
				if [ "$line" = "$newline" ]; then
					err="$(_ "Source already exists")"
					break
				fi
			done < "$SOURCES_LIST"
			if [ -n "$err" ]; then
				printf '(error "%s")' "$err"
			else
				printf '%s\n' "$newline" >> "$SOURCES_LIST"
				echo '()'
			fi
			;;
		delete)
			local err="$(_ "Source does not exists")"
			local f="$(mktemp "$SOURCES_LIST.XXXXXXXXX")"
			local oldline="$(echo " $in__objects " | sed -e 's,^[[:space:]]\+,,; s,[[:space:]]\+$,,; s,[[:space:]]\+, ,g;')"

			while read tline; do
				line="$(echo " $tline " | sed -e 's,^[[:space:]]\+,,; s,[[:space:]]\+$,,; s,[[:space:]]\+, ,g;')"
				if [ "$line" = "$oldline" ]; then
					err=
				else
					printf '%s\n' "$tline" >> "$f"
				fi
			done < "$SOURCES_LIST"
			if [ -n "$err" ]; then
				printf '(error "%s")' "$err"
				rm -f "$f"
			else
				echo '()'
				mv -f "$f" "$SOURCES_LIST"
			fi
			;;
		*)
			echo '#f'
			;;
	esac
}

message_loop
