<%=
salt_install = config[:salt_install]
salt_url = config[:salt_bootstrap_url]
salt_version = config[:salt_version]
bootstrap_options = config[:salt_bootstrap_options] % [salt_version]
salt_apt_repo = config[:salt_apt_repo]
salt_apt_repo_key = config[:salt_apt_repo_key]
salt_ppa = config[:salt_ppa]
salt_yum_rpm_key = config[:salt_yum_rpm_key] % [salt_version]
salt_yum_repo = config[:salt_yum_repo] % [salt_version]
salt_yum_repo_key = config[:salt_yum_repo_key] % [salt_version]
salt_yum_repo_latest = config[:salt_yum_repo_latest]
salt_pip_pkg = config[:pip_install]
salt_pip_bin = config[:pip_bin]
salt_pip_install_command = ' install'
salt_pip_install_command += ' -e' if config[:pip_editable]
salt_pip_install_command += ' --index-url ' + config[:pip_index_url] if config[:pip_index_url]
config[:pip_extra_index_url].each do |url|
  salt_pip_install_command += ' --extra-index-url ' + url
end

<<-INSTALL
sh -c '
#{Util.shell_helpers}

# what version of salt is installed?
command -v #{salt_call} >/dev/null 2>&1 && SALT_VERSION=$(#{salt_call} --version|cut -d " " -f 2)
command -v locale-gen >/dev/null 2>&1 && #{sudo('locale-gen')} en_US.UTF-8
set +x
export DEBIAN_FRONTEND=noninteractive

if [ -z "${SALT_VERSION}" -a "#{salt_install}" = "bootstrap" ]
then
  mkdir -p "#{config[:root_path]}"
  BOOTSTRAP="#{config[:root_path]}/bootstrap.sh"
  if ! test -f $BOOTSTRAP; then
      do_download #{salt_url} "$BOOTSTRAP"
  fi
  #{sudo('sh')} $BOOTSTRAP #{bootstrap_options}
elif [ -z "${SALT_VERSION}" -a "#{salt_install}" = "pip" ]
then
  echo "Make sure setuptools and pip are new enough"
  export REQUIREMENTS_FILE=$(mktemp)
  echo "setuptools >=30,<54.* ; python_version < '3.6'" > $REQUIREMENTS_FILE
  echo "setuptools >=30,!=50.*,!=51.*,!=52.*; python_version >= '3.6'" > $REQUIREMENTS_FILE
  echo "pip >=9,<21.0.0 ; python_version < '3.6'" >> $REQUIREMENTS_FILE
  echo "pip >=9 ; python_version >= '3.6'" >> $REQUIREMENTS_FILE
  #{sudo(salt_pip_bin)} install -r $REQUIREMENTS_FILE
  echo #{sudo(salt_pip_bin)} #{salt_pip_install_command} #{salt_pip_pkg}
  #{sudo(salt_pip_bin)} #{salt_pip_install_command} #{salt_pip_pkg}
elif [ -z "${SALT_VERSION}" -a "#{salt_install}" = "apt" ]
then
  if [ ! $(command -v lsb_release &>/dev/null) ]; then
    . /etc/lsb-release
  else
    DISTRIB_CODENAME=$(lsb_release -s -c)
  fi

  echo "-----> Install apt-transport-https"
  #{sudo('apt-get')} update
  #{sudo('apt-get')} install -y apt-transport-https gnupg

  echo "-----> Configuring apt repo for salt #{salt_version}"
  echo "deb #{salt_apt_repo}/#{salt_version} ${DISTRIB_CODENAME} main" | #{sudo('tee')} /etc/apt/sources.list.d/salt-#{salt_version}.list

  do_download #{salt_apt_repo_key} /tmp/repo.key
  #{sudo('apt-key')} add /tmp/repo.key

  #{sudo('apt-get')} update
  sleep 10
  echo "-----> Installing salt-minion (#{salt_version})"
  #{sudo('apt-get')} install -y salt-minion salt-common salt-master
elif [ -z "${SALT_VERSION}" -a "#{salt_install}" = "distrib" ]
then
  #{sudo('apt-get')} update
  #{sudo('apt-get')} install -y salt-minion salt-master
elif [ -z "${SALT_VERSION}" -a "#{salt_install}" = "ppa" ]
then
  #{sudo('apt-add-repository')} -y #{salt_ppa}
  #{sudo('apt-get')} update
  #{sudo('apt-get')} install -y salt-minion salt-common salt-master
elif [ -z "${SALT_VERSION}" -a "#{salt_install}" = "yum" ]
then
  if [ -z "#{salt_version}" ]
  then
    echo "-----> Installing yum repo for salt latest"
    #{sudo('yum')} install #{salt_yum_repo_latest}
  else
    echo "-----> Installing yum repo for salt #{salt_version}"
    #{sudo('rpm')} --import '#{salt_yum_rpm_key}'
    #{sudo('tee')} /etc/yum.repos.d/saltstack.repo <<EOL
[saltstack-repo]
name=SaltStack repo for RHEL/CentOS $releasever
baseurl=#{Shellwords.escape(salt_yum_repo)}
enabled=1
gpgcheck=1
gpgkey=#{Shellwords.escape(salt_yum_repo_key)}
EOL
  fi
  #{sudo('yum')} clean expire-cache
  echo "-----> Installing salt-minion (#{salt_version})"
  #{sudo('yum')} install -y salt-minion salt-common salt-master
fi

# check again, now that an install of some form should have happened
command -v #{salt_call} >/dev/null 2>&1 && FULL_SALT_VERSION=$(#{salt_call} --version|cut -d " " -f 2)

# extract short format of Salt version
if [ ! -z "${FULL_SALT_VERSION}" ]
then
  YEAR=$(echo "$FULL_SALT_VERSION" | cut -d "." -f1)
  MONTH=$(echo "$FULL_SALT_VERSION" | cut -d "." -f2)
  SALT_VERSION="${YEAR}.${MONTH}"
fi

if [ -z "${SALT_VERSION}" ]
then
  echo "No salt-minion installed, install must have failed!!"
  echo "salt_install = #{salt_install}"
  echo "salt_url = #{salt_url}"
  echo "bootstrap_options = #{bootstrap_options}"
  echo "salt_version = #{salt_version}"
  echo "salt_apt_repo = #{salt_apt_repo}"
  echo "salt_apt_repo_key = #{salt_apt_repo_key}"
  echo "salt_ppa = #{salt_ppa}"
  echo "salt_yum_rpm_key = #{salt_yum_rpm_key}"
  echo "salt_yum_repo = #{salt_yum_repo}"
  echo "salt_yum_repo_key = #{salt_yum_repo_key}"
  echo "salt_yum_repo_latest = #{salt_yum_repo_latest}"
  exit 2
elif [ "${SALT_VERSION}" = "#{salt_version}" -o "#{salt_version}" = "latest" -o "#{salt_version}" = "${FULL_SALT_VERSION}" ]
then
  echo "You asked for #{salt_version} and you have ${FULL_SALT_VERSION} installed, sweet!"
elif [ ! -z "${SALT_VERSION}" -a "#{salt_install}" = "bootstrap" ]
then
  echo "You asked for bootstrap install and you have got ${SALT_VERSION}, hope thats ok!"
else
  echo "You asked for #{salt_version} and you have got ${SALT_VERSION} installed, dunno how to fix that, sorry!"
  exit 2
fi

#{install_chef}'
INSTALL
%>
