#!/@unixroot/usr/bin/sh

#
# Strip debug info and compress OS/2 binaries and DLLs using emxomfstrip and lxlite tools.
#
# Usage: brp-strip-os2 RPM_BUILD_SUBDIR [--[no-]compress] [--[no-]debuginfo]
#            [-n|--names <wildcard1[,wildcard2...]>]
#            [-i|--include <wildcard1[,wildcard2...]>]
#            [-x|--exclude <wildcard1[,wildcard2...]>]
#
# RPM_BUILD_SUBDIR is a directory where the souce code of the package is
# unpacked to. A file `debugfiles.list` containing a list of files with debug
# info is created in this directory when the script is instructed to generate
# debug info (by default). This file is then used by the %debug_package macro to
# automatically generate a `debuginfo` sub-package for the given package.
#
# The --no-compress and --no-debuginfo flags completely disable lxlite and emxomfstrip
# invocation, respectively. The -n flag overrides the default list of files that will be
# compressed and stripped (see FILENAMES below). The -i flag includes additional files
# in this list, the -x flag allows to exclude specific files from processing (applied
# after processing the -n and -i flags). The --compress flag makes all subsequent
# -n/-i/-x flags operate only on the file list passed to lxlite, without affecting the
# strip operation. The --debuginfo flag makes these flags operate on the emxomfstrip
# file list, without affecting compression.
#
# Note that until debug info stripping is completely disabled with --no-debuginfo,
# this script will cause emxomstrip to put debug info in separate *.dbg files and will
# write all resulting file names into a file list which is to be used by the
# %debug_package macro to generate the debug package.
#

die()
{
  (>&2 echo "$0: ERROR: $1")
  exit 1
}

# If using normal root, avoid changing anything.
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" -o "$RPM_BUILD_ROOT" = "/@unixroot" ]; then
	exit 0
fi

NO_COMPRESS=
NO_DEBUGINFO=

FILENAMES="*.exe,*.dll,*.pyd"
FILENAMES_EXCLUDE=

COMPRESS_FILENAMES=
COMPRESS_FILENAMES_EXCLUDE=

DEBUGINFO_FILENAMES=
DEBUGINFO_DFILENAMES_EXCLUDE=

RPM_BUILD_SUBDIR="$1"
[ -n "$RPM_BUILD_SUBDIR" ] || die "RPM_BUILD_SUBDIR is not set."
shift

dbgext="dbg"
dbgfilelist="$RPM_BUILD_SUBDIR/debugfiles.list"

var="FILENAMES"

while [ -n "$1" ] ; do
    case "$1" in
    --no-compress)
        NO_COMPRESS=1
        COMPRESS_FILENAMES=
        COMPRESS_FILENAMES_EXCLUDE=
    ;;
    --no-debuginfo)
        NO_DEBUGINFO=1
        DEBUGINFO_FILENAMES=
        DEBUGINFO_DFILENAMES_EXCLUDE=
    ;;
    --compress)
        NO_COMPRESS=
        COMPRESS_FILENAMES="$FILENAMES"
        COMPRESS_FILENAMES_EXCLUDE="$FILENAMES_EXCLUDE"
        var="COMPRESS_FILENAMES"
    ;;
    --debuginfo)
        NO_DEBUGINFO=
        DEBUGINFO_FILENAMES="$FILENAMES"
        DEBUGINFO_DFILENAMES_EXCLUDE="$FILENAMES_EXCLUDE"
        var="DEBUGINFO_FILENAMES"
    ;;
    --names|-n)
        [ -n "$2" ] && { eval ${var}="\$2" ; shift ; }
    ;;
    --include|-i)
        [ -n "$2" ] && { eval ${var}="\${${var}:+\$${var},}\$2" ; shift ; }
    ;;
    --exclude|-x)
        [ -n "$2" ] && { eval ${var}_EXCLUDE="\${${var}_EXCLUDE:+\$${var}_EXCLUDE,}\$2" ; shift ; }
    ;;
    esac
    shift
