#!/bin/bash

# Adobe is checking the browser agent string to adjust the displayed content.
AGENT="Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0"

# Function to download the latest flash-player tar.gz archives (32bit/64bit).
function get_flash_binaries {
	#AGENT="Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0"

	HTML=$(curl -s -A ${AGENT} 'https://get.adobe.com/flashplayer/')
	STYPE=$(echo "$HTML" | grep -i "stype:" | grep -o '[0-9]\+')

	TMP_URL="https://get.adobe.com/flashplayer/download/?installer=FP_25.0_for_Linux_32-bit_(.tar.gz)_-_NPAPI&sType=${STYPE}&standalone=1"

	DOWNLOAD_URL=$(curl -s -A ${AGENT} ${TMP_URL} )
	DOWNLOAD_URL=$(echo "$DOWNLOAD_URL" | grep setTimeout)
	DOWNLOAD_URL="https${DOWNLOAD_URL##*https}"
	DOWNLOAD_URL="${DOWNLOAD_URL%%tar.gz*}tar.gz"
	
	VERSION=${DOWNLOAD_URL%/*}
	VERSION=${VERSION##*/}

	DOWNLOAD_TARGET=${DOWNLOAD_URL##*/}
	DOWNLOAD_TARGET=${DOWNLOAD_TARGET/_npapi/_npapi_$VERSION}
	wget -q $DOWNLOAD_URL -O ${DOWNLOAD_TARGET}
	if [ $? -eq 0 ]; then
		echo "- i386: done   [${DOWNLOAD_TARGET}]"
	else
		echo "- i386: failed"
	fi
	DOWNLOAD_URL=${DOWNLOAD_URL/i386/x86_64}
	DOWNLOAD_TARGET=${DOWNLOAD_URL##*/}
	DOWNLOAD_TARGET=${DOWNLOAD_TARGET/_npapi/_npapi_$VERSION}
	wget -q ${DOWNLOAD_URL} -O ${DOWNLOAD_TARGET}
	if [ $? -eq 0 ]; then
		echo "- x86_64: done [${DOWNLOAD_TARGET}]"
	else
		echo "- x86_64: failed"
	fi
}


function affected_version {
	AFFECTED=$(echo "$1" | sed '/id="Affected/,/class="header"/!d;//d' | grep -A1 "Adobe Flash Player for" | tail  -n1)
	AFFECTED=${AFFECTED#*>}
	AFFECTED=${AFFECTED%<*}
	AFFECTED=${AFFECTED%% *}
	echo $AFFECTED
}

function fixed_version {
	VERSION=$(echo "$1" | sed '/id="Solution"/,/class="header"/!d;//d' | grep -A1 ">Adobe Flash Player for" | tail -n1)
	VERSION=${VERSION#*>}
	VERSION=${VERSION%<*}
	echo $VERSION
}

function get_cve_numbers {
	CVE_LIST=$(echo "$1" | sed '/id="SecurityupdatesavailableforAdobeFlashPlayer"/,/class="header"/!d;//d' | grep ">CVE number<")
	CVE_LIST=${CVE_LIST##*>CVE number<}
	CVE_LIST="CVE${CVE_LIST#*CVE}"
	CVE_LIST=${CVE_LIST%%<*}
	echo $CVE_LIST
}

function change_log_entry {
	echo "- Security update to $1 (bsc#???????), fixing the following"
	echo "  vulnerabilities advised under $2:"
	BLOCK=0
	echo -n "  *"
	for id in $3;do
		echo -n " $id"
		BLOCK=$(($BLOCK+1))
		if [ $BLOCK -ge 4 ]; then
			echo -ne "\n   "
			BLOCK=0
		fi
	done
	echo
	echo "  * Details:"
	echo "    $4"
}

# Adobe Security Bulletin

ADOBE_SECURITY_URL="https://helpx.adobe.com/security.html"

HTML=$(curl -s -A ${AGENT} $ADOBE_SECURITY_URL)

FLASH_ASB=$(echo "$HTML" | grep "Security updates available for Adobe Flash Player")
echo "$FLASH_ASB" | while read line; do
	ASB="APS${line##*APS}"
	ASB=${ASB%%<*}
	ASB_URL=${line##*href=\"}
	ASB_URL="https://helpx.adobe.com${ASB_URL%%\"*}"
	ASB_HTML=$(curl -s -A ${AGENT} $ASB_URL)
	VERSION=$(fixed_version "$ASB_HTML")
	AFFECTED=$(affected_version "$ASB_HTML")
	echo -e "flash-player version: $AFFECTED -> $VERSION\n"
	CVE_LIST=$(get_cve_numbers "$ASB_HTML")
	#echo $ASB $VERSION $AFFECTED $CVE_LIST
	change_log_entry $VERSION $ASB "$CVE_LIST" $ASB_URL

	echo
	echo "RUN COMMANDS:"
	echo "$> rm flash_player_npapi_${AFFECTED}_linux.*.tar.gz"
	echo "$> sed 's/${AFFECTED}/${VERSION}/' -i flash-player.spec"
	echo "$> osci vc"
	echo "$> osci ar"
	echo "$> osci ci"
	echo "$> osci sr"
	echo
	exit
done

if [ "$1" = "download" ]; then
	echo "Downloading new flash-player binaries..."
	get_flash_binaries
else
	echo "Use '$0 download' to download the latest flash-player version."
fi

exit
