#!/bin/sh

dh_testdir debian/get-latest-source|| exit 1

sourceLinkName=$(curl https://sourcesup.cru.fr/frs/?group_id=379 2>/dev/null | sed -n 's%.*\(/frs/download.php/[0-9]*/wims-[0-9][.0-9]*[a-z]*.tgz\)">\(.*\)</a>.*%\1 \2% p'|sort -k 2| tail -1)

link=$(echo ${sourceLinkName}| awk '{print $1}')
name=$(echo ${sourceLinkName}| awk '{print $2}')
name=$(echo ${name} | sed s/.tgz//)~dfsg1
version=$(echo ${name} | sed s/wims-//)
wimshome=debian/temp-dir/${name}/wims

rm -rf debian/temp-dir
mkdir -p ${wimshome}

curl https://sourcesup.cru.fr${link} 2>/dev/null | gzip -cd | tar xf - -C ${wimshome}

#### delete binaries ###
rm -f ${wimshome}/bin/true
rm -f ${wimshome}/bin/false
find ${wimshome}/public_html/java -name "*.jar" | xargs rm -f
find ${wimshome}/src/Misc/authors/jm.evers/applets  -name "*.jar" | xargs rm -f
find ${wimshome}/src/Misc/applets -name "*.jar" | xargs rm -f
rm -f ${wimshome}/public_html/flash/*.swf

### delete symbols which will be generated during the post-installation ###
rm  -f ${wimshome}/public_html/gifs/symbols/20/*.gif

### create the new orig.tar.gz file ###

srcpackage=wims_${version}.orig.tar.gz

(cd debian/temp-dir && tar pcf - ${name} | gzip -9 - > ../../../${srcpackage})

echo "Created ../${srcpackage}"

rm -rf debian/temp-dir
