#!/bin/bash -u

# if I was run from not bash, run me from bash
if [[ -z "$BASH" ]]; then
    bash -u $@
    exit $?
fi

source ./locations.include

##########
# setup args in the right order for making getopt evaluation
# nice and easy.  You'll need to read the manpages for more info
args=$(getopt -o h -- "$@")
eval set -- "$args"

usage() {
    echo ''            >&2
    echo "$0 [target]" >&2
    echo ''            >&2

    echo "This script looks for debuginfo and stages it as needed" >&2
    echo ''           >&2
    echo 'This should be called by one of the "moveto" scripts' >&2
    echo ''           >&2
    echo ' target is one of:'    >&2
    echo '   distro'    >&2
    echo '   fastbugs'    >&2
    echo '   security'    >&2
    exit 1
}

for arg in $@; do
    case $1 in
        -- )
            # end of getopt args, shift off the -- and get out of the loop
            shift
            break 2
           ;;
         -h )
            # get help
            usage
           ;;
    esac
done

target=${1:-}
if [[ -z "${target}" ]]; then
    usage
fi

# for SL6 there actually is an i386 tree we care about
if [[ "${ARCH}" == 'i386' ]]; then
    if [[ "${VERSION}" != '6' ]]; then
        mv *debuginfo*.rpm ../x86_64/
        exit 0
    fi
fi

mkdir -p ${NOTDONEYET}/
if [[ "${target}" == 'distro' ]]; then
    mv -fv *debuginfo*.rpm ${NOTDONEYET}/
elif [[ "${target}" == 'fastbugs' ]]; then
    ln -f *debuginfo*.rpm ${NOTDONEYET}/
    mkdir -p ${PRE_RELEASE_FASTBUGS}/${ARCH}/debuginfo/
    mv -fv *debuginfo*.rpm ${PRE_RELEASE_FASTBUGS}/${ARCH}/debuginfo/
elif [[ "${target}" == 'security' ]]; then
    ln -f *debuginfo*.rpm ${NOTDONEYET}/
    mkdir -p ${PRE_RELEASE_SECURITY}/${ARCH}/debuginfo/
    mv -fv *debuginfo*.rpm ${PRE_RELEASE_SECURITY}/${ARCH}/debuginfo/
else
    usage
fi

