#!/usr/bin/env bash

# Copyright (c) 2012 Dickson S. Guedes.
#
# This module is free software; you can redistribute it and/or modify it under
# the [PostgreSQL License](http://www.opensource.org/licenses/postgresql).
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose, without fee, and without a written agreement is
# hereby granted, provided that the above copyright notice and this paragraph
# and the following two paragraphs appear in all copies.
#
# In no event shall Dickson S. Guedes be liable to any party for direct,
# indirect, special, incidental, or consequential damages, including lost
# profits, arising out of the use of this software and its documentation, even
# if Dickson S. Guedes has been advised of the possibility of such damage.
#
# Dickson S. Guedes specifically disclaims any warranties, including, but not
# limited to, the implied warranties of merchantability and fitness for a
# particular purpose. The software provided hereunder is on an "as is" basis,
# and Dickson S. Guedes has no obligations to provide maintenance, support,
# updates, enhancements, or modifications.

ACTUAL_DIR=`pwd`
STABLE_VERSION="tags/v0.2.1"

clone_repo()
{
  echo
  echo -n "wait while the installer fetches pgvm's files ..."
  git clone git://github.com/guedes/pgvm.git $pgvm_home > /dev/null 2>&1 || {
    echo "sorry, pgvm could not be installed" && exit 1
  }

  cd $pgvm_home && git checkout $STABLE_VERSION > /dev/null 2>&1 && echo " done" || {
    echo "sorry, your git could not checkout from '$STABLE_VERSION'" && exit 1
  }

}

copy_repo()
{
  from_dir=${1}
  cp -a $from_dir $pgvm_home
}

update_repo()
{
	cd $pgvm_home
	git checkout $STABLE_VERSION > /dev/null 2>&1 && \
      git pull origin $STABLE_VERSION > /dev/null 2>&1 || \
        echo "sorry, pgvm could not checkout from '$STABLE_VERSION'"
}

force_clone_repo()
{
  echo "DEPRECATED! Please use --update instead"
}

set_bashrc_env()
{
  [[ -f $HOME/.bashrc ]] || touch $HOME/.bashrc

  egrep "source .*pgvm_env.*" $HOME/.bashrc >/dev/null \
    || echo "source ${pgvm_home}/pgvm_env" >> $HOME/.bashrc
}

set_bash_profile_env()
{
  [[ -f $HOME/.bash_profile ]] || touch $HOME/.bash_profile

  egrep "source .*pgvm_env.*" $HOME/.bash_profile >/dev/null \
    || echo "source ${pgvm_home}/pgvm_env" >> $HOME/.bash_profile
}

set_bash_env()
{
  echo -n "setting up the environment ..."

  cat > ${pgvm_home}/pgvm_env <<EOF
pgvm_home=${pgvm_home}
pgvm_src=${pgvm_src}
pgvm_logs=${pgvm_logs}
pgvm_clusters=${pgvm_clusters}
pgvm_environments=${pgvm_environments}

export pgvm_home pgvm_src pgvm_logs pgvm_environments pgvm_clusters

export PATH=\${pgvm_home}/bin:\$PATH
export PATH=\${pgvm_environments}/current/bin:\$PATH
EOF

  case $OSTYPE in
    darwin*) set_bash_profile_env ;;
          *) set_bashrc_env ;;
  esac

  echo "done"; echo
  sleep 1
  echo "now you could open another terminal and type: pgvm help"
}

# stolen from helpers
parse_options()
{
  for opt in $*
  do
    opt=$(echo $opt | sed 's/--/opt_/g' | sed 's/-/_/g')
    value=$(echo $opt | cut -s -d'=' -f2)
    opt=$(echo $opt | cut -d'=' -f1)
    value=${value:-1}
    eval "export $opt=$value"
  done
}

parse_options $*

pgvm_home=${opt_pgvm_home:-"$HOME/.pgvm"}
pgvm_src=${opt_pgvm_src:-"\${pgvm_home}/src"}
pgvm_logs=${opt_logs_dir:-"\${pgvm_home}/logs"}
pgvm_clusters=${opt_clusters_dir:-"\${pgvm_home}/clusters"}
pgvm_environments=${opt_environments_dir:-"\${pgvm_home}/environments"}

echo "PostgreSQL version manager's installer"

if [ -d $pgvm_home ]
then
	if [ "x$1" == "x--force" ]
	then
		force_clone_repo
	elif [ "x$1" == "x--update" ]
	then
		update_repo
	else
		echo "ERROR: pgvm already installed! Use --force to overwrite or --update to update"
		exit 1
	fi
else
	if [ -n "$opt_from_dir" ]
	then
		copy_repo $opt_from_dir
	else
		clone_repo
	fi
fi

set_bash_env