#!/bin/sh

TARGETDIR=/srv/poky/autobuild-output
if [ "$1" = "/srv/poky/autobuild/full-shihtzu/build" ]; then
    DISTRO="poky"
fi
if [ "$1" = "/srv/poky/autobuild/full-bleeding-shihtzu/build" ]; then
    DISTRO="poky-bleeding"
fi
if [ "$1" = "/srv/poky/autobuild/toolchain-shihtzu/build" ]; then
    DISTRO="toolchain"
fi
if [ "$1" = "/srv/poky/autobuild/incremental-shihtzu/build" ]; then
    DISTRO="incremental"
fi

cd $1/build/tmp/deploy/

LASTREV=`tail images/svninfo | grep Revision | cut -f 2 -d ' '`
DEST=$TARGETDIR/$DISTRO/$LASTREV/

mkdir -p $DEST

COMPRESS_FILES=()
FILES=()

if [ "x$DISTRO" = "xpoky-bleeding" ]; then
    COMPRESS_FILES=(
        poky-image-sato-qemuarm-*.rootfs.ext2
    )

    FILES=(
        svninfo
        `readlink images/zImage-akita.bin`
        `readlink images/zImage-qemuarm.bin`
        updater.sh.akita
        poky-image-sato-akita-*.rootfs.summary.jffs2
        poky-image-sato-qemuarm-*.rootfs.tar.bz2
    )
fi

if [ "x$DISTRO" = "xtoolchain" ]; then
    FILES=(
        svninfo
        poky-*-toolchain-*.tar.bz2
    )
fi

if [ "x$DISTRO" = "xpoky" ]; then
    COMPRESS_FILES=(
        poky-image-sdk-qemuarm-*.rootfs.ext2
        poky-image-sdk-qemux86-*.rootfs.ext2
        poky-image-minimal-qemuarm-*.rootfs.ext2
        poky-image-minimal-qemux86-*.rootfs.ext2
        poky-image-sato-cd-*.iso
    )

    FILES=(
        svninfo
        `readlink images/zImage-akita.bin`
        `readlink images/zImage-c7x0.bin`
        `readlink images/zImage-qemuarm.bin`
        `readlink images/zImage-spitz.bin`
        `readlink images/zImage-nokia800.bin`
        `readlink images/bzImage-qemux86.bin`
        `readlink images/zImage-htcuniversal.bin`
        `readlink images/zImage-mx31litekit.bin`
        `readlink images/zImage-mx31ads.bin`
        `readlink images/zImage-nokia770.bin`
        `readlink images/zImage-zylonite.bin`
        `readlink images/zImage-cm-x270.bin`
        `readlink images/uImage-em-x270.bin`
        `readlink images/uImage-mx31phy.bin`
        `readlink images/uImage-neo1973-latest.bin`
        `readlink images/uImage-fic-gta01.bin`
        updater.sh.akita
        updater.sh.c7x0
        updater.sh.spitz
        gnu-tar
        poky-image-sato-akita-*.rootfs.summary.jffs2
        poky-image-sato-c7x0-*.rootfs.jffs2
        poky-image-sato-spitz-*.rootfs.tar.gz
        poky-image-sdk-qemuarm-*.rootfs.tar.bz2
        poky-image-sdk-qemux86-*.rootfs.tar.bz2
        poky-image-minimal-qemuarm-*.rootfs.tar.bz2
        poky-image-minimal-qemux86-*.rootfs.tar.bz2
        poky-image-sdk-spitz-*.rootfs.tar.gz
        poky-image-sdk-nokia800-*.rootfs.jffs2
        poky-image-sato-nokia770-*.rootfs.jffs2
        poky-image-sato-zylonite-*.rootfs.jffs2
        poky-image-sato-cm-x270-*.rootfs.jffs2
        poky-image-sato-em-x270-*.rootfs.jffs2
        poky-image-sato-mx31litekit-*.rootfs.tar.gz
        poky-image-sato-htcuniversal-*.rootfs.tar.gz
        poky-image-sato-fic-gta01-*.rootfs.jffs2
        poky-image-sato-mx31phy-*.jffs2
        poky-image-sato-mx31ads-*.jffs2
        `readlink images/updater-em-x270.ext2`
    )
fi

for FILE in ${FILES[@]}; do
    for FILE2 in `find -name $FILE`; do
        if [ ! -e "$DEST/$FILE2" ]; then
            cp $FILE2 $DEST
        fi
    done
done

for FILE in ${COMPRESS_FILES[@]}; do
    for FILE2 in `find -name $FILE`; do
        if [ ! -e "$DEST/$FILE2.bz2" ]; then
            bzip2 $FILE2
            cp $FILE2.bz2 $DEST
        fi
    done
done

if [ -e ./images-complete ]; then
    touch $DEST/complete
fi
chmod a+w -R $DEST

