#!/bin/bash

### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash

if [ $(ymc_is_chroot) -ne 0 ]; then
  echo "WARNING: $0 can not be run inside a chroot..." 1>&2
  exit 0
fi

do_install=1
package_list_update_needed=0
apt_option_distribution=""


if [ $do_install -eq 1 ]; then
  ### Get all packages for this host
  packages_to_check=$(fliwi-get-my-packages)

  ### Get all packages for all services on this host
  packages_to_check=$packages_to_check' '$(fliwi-get-my-packages -s)

  ### By default we want a fresh list of packages...
  package_list_update_needed=1

  for package_info_to_check in $packages_to_check
  do
    apt_option_distribution=""
    package_to_check=$(echo $package_info_to_check | cut -d '/' -f 1)
    distribution_to_use=$(echo $package_info_to_check | cut -d '/' -f 2 --only-delimited)
    if [ -n "$distribution_to_use" ]; then
      apt_option_distribution="-t $distribution_to_use"
    fi

    ### Check if package is installed
    if [ -n "$package_to_check" ] && \
       [ "$(dpkg --get-selections "$package_to_check" | sed -r 's/[[:space:]]+/ /g')" = "$package_to_check install" ]; then
      echo "INFO: Package '$package_to_check' already installed..." 1>&2
      continue
    fi

    if [ $package_list_update_needed -ne 1 ] && \
       [ -n "$apt_conf_hash_value_pre_pkg_install" ] && \
       [ -n "$apt_conf_hash_value_post_pkg_install" ] && \
       [ "$apt_conf_hash_value_pre_pkg_install" != "$apt_conf_hash_value_post_pkg_install" ]; then
      echo "INFO: Advising package list update due to changed apt configuration..." 1>&2
      package_list_update_needed=1
    fi

    if [ $package_list_update_needed -ne 1 ]; then
      ### Check if apt knows the package
      filtered_list_of_possible_packages=$(apt-cache pkgnames $package_to_check 2>/dev/null)
      if [ $? -ne 0 ] || \
         [ $(ymc_contains $package_to_check $filtered_list_of_possible_packages) -eq 0 ]; then
        echo "INFO: Advising package list update since apt does not know the package..." 1>&2
        package_list_update_needed=1
      fi
    fi

    if [ $package_list_update_needed -eq 1 ]; then
        ### Always care about an updated package list before installing packages...
        echo "INFO: Updating package list using apt-get..." 1>&2
        apt_get_update_info="$(LC_ALL=C apt-get update -o Acquire::Languages='none' 2>&1)"
        apt_get_update_exit_code=$?

        if [ $(echo $apt_get_update_info | grep -c -F 'NO_PUBKEY') -gt 0 ]; then
          echo "INFO: Rewriting apt configuration using fliwi-update-apt-conf due to possibly missing apt-keys..." 1>&2
          fliwi-update-apt-conf
          if [ $? -ne 0 ]; then
            echo "ERROR: Failed to run '# fliwi-update-apt-conf' - stopping further operations..." 1>&2
            exit 1
          fi
          echo "INFO: Updating package list using apt-get, again..." 1>&2
          apt-get update -o Acquire::Languages='none' 1>/dev/null
          apt_get_update_exit_code=$?
        fi

        if [ $apt_get_update_exit_code -ne 0 ]; then
          echo "ERROR: Failed to run '# apt-get update' - stopping further operations..." 1>&2
          exit $apt_get_update_exit_code
        fi

        package_list_update_needed=0
    fi

    ## Get a hash value for the apt configuration previously to installing the new package
    apt_conf_hash_value_pre_pkg_install="$(find /etc/apt/ -type f -exec md5sum {} \; | md5sum -)"

    ## Install package
    echo "INFO: Installing package '$package_to_check'..." 1>&2
    DEBIAN_FRONTEND=noninteractive apt-get -y install $apt_option_distribution $package_to_check
    if [ $? -ne 0 ]; then
      echo "ERROR: Failed to install package '$package_to_check' - stopping further operations..." 1>&2
      exit 1
    fi

    ## Get a hash value for the apt configuration after to installing the new package
    apt_conf_hash_value_post_pkg_install="$(find /etc/apt/ -type f -exec md5sum {} \; | md5sum -)"
  done
fi

exit 0
