#!/usr/bin/env bash

set -e # bail on errors

fail() {
  echo "error: $@"
  exit 1
}

function command_exists() {
  which "$1" >/dev/null 2>/dev/null
}

install() {

  if command_exists "$1"; then
    return
  fi

  # apt (debian / ubuntu)
  if command_exists aptitude; then
    sudo aptitude install -y "$1"

  # homebrew (os x)
  elif command_exists brew; then
    brew install "$1"

  # rpm (redhat / centos / fedora)
  elif command_exists yum; then
    sudo yum install -y "$1"

  # arch
  elif command_exists pacman; then
    sudo pacman -S "$1"

  else
    fail "Don't know how to install $1 on this box. Install $1 and run this again."
  fi

}

install zsh || fail "Failed to install zsh."
install git || fail "Failed to install git."

cd ~
if ! [[ -d config ]]; then
  git clone git://github.com/samsonjs/config || fail "cannot clone config repo"
fi

cd config
./init.sh
cd ..

echo " * Done!"

# FIXME figure out how to change the shell semi-interactively (only type in password)
if ! grep `id -u` /etc/passwd | grep zsh; then
  #chsh -s `which zsh`
  echo " *** Use chsh to change your shell to `which zsh`"
fi

