#!/usr/bin/env bash

# This file is used to generate the annotation of package info that
# records the user, url, revision and timestamp.

unset LANG
unset LC_CTYPE

version=$1
outputDirectory=$2

pushd .

user=`whoami | sed -n -e 's/\\\/\\\\\\\\/p'`
if [ "$user" == "" ]
then
  user=`whoami`
fi
date=`date`
cwd=`pwd`
if [ -d .svn ]; then
  revision=`svn info | sed -n -e 's/Last Changed Rev: \(.*\)/\1/p'`
  url=`svn info | sed -n -e 's/^URL: \(.*\)/\1/p'`
elif [ -d .git ]; then
  revision=`git log -1 --pretty=format:"%H"`
  hostname=`hostname`
  url="git://${hostname}${cwd}"
else
  revision="Unknown"
  url="file://$cwd"
fi
which md5sum > /dev/null
if [ "$?" != "0" ] ; then
  which md5 > /dev/null
  if [ "$?" != "0" ] ; then
    srcChecksum="Unknown"
  else
    srcChecksum=`find src/main/ | grep -e "\.java" -e "\.proto" | LC_ALL=C sort | xargs md5 | md5 | cut -d ' ' -f 1`
  fi
else
  srcChecksum=`find src/main/ | grep -e "\.java" -e "\.proto" | LC_ALL=C sort | xargs md5sum | md5sum | cut -d ' ' -f 1`
fi
popd

mkdir -p "$outputDirectory/com/alibaba/hologres/client"
cat >"$outputDirectory/com/alibaba/hologres/client/Version.java" <<EOF
/*
 * Generated by src/saveVersion.sh
 */
package com.alibaba.hologres.client;

public class Version {
    public static final String version = "$version";
    public static final String revision = "$revision";
    public static final String date = "$date";
    public static final String srcChecksum = "$srcChecksum";
}

EOF

