#!/bin/sh

echo "\n"
echo "================================================================================"
echo ""
echo "Checking MongoDB"
echo ""
echo "================================================================================"

mongoLinux() {
  if type -p mongo; then
      echo found Mongo executable in PATH
      _mongo=mongo
  else
      echo "Mongo not found."
  fi

  if [[ "$_mongo" ]]; then
      version=$("$_mongo" --version 2>&1 | awk '{print $4}')
      echo version "$version"
      if [[ "$version" > "2.4.0" ]]; then
          echo "\nGood job, you have a recent version of Mongo."
      else
          echo "\nYou should probably updgrade to a later version of Mongo..."
      fi
  else
      echo "Do you wish to install MongoDB??"
      select yn in "Yes" "No"; do
          case $yn in
              Yes ) 
                  echo "[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1" >> /etc/yum.repos.d/10gen.repo
                  yum install -y mongo-10gen-2.4.1 mongo-10gen-server-2.4.1
                  echo "exclude=mongo-10gen,mongo-10gen-server" >> /etc/yum.conf
                  mkdir -p /data/db
                  mkdir -p /u01/apps/mongo
                  chown mongod:mongod /u01/apps/mongo
                  sed -i 's/dbpath=\/var\/lib\/mongo/dbpath=\/u01\/apps\/mongo/g' /etc/mongod.conf
                  echo "smallfiles = true" >> /etc/mongod.conf
                  service mongod start
                  chkconfig mongod on
                  echo "MongoDB is installed and is running. Open a new terminal and run 'mongo' to connect to test database."
                  break;;
              No ) 
                  echo "Skipping MongoDB install."
                  break;;
          esac
      done
  fi
}

mongoMac() {
  if type -p mongo; then
      echo found Mongo executable in PATH
      _mongo=mongo
  else
      echo "Mongo not found."
  fi

  if [[ "$_mongo" ]]; then
      version=$("$_mongo" --version 2>&1 | awk '{print $4}')
      echo version "$version"
      if [[ "$version" > "2.4.0" ]]; then
          echo "\nGood job, you have a recent version of Mongo."
      else
          echo "\nYou should probably updgrade to a later version of Mongo..."
      fi
  else
      echo "Do you wish to install MongoDB??"
      _mongostr=mongodb-osx-x86_64-2.4.1
      select yn in "Yes" "No"; do
          case $yn in
              Yes ) 
                  curl http://downloads.mongodb.org/osx/$_mongostr.tgz > /tmp/mongodb.tgz        
                  mkdir -p /usr/local
                  tar -C /usr/local -zxvf /tmp/mongodb.tgz
                  rm /tmp/mongodb.tgz
                  ln -s /usr/local/$_mongostr /usr/local/mongodb
                  mkdir -p /data/db
                  chown `id -u` /data/db
                  echo "/usr/local/mongodb/bin" > /etc/paths.d/mongodb 
                  echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\"
    \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
  <plist version=\"1.0\">
  <dict>
    <key>Label</key>
    <string>mongodb</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/mongodb/bin/mongod</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/usr/local/mongodb</string>
    <key>StandardErrorPath</key>
    <string>/var/log/mongodb/error.log</string>
    <key>StandardOutPath</key>
    <string>/var/log/mongodb/output.log</string>
  </dict>
  </plist>" >> /Library/LaunchDaemons/mongodb.plist
                  launchctl load /Library/LaunchDaemons/mongodb.plist
                  echo "MongoDB is installed and is running. Open a new terminal and run 'mongo' to connect to test database."
                  break;;
              No ) 
                  echo "Skipping MongoDB install."
                  break;;
          esac
      done
  fi  
}

#check OS
if [[ `uname` == 'Linux' ]]; then
    mongoLinux
elif [[ `uname` == 'Darwin' ]]; then
    mongoMac
fi