#! /bin/sh -e

INCDIR=/usr/include
MBINDIR=/usr/libexec/ustr-1.0.4

NOT_DONE=1
inst32()
{
  foo=no
  if [ "x$1" = "xeither" ]; then
    foo=either
    shift
  fi
  if [ -f "$MBINDIR/ustr-import-32" ]; then
    "$MBINDIR/ustr-import-32" "$@"
    RETVAL=$?
  fi

  if [ "x$foo" = "xeither" ]; then
    # This assume an installed multilib ustr-conf.h
    if [ $RETVAL -eq 0 ] && [ -f ustr-conf.h ]; then
        mv ustr-conf.h ustr-conf-32.h
        cp "$INCDIR/ustr-conf.h" .
    fi
    if [ $RETVAL -eq 0 ] && [ -f ustr-conf-debug.h ]; then
        mv ustr-conf-debug.h ustr-conf-debug-32.h
        cp "$INCDIR/ustr-conf-debug.h" .
    fi
    NOT_DONE=0
  else
    exit $RETVAL
  fi
}

inst64()
{
  foo=no
  if [ "x$1" = "xeither" ]; then
    foo=either
    shift
  fi
  if [ -f "$MBINDIR/ustr-import-64" ]; then
    "$MBINDIR/ustr-import-64" "$@"
    RETVAL=$?
  fi

  if [ "x$foo" = "xeither" ]; then
    # This assume an installed multilib ustr-conf.h
    if [ $RETVAL -eq 0 ] && [ -f ustr-conf.h ]; then
        mv ustr-conf.h ustr-conf-64.h
        cp "$INCDIR/ustr-conf.h" .
    fi
    if [ $RETVAL -eq 0 ] && [ -f ustr-conf-debug.h ]; then
        mv ustr-conf-debug.h ustr-conf-debug-64.h
        cp "$INCDIR/ustr-conf-debug.h" .
    fi
    NOT_DONE=0
  else
    exit $RETVAL
  fi
}

if [ "x$1" = "x--32" ]; then
  shift
  inst32 $@
fi

if [ "x$1" = "x--64" ]; then
  shift
  inst64 $@
fi

inst64 either $@
inst32 either $@

exit $NOT_DONE
