#!/bin/bash 

git_env_home=`pwd`
file=.git_env

if [ ! -f "$file" ] ; then
  echo "Move to hypersql-dev-util directory and try again"
  exit -1
fi

cd $git_env_home/..
project_home=`pwd`

export PROJECT_HOME=$project_home
export PROJECT_HOME_NAME=${PROJECT_HOME##*/}

init_github_info()
{
    cur_username=`git config --local user.name`
    if test -z $cur_username ; then
      echo "Configuring github account information"
      read -p "Enter github username : " GITHUB_USERNAME
      read -p "Enter github email    : " GITHUB_EMAIL

      git config --local user.name ${GITHUB_USERNAME}
      git config --local user.email ${GITHUB_EMAIL}
      git config --local core.editor "vim"
    fi
}

# Copy git hook scripts if it has changed
if test ! -z $project_home/.git ; then
    if [ -d $git_env_home/githooks ]; then
        src_dir=$git_env_home/githooks
        tar_dir=$project_home/.git/hooks
    
        for file in `\ls $src_dir`; do
            sfile=$src_dir/$file
            tfile=$tar_dir/$file
    
            if [ -f "$tfile" ]; then
                cmp $sfile $tfile > /dev/null 2>&1
                if [ "$?" = "0" ]; then
                    continue;
                fi
            fi
    
            #copy if file changed
            echo "Copying git script... $file"
            \cp -p -f $sfile $tfile
        done
    fi
    /bin/sh $git_env_home/gitconfig.sh || 
        echo "No dev-util."
fi

[ "$TERM" != 'dumb'  ] && PS1='\[\033[1;32m\][$PROJECT_HOME_NAME]\[\033[00m\]:\w\$ '

init_github_info
