#!/bin/bash
# vim: sw=4 et

t=$(mktemp  -d XXXXXXX)

set -e

pushd $t &>/dev/null


    tar xf ../install_flash_player_11*_linux.i386.tar.gz libflashplayer.so
    md5sum libflashplayer.so > ORIG_FLASH
    rm libflashplayer.so
    # fetch the current download url
    download_url=$(curl -s 'http://get.adobe.com/flashplayer/download/?installer=Flash_Player_11.2_for_other_Linux_%28.tar.gz%29_32-bit&standalone=1' | egrep  -o 'http:\/\/fpdownload.macromedia.com\/get\/flashplayer\/pdc\/11.*\/install_flash_player_11_linux.i386.tar.gz')
    echo "Trying download $download_url";
    wget $download_url
    tar xf install_flash_player_11_linux.i386.tar.gz libflashplayer.so
    md5sum libflashplayer.so > NEW_FLASH

    if cmp -s ORIG_FLASH NEW_FLASH; then

        echo "no new update available"

    else
        echo "*** NEW flashplugin UPDATE AVAILABLE!"
        version=$(strings libflashplayer.so | grep "LNX 11," | cut -d' ' -f2 | sed -e 's#,#.#g')
        echo "new version is $version"
        download_url=$(echo $download_url | sed -e 's,i386,ARCH,')

        for arch in i386 x86_64; do
            wget -q $(echo $download_url | sed -e "s,ARCH,$arch,")
            mv install_flash_player_11_linux.$arch.tar.gz install_flash_player_${version}_linux.$arch.tar.gz
            mv install_flash_player_${version}_linux.$arch.tar.gz ..
        done
        sed -i -e "s,Version:.*,Version:        $version," ../flash-player.spec
        ( cd ..; 
        osc vc -m "Security update to $version (bnc#XXXXXX):"
        )
    fi

    for arch in i386; do
	if tar tf ../flashplayer_*_sa.$arch.tar.gz | grep -q -F install_flash_player_linux_sa/flashplayer ; then
            tar xf ../flashplayer_*_sa.$arch.tar.gz install_flash_player_linux_sa/flashplayer
            md5sum install_flash_player_linux_sa/flashplayer > ORIG_FLASH
            rm -r install_flash_player_linux_sa
        else
            tar xf ../flashplayer_*_sa.$arch.tar.gz flashplayer
            md5sum flashplayer > ORIG_FLASH
            rm flashplayer
        fi
        wget http://fpdownload.macromedia.com/pub/flashplayer/updaters/11/flashplayer_11_sa.$arch.tar.gz
        if tar xvf flashplayer_11_sa.$arch.tar.gz | grep -q -F install_flash_player_linux_sa/flashplayer ; then
            md5sum install_flash_player_linux_sa/flashplayer > NEW_FLASH
	else
            md5sum flashplayer > NEW_FLASH
	fi

        if cmp -s ORIG_FLASH NEW_FLASH; then

            echo "no new update available"

        else
            echo "*** NEW flashplayer UPDATE AVAILABLE!"
            if test -f install_flash_player_linux_sa/flashplayer ; then
                version=$(strings install_flash_player_linux_sa/flashplayer | grep "LNX 11," | cut -d' ' -f2 | sed -e 's#,#.#g')
            else
                version=$(strings flashplayer | grep "LNX 11," | cut -d' ' -f2 | sed -e 's#,#.#g')
            fi
            echo "new version is $version"
            mv flashplayer_11_sa.$arch.tar.gz flashplayer_${version}_sa.$arch.tar.gz
            mv flashplayer_${version}_sa.$arch.tar.gz ..
        fi
    done

popd &>/dev/null
rm -rf $t
