#!/bin/sh -e
# vim:ts=2:et
# Separate tarball/patch build system by Adam Heath <doogie@debian.org>
# Modified by Ben Collins <bcollins@debian.org>
# Modified by Brian May <bam@debian.org>

DBS_FILE2CAT=/usr/share/dbs/internal/file2cat
DBS_VARSBUILD=/usr/share/dbs/internal/vars.build
DBS_LIB=/usr/share/dbs/internal/lib


######################
# PARSE COMMAND LINE #
######################
usage () {
  echo "dbs-edit-patch, edit/create a dbs patch file" >&2
  echo "   usage: dbs-edit-patch [-t|--tmpdir dir] [-s|--sourcedir dir] " >&2
  echo "                         [-P|--sourcepatchdir dir] [-T|--sourcetardir dir]" >&2
  echo "                         [-p|--strip level] patch" >&2
}

. $DBS_LIB

TEMP=""
CDPATH=""
# stolen from /usr/bin/fakeroot, Debian potato version.
GETOPTEST=`getopt --version`
case $GETOPTEST in
getopt*) # GNU getopt
    TEMP=`getopt -n dbs-edit-patch -l help,tmpdir:,sourcedir:,strip:,sourcepatchdir:,sourcetardir: -- +ht:s:p:P:T: "$@"`
    ;;
*) # POSIX getopt ?
    TEMP=`getopt ht:s:p:T:P: "$@"`
    ;;
esac

if test "$?" != "0"
then
  echo "Terminating..." >&2
  exit 1
fi

# Initialize value of temporary directory to $TMP || /tmp
TMP_DIR="${TMP:-/tmp}"

STRIP_LEVEL=0

SOURCE_DIR=`pwd`
if ! test -d ./debian 
then
  TMP_SD="${SOURCE_DIR%/*}"
  while test "$TMP_SD" != "" ; do
    test -d "$TMP_SD/debian"  && { SOURCE_DIR="$TMP_SD" ; break ; }
    TMP_SD="${TMP_SD%/*}"
  done
fi  

eval set -- "$TEMP"
while test "$1" != "--"; do
  case "$1" in
    -p|--strip)
      shift
      STRIP_LEVEL="$1"
      ;;
    -t|--tmpdir)
      shift
      TMP_DIR="$1"
      ;;
    -s|--sourcedir)
      shift
      SOURCE_DIR="$1"
      ;;
    -P|--sourcepatchdir)
      shift
      SRC_PATCH_DIR="$1"
      ;;
    -T|--sourcetardir)
      shift
      SRC_TAR_DIR="$1"
      ;;
    -h|--help)
      usage
      exit 0
      ;;
  esac
  shift
done

# Defaults that depend on other variables
HOOK_DIR="${HOOK_DIR:-$SOURCE_DIR/debian/dbs-hooks}"

shift

if [ $# -lt 1 ]
then
  usage
  exit 
fi

PATCH="$1"; shift

# ENSURE TMP DIRECTORY EXISTS (where files to be edited will go)
if [ ! -d "$TMP_DIR" ]
then 
  echo "Cannot find directory $TMP_DIR" >&2
  exit 1
fi

if ! cd "$SOURCE_DIR" ; then
  echo "Cannot change directory to $SOURCE_DIR" >&2
  exit 1
fi  
  

# CREATE DIRECTORY WITH SAME NAME AS PATCH FILE
OUT_DIR="$TMP_DIR/$PATCH"
SOURCE_DIR="$OUT_DIR"
STAMP_DIR="$SOURCE_DIR/.stampdir"

rm -rf "$OUT_DIR"
mkdir "$OUT_DIR"
mkdir "$STAMP_DIR"

if [ ! -d "$OUT_DIR" ]
then 
  echo "Cannot create directory $OUT_DIR" >&2
  exit 1
fi



# Initialize values using the debian/vars file, if exists
if [ -e "debian/vars" ] ; then
   echo "Parse debian/vars" 
   STRIP_LEVEL=1
   sh "$DBS_VARSBUILD" "debian/vars" shell > "$STAMP_DIR/vars.sh"
   # SOURCE_DIR is the package root for packaged DBS, and where unpacked (=build-tree) for glib DBS
   . "$STAMP_DIR/vars.sh"
  SOURCE_DIR="$OUT_DIR"
  STAMP_DIR="$SOURCE_DIR/.stampdir"
fi

DIFF_EXCLUDE=`for i in $DIFF_EXCLUDE ; do echo -n "-x $i "; done`

#### ENSURE PATCH DIRECTORY EXISTS (directory with patches)
###if [ ! -d "$PATCH_DIR" ]
###then 
###  echo "Cannot find patch dir $PATCH_DIR" >&2
###  exit 1
###fi
###
###
###if [ "$STRIP_LEVEL" != 1 -a "$STRIP_LEVEL" != 0 ]
###then 
###  echo "Invalid value for -p or --strip" >&2
###  exit 1
###fi
###

#t EXTRACT SOURCE FILES
source_unpack
# APPLY UPSTREAM PATCHES
source_patch

# APPLY PATCHES UP TO (and not including) $PATCH
NEW_PATCH=""
echo "Applying patches up to (and not including) the requested patch"
if [ ! -f "$PATCH_DIR/$PATCH" ]; then
  touch "$PATCH_DIR/$PATCH"
  NEW_PATCH="true"
fi

patch_apply --stop "$PATCH"

if [ -n "$NEW_PATCH" ]; then
  rm "$PATCH_DIR/$PATCH"
fi

# MAKE COPY OF SOURCE
cat >> "$OUT_DIR/dbs-update-patch" <<EOF
#!/bin/sh -e
cd "$OUT_DIR"
HOOK_DIR=$HOOK_DIR
test -d "\$HOOK_DIR" && run-parts "\$HOOK_DIR" --arg update-patch-prediff
find -name "*.bak" -print0 | xargs -0 --no-run-if-empty rm
find -name "*~" -print0 | xargs -0 --no-run-if-empty rm
: > new_patch
EOF

bs=`basename $STAMP_DIR`
for f in `find "$OUT_DIR" -mindepth 1 -maxdepth 1 ! -name "$bs" -type d`; do 
  file=`basename "$f"`
  echo -n "Copying $file to $file-old ... "; 
  cp -a "$f" "$f-old"
  echo "successful." 

# CREATE SHELL SCRIPT TO UPDATE PATCH
cat >> "$OUT_DIR/dbs-update-patch" <<EOF
diff -ruN $DIFF_EXCLUDE $file-old $file >> new_patch || test \$? -eq 1
EOF
done

cat >> $OUT_DIR/dbs-update-patch <<EOF
mv new_patch "`pwd`/$PATCH_DIR/$PATCH"
test -d "\$HOOK_DIR" && run-parts "\$HOOK_DIR" --arg update-patch-postdiff
EOF

chmod +x $OUT_DIR/dbs-update-patch

# APPLY LAST PATCH
if [ -n "$NEW_PATCH" ]; then
   echo "Patch does not yet exist; will create a new patch $PATCH";
else
  patch_apply --stop-include "$PATCH"
fi
