#!/bin/bash

PATH=/usr/bin:/bin

set -x
set -u
set -e

DCRAWHOME=http://www.cybercom.net/~dcoffin/dcraw/
WGET=wget

http_proxy=
export http_proxy

TMPFILE=rawphoto.c_new
TEMPLATEDIR=dcraw-template

# Get the new source
${WGET} -O ${TMPFILE} ${DCRAWHOME}rawphoto.c

# grep for the current version
NEWVERSION=$(grep -i '\$Revision: [0-9][.][0-9][0-9] \$' ${TMPFILE} | sed -e 's/^.*Revision:.*\([0-9][.][0-9][0-9]\).*$/\1/')

# Build a working directory
WORKDIR=gimp-dcraw-${NEWVERSION}
if [ -d ${WORKDIR} ]
then
  echo "Working directory for current version already exists. stop"
  exit 0
fi

rm -rf ${WORKDIR}
mkdir ${WORKDIR}
mv ${TMPFILE} ${WORKDIR}/rawphoto.c

# Build the .orig tar file
rm -rf ${WORKDIR}.orig
cp -a ${WORKDIR} ${WORKDIR}.orig

#tar -cvzf ${WORKDIR}.orig.tar.gz ${WORKDIR}.orig
#rm -rf ${WORKDIR}.orig

# Build a template directory with apt-get source
rm -rf ${TEMPLATEDIR}
mkdir ${TEMPLATEDIR}
cd ${TEMPLATEDIR}
apt-get source gimp-dcraw
cd ..

# Copy in the template debian bits and pieces
cp -a ${TEMPLATEDIR}/gimp-dcraw-*/debian ${WORKDIR}
cp -a ${TEMPLATEDIR}/gimp-dcraw-*/Makefile ${WORKDIR}

# Prep the changelog
cd ${WORKDIR}
dch --newversion=${NEWVERSION}-1
egrep '\$Date: [0-9]{4}/[0-9]+/[0-9]+ [0-9]+:[0-9]+:[0-9]+.*\$$' rawphoto.c >> debian/README.Debian

vi debian/README.Debian

dpkg-buildpackage -rfakeroot
