#!/bin/bash

# -----------------------------------------------------------------------------
#
# Bash functions
#
# -----------------------------------------------------------------------------

# Verify if a package is installed via yum
function is_installed {
  if [ $(dpkg -l | grep -c "^$@") -gt 0 ]; then
      echo "present" # package is present
  else
      echo "missing" # package is missing
  fi
}

function install_packages {
  for package in "$@"
  do
    installed=$(is_installed "${package}")

    if [ "$installed" == "missing" ]; then
      echo "Installing ${package}"

      apt-get install -qq -y --force-yes "${package}"
    fi
  done
}

function copy_file {
  source=$1
  destination=$2

  if [ ! -f ${destination} ]; then
    echo "Copying a new file from ${source} to ${destination}"

    cp ${source} ${destination}
  else
    source_md5=_$(md5sum ${source} | cut -d ' ' -f 1)
    destination_md5=_$(md5sum ${destination} | cut -d ' ' -f 1)

    if [ ${source_md5} != ${destination_md5} ]; then
      echo "Updating the ${destination}"

      cp ${source} ${destination}
    fi
  fi
}
