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

# If you want to update more than one repository at once, set
# FLASH_PLAYER_UPDATE_COMMON_DESC variable to a place in writable
# directory. If the file exists, it will be used, otherwise it will be
# created.
if test -n "$FLASH_PLAYER_UPDATE_COMMON_DESC" ; then
	if test -f "$FLASH_PLAYER_UPDATE_COMMON_DESC" ; then
		. $FLASH_PLAYER_UPDATE_COMMON_DESC
	else
		echo -n >$FLASH_PLAYER_UPDATE_COMMON_DESC
	fi
fi

function cache_set() {
if test -n "$FLASH_PLAYER_UPDATE_COMMON_DESC" ; then
    echo >>$FLASH_PLAYER_UPDATE_COMMON_DESC "$1=\"$2\""
fi
}

set -e

if test -z "$UPDATE_EXISTS" ; then
    # Unknown status of UPDATE_EXISTS means no FLASH_PLAYER_UPDATE_COMMON_DESC

    t=$(mktemp  -d XXXXXXX)

    mkdir ORIG_FLASH_SAVE
    mv *.tar.gz ORIG_FLASH_SAVE
    pushd $t &>/dev/null

    UPDATE_EXISTS=false

    tar xf ../ORIG_FLASH_SAVE/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 'https*:\/\/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 ..
	    cache_set UPDATE_EXISTS "true"
	    cache_set VERSION "$VERSION"
	    cache_set INSTALL_FLASH_PLAYER_FILENAME_$arch install_flash_player_${VERSION}_linux.$arch.tar.gz
        done
        UPDATE_EXISTS=true
    fi

    for arch in i386; do
	if tar tf ../ORIG_FLASH_SAVE/flashplayer_*_sa.$arch.tar.gz | grep -q -F install_flash_player_linux_sa/flashplayer ; then
            tar xf ../ORIG_FLASH_SAVE/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 ../ORIG_FLASH_SAVE/flashplayer_*_sa.$arch.tar.gz flashplayer
            md5sum flashplayer > ORIG_FLASH
            rm flashplayer
        fi
        wget https://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"

	    if $UPDATE_EXISTS ; then
		"UPDATE WARNING: Update of install_flash_player exists but update of flashplayer does not."
		"UPDATE WARNING: You have to edit spec file manually!"
	    fi
        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 ..
	    cache_set FLASH_PLAYER_FILENAME_$arch flashplayer_${VERSION}_sa.$arch.tar.gz
        fi
    done

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

    cache_set FLASH_PLAYER_DIRNAME $PWD
    if $UPDATE_EXISTS ; then
	rm -r ORIG_FLASH_SAVE
    else
	cache_set UPDATE_EXISTS false
	mv ORIG_FLASH_SAVE/* .
	rmdir ORIG_FLASH_SAVE
	exit 2
    fi

    # Create changelog entry.
    echo -n "Please enter BSC# (number only${BSC:+, press Enter for \"$BSC\"}): "
    read BSC
    if test -z "$BSC" ; then
	echo >&2 "BSC is empty!"
	exit 1
    fi
    cache_set BSC "$BSC"

    echo -n "Please enter Adobe Vulnerability identifier (including APSA/APSB): "
    read APSA
    if test -z "$APSA" ; then
	echo >&2 "APSA is empty!"
    fi
    cache_set APSA "$APSA"

    echo -n "Please enter list of CVE (\", \" separated): "
    read CVE
    if test -z "$CVE" ; then
	echo >&2 "CVE is empty!"
	exit 1
    fi
    cache_set CVE "$CVE"

    osc vc -m "Security update to $VERSION (bsc#$BSC):
  * $(echo "    $APSA, $CVE" | fmt -w 67 | tail -c +5)"

else # Update was already done in another repository.

    if $UPDATE_EXISTS ; then
	# ./update.sh was already called in another directory. Reuse its output.

	rm *.tar.gz
	cp -a $FLASH_PLAYER_DIRNAME/$INSTALL_FLASH_PLAYER_FILENAME_i386 .
	cp -a $FLASH_PLAYER_DIRNAME/$INSTALL_FLASH_PLAYER_FILENAME_x86_64 .
	cp -a $FLASH_PLAYER_DIRNAME/$FLASH_PLAYER_FILENAME_i386 .

	sed -n '1p;1,/^-----/{/^-----/d;p}' <$FLASH_PLAYER_DIRNAME/flash-player.changes >flash-player.changes.add
	cat flash-player.changes.add flash-player.changes >flash-player.changes.new
	mv flash-player.changes.new flash-player.changes
	rm flash-player.changes.add
    else
	exit 2
    fi
fi

sed -i -e "s,Version:.*,Version:        $VERSION," flash-player.spec

osc addremove

echo "Done. Please call \"osc ci\"."
