#!/usr/bin/env bash
#
# This script works with CentOS 6 or 7
# The CentOS 5 kernel is too old for go's liking, but this script works on CentOS 5

set -eu

trap 'echo FAIL' ERR

REDHAT_VERSION=($(head -n 1 /etc/redhat-release | \grep -Eo '[0-9]+'))

cd $(dirname ${BASH_SOURCE[0]})

if [[ ${REDHAT_VERSION[0]} == 5 ]]; then
  if ! rpm -q epel-release; then
    yum install -y epel-release
  fi
  yum install -y bison git make man which perl-Digest-SHA
else
  if [[ ${REDHAT_VERSION[0]} == 6 ]]; then
    rpm -q epel-release || yum install -y epel-release
    yum install -y bison golang make man which perl-Digest-SHA
    if which git > /dev/null 2>&1; then
      GIT_VERSION=($(git --version))
      IFS_OLD=${IFS}
      IFS=.
      GIT_VERSION=(${GIT_VERSION[2]})
      IFS=${IFS_OLD}
    else
      GIT_VERSION=(0 0 0)
    fi
    if ( [[ ${GIT_VERSION[0]} == 1 ]] && [[ ${GIT_VERSION[1]} < 8 ]] ) || [[ ${GIT_VERSION[0]} < 1 ]]; then
      mkdir -p git_build
      pushd git_build
        curl -L -O https://www.kernel.org/pub/software/scm/git/git-2.4.5.tar.xz
        yum install -y  curl-devel expat-devel gettext openssl-devel zlib-devel perl-Error perl-ExtUtils-MakeMaker emacs asciidoc xmlto gcc tar
        tar Jxvf git-*.tar.xz
        pushd git-*/
          make
          make install prefix=/usr/local
        popd
      popd
      rm -rf git_build
    fi
  else
    yum install -y bison git golang make man which perl-Digest-SHA
  fi
fi

if git rev-parse; then
  cd $(git rev-parse --show-toplevel)
elif [ -e bootstrap ];then
  cd ..
else
  cd /tmp
  [ -d git-lfs ] || git clone https://github.com/github/git-lfs
  cd git-lfs
fi

if [[ ${REDHAT_VERSION[0]} == 5 ]]; then
  if ! env PATH=${PATH}:`pwd`/go/bin which gofmt > /dev/null 2>&1; then
    yum install -y gcc glibc
    curl -L -O https://storage.googleapis.com/golang/go1.4.2.src.tar.gz
    tar -zxvf go1.4.2.src.tar.gz
    rm go1.4.2.src.tar.gz
    pushd ./go/src
      sed -i 's|.*--build-id.*||' ./cmd/go/build.go
      ./make.bash
      #This will have a few failures, but that is expected in CentOS 5
      ./run.bash | true
    popd
  fi
  #If go isn't in the path, then it's in the go dir, so add it to the path
  if ! which gofmt > /dev/null 2>&1; then
    export PATH=${PATH}:`pwd`/go/bin
  fi
fi

mkdir -p src/github.com/github
if [ ! -e "src/github.com/github/git-lfs" ]; then
  ln -s $(pwd) src/github.com/github/git-lfs
fi
GOPATH=`pwd` ./script/bootstrap

install -D bin/git-lfs /usr/local/bin
git lfs init

if which ruby > /dev/null 2>&1; then
  IFS_OLD=${IFS}
  IFS=.
  RUBY_VERSION=($(ruby -e "print RUBY_VERSION"))
  IFS=${IFS_OLD}
else
  RUBY_VERSION=(0 0 0) #Thanks -u!
fi

if [[ ${RUBY_VERSION[0]} < 2 ]]; then
  if [[ ${REDHAT_VERSION[0]} < 7 ]]; then
    yum install -y tar
    #if gpg isn't installed, everything actually works. if it is, the key must be added or rvm won't work
    which gpg2 > /dev/null 2>&1 && 
      gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
    which gpg > /dev/null 2>&1 &&
      gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
    set +e #For CentOS 5
    curl -L get.rvm.io | bash -s stable
    set +u
    source /usr/local/rvm/scripts/rvm
    rvm install ruby
    rvm use ruby
    set -ue
  elif grep -q ' 7' /etc/redhat-release; then
    yum install -y ruby ruby-devel
  fi
fi

gem install ronn
GOPATH=`pwd` ./script/man
install -D man/*.1 /usr/local/share/man/man1
git help lfs > /dev/null

echo SUCCESS
