#! /bin/sh

##  toolbox for the Partclone
##  initial copied from VLC
##  Authors: thomas thomas at nchc dot org dot tw 

###
###  Get a sane environment, just in case
###
LC_ALL=C
export LC_ALL
LANG=C
export LANG

##
##  Give help
##
help()
{
  cat << EOF
recognized flags are:
  --update-po             update translation files
  --update-log            update Changelog files from git
  --update-btrfs          update source of btrfs
  --update-version        generate src/version.h
EOF
  exit 1
}

###
###  argument check
###

if test "$1" = ""
then
  help
fi

case "$1" in
  --update-po)
    action=po
    ;;
  --update-btrfs)
    action=btrfs
    ;;
  --update-log)
    action=changelog
    ;;
  --update-version)
    action=version
    ;;
  --help)
    help
    ;;
  *)
    echo "$0: unknown option $1"
    help
    ;;
esac
shift

##
##  Update the potfiles because no one ever does it
##
if test "${action}" = "po"
then
  # find out the source files
  echo "WARNING: you should run \"make update-po\" instead!" >&2
  cd po
  make update-po
  exit $?
fi

##
##  Update the source of btrfs
##
if test "${action}" = "btrfs"
then
   BTRFS_SOURCE="bitops.h ctree.h extent_io.c inode-item.c math.h radix-tree.c root-tree.c ulist.c volumes.c btrfsck.h dir-item.c extent_io.h inode-map.c print-tree.c radix-tree.h send.h ulist.h volumes.h btrfs-list.c disk-io.c extent-tree.c ioctl.h print-tree.h raid6.c send-stream.c utils.c btrfs-list.h disk-io.h file-item.c kerncompat.h qgroup.c rbtree.c send-stream.h utils.h crc32c.c free-space-cache.c list.h qgroup.h rbtree.h send-utils.c utils-lib.c rbtree-utils.c crc32c.h extent-cache.c free-space-cache.h list_sort.c qgroup-verify.c repair.c send-utils.h uuid-tree.c ctree.c extent-cache.h hash.h list_sort.h qgroup-verify.h repair.h transaction.h version.h rbtree-utils.h rbtree_augmented.h"

    echo "cp $BTRFS_SOURCE partclone/src/btrfs/"

fi
##
## update Changelog from SVN
##
#if test "${action}" = "changelog"
#then
#  echo "Would you want to export svn log to ChangeLog(y/N)?"
#  read log
#  case "$log" in  
#    [yY] | [Yy][Ee][Ss] )
#      if which svn2cl 2>&1 >/dev/null ; then
#        echo "please input svn password to export svn log."
#        svn2cl --reparagraph --break-before-msg=2
#      else
#        echo "WARNING: you should install subversion-tools firstly."
#      fi
#      ;;
#    esac
#  exit $?
#fi
#

##
##  Create version file
##
if test "${action}" = "version"
then
  scriptdir=`dirname "$0"`
  file="$scriptdir/src/version.h"
  GIT_REPOSITORY=`git remote -v | grep partclone`
  GIT_REVISION=`git log -1 | grep ^commit | sed 's/commit //'`
  #GIT_REVISION_UPSTREAM=`git log master -1 | grep commit | sed 's/commit //'`
  git_ver="none"

  if [ "${GIT_REPOSITORY}" = "" ];
  then
    echo "not git repository?"
    exit 0
  fi

  #if [ "${GIT_REVISION_UPSTREAM}" != "" ]; then
  #    git_ver=$GIT_REVISION_UPSTREAM
  #el
  if [ "${GIT_REVISION}" != "" ]; then
      git_ver=$GIT_REVISION
  else
      git_ver="none"
  fi

  if [ "$git_ver" != "none" ];
  then

  echo "create ${file}"
cat > ${file} << EOF
/* DO NOT EDIT THIS FILE - IT IS REGENERATED AT EVERY COMPILE -
 * IT GIVES BETTER TRACKING OF DEVELOPMENT VERSIONS
 * WHETHER THEY ARE BUILT BY OTHERS OR DURING DEVELOPMENT OR FOR THE
 * OFFICIAL PARTCLONE RELEASES.
 */
#define git_version  "$git_ver"

EOF
  fi

  exit 0

fi

if test "${action}" = "changelog"
then
    # get source code from Jim Meyering at 
    # http://git.mymadcat.com/index.php/p/tracker/source/tree/master/gitlog-to-changelog
    perl gitlog-to-changelog --since 1999-01-01 > ChangeLog
fi
