#!/bin/bash
###########
# install-gem
# install a ruby gem into a user's home directory
# https://gatorwiki.hostgator.com/Admin/Install-gem 
# http://git.hgfix.net/install-gem/install-gem
# Robert West <rwest@hostgator.com>
# Please submit all bug reports at bugs.hostgator.com
#
# (C) 2011 - HostGator.com, LLC
###########


PROGNAME=${0##*/}
PROGVERSION=0.1

RAILSENV=/root/bin/railsenv

echo "${PROGNAME} ${PROGVERSION}"

############################################################
# Commandline handling

usage()
{
  cat << EO
Usage: $PROGNAME [options]

  Install a gem into a user's home directory

  Options:
EO
  cat <<EO | column -s\& -t
    
    -h|--help & show this message
    -g|--gem & name of the gem to install (required)
    -v|--version & specify a version of the gem to install (optional)
    -u|--user & specify which user to install the gem for (required)
EO
}


SHORTOPTS="hg:v:u:"
LONGOPTS="help,gem:,version:,user:"

ARGS=$(getopt -s bash --options $SHORTOPTS --longoptions $LONGOPTS --name $PROGNAME -- "$@")

if [ $? -ne 0 ];then
  usage
  exit 2
fi

eval set -- "$ARGS"

for OPT; do
  case $OPT in
    -h|--help)
      usage
      exit 0
      ;;
    -u|--user)
      GEMUSER=$2
      shift
      shift
      ;;
    -g|--gem)
      GEMNAME=$2
      shift
      shift
      ;;
    -v|--version)
      GEMVERSION=$2
      shift
      shift
      ;;
    --)
      shift
      break
      ;;
  esac
done


if [ ! $GEMUSER ];then
  "--user or -u is required."
  exit 2
fi

if [ ! $GEMNAME ];then
  "--gem or -g is required."
  exit 2
fi



############################################################
# Make sure we can find the necessary bits

RUBY=$(which ruby)
GEM=$(which gem)

if [ ! $RUBY ];then
  "Can't find ruby!"
  exit 3
fi

if [ ! $GEM ];then
  "Can't find gem!"
  exit 3
fi

# Basic install command: gem install --config-file /home/rwest/.gemrc

# check for user's .gemrc - there must be a .gemrc since that's where we're 
# going to get the gem install directory
if [ ! -f /home/$GEMUSER/.gemrc ];then
  echo "$GEMUSER has no .gemrc file."
  if [ -f $RAILSENV ];then
    echo "Create one?"
    read -p "y/[n]?" CONFIRM
    echo
    if [[ ! $CONFIRM =~ ^[Yy]$ ]];then
      echo "Cannot install a user gem without a .gemrc"
      echo "Bye."
      exit 4
    fi
    $RAILSENV --user=$GEMUSER
    if [[ $? != 0 ]];then
      echo "railsenv failed to create a proper .gemrc for $GEMUSER"
      echo "Please create a .gemrc for the user and re-run this script."
      exit 4
    fi
  else
    echo "There is no railsenv on this server."
    echo "Please create a .gemrc for the user and re-run this script."
    exit 4
  fi
fi


############################################################
# Install the requested gem

if [ $GEMVERSION ];then
  $GEM install $GEMNAME --version=$GEMVERSION --config-file /home/$GEMUSER/.gemrc
  if [[ $? == 0 ]];then
    echo "$GEMNAME successfully installed."
  else
    echo "There was a problem installing $GEMNAME"
    exit 5
  fi
else
  $GEM install $GEMNAME --config-file /home/$GEMUSER/.gemrc
  if [[ $? == 0 ]];then
    echo "$GEMNAME successfully installed."
  else
    echo "There was a problem installing $GEMNAME"
    exit 5
  fi

fi


############################################################
# Fix up the file ownerships since we installed as root

# fetch the gemhome so we know where the gems are
# there should be a Yaml commandline tool
GEMHOME=$($GEM env --config-file /home/$GEMUSER/.gemrc | grep gemhome | awk '{print $4}' | sed 's/\"//g')


# This is about as careful as I can be
# This now checks each file to ensure the file is located in the user's homedir
if [[ $GEMHOME =~ "^/home/$GEMUSER/" ]];then
  echo "Fixing file ownership on newly installed gem."
  for GEMFILE in $(find $GEMHOME -user root);
  do
    if [[ $GEMFILE =~ "^/home/$GEMUSER/" ]];then
      chown $GEMUSER:$GEMUSER $GEMFILE
    fi
  done
  if [[ $(stat -c %U  $( echo $GEMHOME | sed "s/\/1.8//"  ) ) != $GEMUSER ]];then
    echo "Fixing owner on GEMHOME"
    chown $GEMUSER:$GEMUSER $( echo $GEMHOME | sed "s/\/1.8//" )
  fi
else
  echo "Cannot safely determine \$GEMHOME. Fix file permissions manually."
  exit 5
fi

