#!/bin/sh -e

all_systems_cpus="`dpkg-architecture -L | sed -e '/^$/d' -e '/.*-.*/!s/^/linux-/'`"
all_cpus="`echo "$all_systems_cpus" | sed -e "s/.*-//" | sort -u`"
all_systems="`echo "$all_systems_cpus" | sed -e "s/-.*//" | sort -u`"

if [ "$#" = "0" ] ; then
  echo "Known cpus: `for i in ${all_cpus} ; do echo -n $i\  ; done`"
  echo "Known systems: `for i in ${all_systems} ; do echo -n $i\  ; done`"
  exit
fi

negated="0"
reverted="0"
cpus=""
systems=""

while [ $# -gt 0 ]; do
  case "$1" in
    "-n"|"--negated")	negated="1" ;;
    "-r"|"--reverted")  reverted="1" ;;
    *)
      if [ "${cpus}" = "" ] ; then
        cpus="$1"
      else
        systems="$1"
      fi
    ;;
  esac
  shift
done

# Some ugly conversions
systems="`echo ${systems} | sed "s/linux-gnu/linux/g" | sed "s/\bgnu/hurd/g"`"
cpus="`echo ${cpus} | sed "s/x86_64/amd64/g" | sed "s/arm\b/arm,armel/g"`"

# check if we have a cached result for this
if test -e "/usr/share/type-handling/${cpus}:${systems}" \
  && [ ":${TYPE_HANDLING_BOOTSTRAP}" = ":" ] \
  && [ "${negated}:${reverted}" = ":" ] ; then
  cat "/usr/share/type-handling/${cpus}:${systems}"
  exit
fi

# "all" / "any" wildcards
case "${cpus}:${systems}" in
  all:*)		echo all ; exit ;;
  any:any|any:all)	echo any ; exit ;;
  any:*)		cpus=`for i in ${all_cpus}; do echo -n $i, ; done` ;;
  *:any|*:all)		systems=`for i in ${all_systems}; do echo -n $i, ; done` ;;
esac

# replace commas with spaces
cpus="`echo ${cpus} | sed "s/,/ /g"`"
systems="`echo ${systems} | sed "s/,/ /g"`"

# match:negated:reverted logic table
#
# 000	- ""
# 001	- "!arch"
# 010	- "arch"
# 011	- ""
# 100	- "arch"
# 101	- ""
# 110	- ""
# 111	- "!arch"

# for each concievable type..
(for cpu in ${all_cpus} ; do for system in ${all_systems} ; do
  # if it's in our list..
  if echo " ${cpus} " | grep -q " ${cpu} " && echo " ${systems} " | grep -q " ${system} " ; then
    match="1"
  else
    match="0"
  fi
  case "${match}${negated}${reverted}" in
    010|100)
      # make sure it exists..
      if echo "${all_systems_cpus}" | grep -q "${system}-${cpu}" ; then
        echo -n " ${system}-${cpu}"
      fi
    ;;
    001|111)
      # make sure it exists..
      if echo "${all_systems_cpus}" | grep -q "${system}-${cpu}" ; then
        echo -n " !${system}-${cpu}"
      fi
    ;;
  esac
done ; done) | cut -c 2- | sed -e "s/linux-//g"
echo
