#!/bin/sh
#
# Copyright © 2014, 2021 Guillem Jover <guillem@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

set -e

arch="$1"
if [ -z "$arch" ]; then
  echo "error: missing architecture argument" 2>&1
  exit 1
fi

case "$arch" in
i386)
  sarch=amd64
  ;;
armel|armhf)
  sarch=arm64
  ;;
mips)
  sarch=mips64
  ;;
mips64el)
  # The host is mipsel, but the kernel is 64-bit MIPS.
  sarch=mipsel
  ;;
mipsel)
  sarch=mips64el
  ;;
powerpc)
  sarch=ppc64
  ;;
esac

archfilter="(architecture=$arch)"
if [ -n "$sarch" ]; then
  archfilter="(|$archfilter(architecture=$sarch))"
fi

exec ldapsearch -LLL -x -h db.debian.org -b ou=hosts,dc=debian,dc=org \
  "(&(purpose=porterbox)$archfilter)" hostname architecture | \
  sed -e '/^dn:/d;s/^hostname:/host:/;s/^architecture:/arch:/'
