#!/opt/angry-omnibus-toolchain/bin/bash
#
# Copyright:: Copyright 2017 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# This script is based off of https://gist.github.com/cosimo/3760587

set -eou pipefail

function usage() {
  cat <<DOC
Usage: ${0##*/} [-P PRODUCT] [-v VERSION] [-c CHANNEL] [-d DOWNLOAD_DIR] -h
Download VERSION of PRODUCT from CHANNEL to DOWNLOAD_DIR and install it.
OPTIONS:
  -h               Show this message.
  -P PRODUCT       The name of the product you wish to install. (required)
  -v VERSION       Which version of the product you wish to install. (default: latest)
  -c CHANNEL       The channel from which you wish to install the product. (default: stable)
  -d DOWNLOAD_DIR  The directory to which you wish to download the product package. (default: PWD)
  -i INSTALL_DIR   The directory to which the product package is installed. (default: PWD)
DOC
}

channel="stable"
download_dir="$(pwd)"
install_dir=""
product_name=""
product_version="latest"

while getopts ":c:d:i:P:v:h" opt
do
  case "$opt" in
    c)
      channel="$OPTARG"
      ;;
    d)
      download_dir="$OPTARG"
      ;;
    i)
      install_dir="$OPTARG"
      ;;
    P)
      product_name="$OPTARG"
      install_dir=${install_dir:-$product_name}
      ;;
    v)
      product_version="$OPTARG"
      ;;
    h)
      usage
      exit 0
      ;;
    \?)
      usage
      exit 1
      ;;
    *)
      ;;
  esac
done
shift $((OPTIND -1))

# Exit if product_name is not set
product_name=${product_name:?A product name must be specified}

echo "--- Installing $channel $product_name $product_version"
download_url="$(/opt/angry-omnibus-toolchain/embedded/bin/mixlib-install download --url --channel "$channel" "$product_name" --version "$product_version")"

echo "Removing any existing install of $product_name"

FILE_TYPE="${download_url##*.}"
case "$FILE_TYPE" in
  "bff")
    sudo installp -u "$product_name" || true
    ;;
  "deb")
    sudo dpkg -P "$product_name" || true
    ;;
  "dmg")
    sudo rm -rf "$install_dir"
    sudo pkgutil --forget "com.getchef.pkg.$product_name" || true
    ;;
  "pkg")
    sudo rm -rf "$install_dir"
    sudo pkgutil --forget "com.getchef.pkg.$product_name" || true
    ;;
  "p5p")
    sudo pkg uninstall "$product_name" || true
    ;;
  "rpm")
    sudo rpm -ev "$product_name" || true
    ;;
  "sh")
    # intentionally left blank
    ;;
  "solaris")
    if sudo pkgrm -a /var/sadm/install/admin/auto-install -n "$product_name"; then
      :
    else
      echo "WARNING: CLEANING UP BROKEN POSTRM FROM PRIOR INSTALLATION!!!!!!!!!!!!!!!!!!!!!!"
      sudo rm -f "/var/sadm/pkg/$product_name/install/postremove" || true
      sudo pkgrm -a /var/sadm/install/admin/auto-install -n "$product_name" || true
    fi
    ;;
esac

sudo rm -rf "$install_dir"

/opt/angry-omnibus-toolchain/embedded/bin/mixlib-install install-script | sudo bash -s -- -P "$product_name" -d "$download_dir" -l "$download_url" 2>&1

package_file="${download_url##*/}"
echo "${download_dir%/}/$package_file"
