#!/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=dcraw.c_new
TEMPLATEDIR=dcraw-template

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

# grep for the current version
#NEWVERSION=$(grep -i v[0-9][.][0-9][0-9] ${TMPFILE} | sed -e 's/^.*Raw Photo Decoder.*v\([0-9][.][0-9][0-9]\).*$/\1/')
NEWVERSION=$(grep -i '^#define DCRAW_VERSION "[0-9][.][0-9][0-9]"$' ${TMPFILE} | sed -e 's/^#define DCRAW_VERSION "\([0-9][.][0-9][0-9]\)"$/\1/')

if [ -z "${NEWVERSION}" ]
then
  echo "error: Could not get new version number"
  exit 1
fi


# Build a working directory
WORKDIR=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}/dcraw.c

cp "${0}" "${WORKDIR}/getsource"

# Get the other bits and pieces
cd ${WORKDIR}
${WGET} ${DCRAWHOME}dcraw.1
#${WGET} ${DCRAWHOME}dcwrap
${WGET} ${DCRAWHOME}parse.c
#${WGET} ${DCRAWHOME}fixdates.c
${WGET} ${DCRAWHOME}fujiturn.c
${WGET} ${DCRAWHOME}fuji_green.c
${WGET} ${DCRAWHOME}clean_crw.c
${WGET} ${DCRAWHOME}.badpixels

cd ..

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

mv ${WORKDIR}/.badpixels ${WORKDIR}/badpixels

#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 dcraw
cd ..

# Copy in the template debian bits and pieces
cp -a ${TEMPLATEDIR}/dcraw-*/debian ${WORKDIR}
#cp -a ${TEMPLATEDIR}/dcraw-*/dcfixdates.1 ${WORKDIR}
cp -a ${TEMPLATEDIR}/dcraw-*/dcparse.1 ${WORKDIR}
cp -a ${TEMPLATEDIR}/dcraw-*/Makefile ${WORKDIR}
cp -a ${TEMPLATEDIR}/dcraw-*/dcfujiturn.1 ${WORKDIR}
cp -a ${TEMPLATEDIR}/dcraw-*/dcfujiturn16.1 ${WORKDIR}
cp -a ${TEMPLATEDIR}/dcraw-*/dccleancrw.1 ${WORKDIR}
cp -a ${TEMPLATEDIR}/dcraw-*/dcfujigreen.1 ${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]+.*\$$' dcraw.c >> debian/README.Debian

vi debian/README.Debian

dpkg-buildpackage -rfakeroot
