#!/bin/sh
set -eu
exec gawk -e '
		!/\t/ { next }
		{
			# Determine whether the contigs are in reverse order.
			reverse = /^[^\t]*\t[^\t]*,1,[01],D/
			# Orient the contigs.
			$1 = $1 "+"
			rc = 0
			for (i = 2; i <= NF; ++i) {
				if ($i ~ /,1,D/)
					rc = !rc
				sub(/,.*/, rc ? "-" : "+", $i)
			}
			# Print the contig IDs and orientations.
			printf "%u", id++
			if (reverse) {
				for (i = NF; i > 0; --i)
					printf " %s", $i
				printf "\n"
			} else {
				print " " $0
			}
		}' "$@" \
	| gsed -e 's/ /\t/;s/ / 199N /g'