done

# Exit early if nothing to do
[ -n "$NO_COMPRESS" -a -n "$NO_DEBUGINFO" ] && exit
[ -z "$FILENAMES" -a -z "$COMPRESS_FILENAMES" -a -z "$DEBUGINFO_FILENAMES" ] && exit

sed_names="sed -e 's/^/-name \"/' -e 's/,/\" -o -name \"/g' -e 's/$/\"/'"

find_name_args=`echo "$FILENAMES" | eval $sed_names`

[ -n "$FILENAMES_EXCLUDE" ] && \
    find_name_args="\( $find_name_args \) -a ! \( "`echo "$FILENAMES_EXCLUDE" | eval $sed_names`" \)"

if [ -n "$COMPRESS_FILENAMES" ] ; then
    if [ "$COMPRESS_FILENAMES" != "$FILENAMES" -o "$COMPRESS_FILENAMES_EXCLUDE" != "$FILENAMES_EXCLUDE" ] ; then
        c_find_name_args=`echo "$COMPRESS_FILENAMES" | eval $sed_names`

        [ -n "$COMPRESS_FILENAMES_EXCLUDE" ] && \
            c_find_name_args="\( $c_find_name_args \) -a ! \( "`echo "$COMPRESS_FILENAMES_EXCLUDE" | eval $sed_names`" \)"
    fi
fi

if [ -n "$DEBUGINFO_FILENAMES" ] ; then
    if [ "$DEBUGINFO_FILENAMES" != "$FILENAMES" -o "$DEBUGINFO_FILENAMES_EXCLUDE" != "$FILENAMES_EXCLUDE" ] ; then
        d_find_name_args=`echo "$DEBUGINFO_FILENAMES" | eval $sed_names`

        [ -n "$DEBUGINFO_FILENAMES_EXCLUDE" ] && \
            d_find_name_args="\( $d_find_name_args \) -a ! \( "`echo "$DEBUGINFO_FILENAMES_EXCLUDE" | eval $sed_names`" \)"
    fi
fi

if [ -n "$c_find_name_args" -o -n "$d_find_name_args" ] ; then
    [ -z "$c_find_name_args" ] && c_find_name_args="$find_name_args"
    [ -z "$d_find_name_args" ] && d_find_name_args="$find_name_args"
    if [ "$c_find_name_args" = "$d_find_name_args" ] ; then
        find_name_args="$c_find_name_args"
        c_find_name_args=
        d_find_name_args=
    else
        find_name_args=
    fi
fi

# Now do the job.

if [ -n "$find_name_args" ] ; then
    # Single file set (emxomfstrip must come first!)
    for f in `eval find \"$RPM_BUILD_ROOT\" -type f $find_name_args`; do
        [ -z "$NO_DEBUGINFO" ] && emxomfstrip -D "${f%.*}.$dbgext" $f
        [ -z "$NO_COMPRESS" ] && lxlite /ml1 /mf2 /ydd /yxd /b- "$f"
    done
else
    # Two different file sets (emxomfstrip must come first!)
    if [ -n "$d_find_name_args" ] ; then
        for f in `eval find \"$RPM_BUILD_ROOT\" -type f $d_find_name_args`; do
            emxomfstrip -D "${f%.*}.$dbgext" $f
        done
    fi
    if [ -n "$c_find_name_args" ] ; then
        for f in `eval find \"$RPM_BUILD_ROOT\" -type f $c_find_name_args`; do
            lxlite /ml1 /mf2 /ydd /yxd /b- "$f"
        done
    fi
fi

[ -z "$NO_DEBUGINFO" ] && \
    find "$RPM_BUILD_ROOT" -type f -name "*.$dbgext" | sed -e "s|^$RPM_BUILD_ROOT||" > "$dbgfilelist"

exit 0
