#! /bin/sh

if [ $# -ne 2 ]; then
  echo "usage: get-errno-list [--perl PERL|--python PYTHON]" 1>&2
  exit 1
fi

if [ $1 = "--perl" ]; then
  PERL="$2";
  $PERL -e 'foreach $key (keys(%!)) {
    $x .= "#if defined ($key)\n    { \"$key\", $key, },\n#endif\n";
  }
  while (<>) {
    s/^ *\@SYSDEP_ERRNO_LIST\@/$ x/;
    print;
  }'

elif [ $1 = "--python" ]; then
  PYTHON="$2";
  $PYTHON -c '
from errno import errorcode
from sys import stdin

t = "#if defined (%s)\n    { \"%s\", %s, },\n#endif\n"
errstr = ""
for k in errorcode.keys():
    errstr += t % tuple(3*[errorcode[k]])

for l in stdin:
    print l.replace("@SYSDEP_ERRNO_LIST@", errstr),
'
fi

exit $?
