#!/bin/sh

# --------------------------
# list-packages.sh
# version 2.7.5.5
# --------------------------

# check for parameters

usage_str="Usage: ${0} [ --no-color ] [ --size ] [ --date ] [ --version ] [ --fname ]"
if [ "${1}" = "--help" ] || [ "${1}" = "--usage" ]; then
  printf "%s\n" "${usage_str}"
  exit 0
fi

DEFAULT_FORMAT_OPT="yes"
SIZE_OPT=""
DATE_OPT=""
VERSION_OPT=""
FNAME_OPT=""
NOCOLOR_OPT=""
if [ $# -gt 0 ]; then
  DEFAULT_FORMAT_OPT=""
  for arg in $@
  do
    is_option=`echo ${arg} | grep '\-\-'`
    if [ ! -z "${is_option}" ]; then
      [ $arg = "--size" ] && SIZE_OPT="yes" && continue
      [ $arg = "--date" ] && DATE_OPT="yes" && continue
      [ $arg = "--version" ] && VERSION_OPT="yes" && continue
      [ $arg = "--fname" ] && FNAME_OPT="yes" && continue
      [ $arg = "--no-color" ] && NOCOLOR_OPT="yes" && continue
      echo "${0}: unknown option '${arg}'"
    fi
  done
fi
if [ -z "${SIZE_OPT}" ] && [ -z "${DATE_OPT}" ] && [ -z "${VERSION_OPT}" ] && [ -z "${FNAME_OPT}" ]; then
  DEFAULT_FORMAT_OPT="yes"
fi

[ -z "${BUILD_FARM}" ] && BUILD_FARM="${HOME}/build-farm"
if [ ! -d "${BUILD_FARM}" ]; then
  echo "ERROR: base directory '${BUILD_FARM}' doesn't exist. Abort."
  exit 1
fi

[ -z "${PACKAGES_DIR}" ] && PACKAGES_DIR="PACKAGES"

if [ -z ${ARCH_NAME} ]; then
  binpkg_dir=`find "${BUILD_FARM}/${PACKAGES_DIR}" -name "binpkg-*" | head -1`
  ARCH_NAME=`basename "${binpkg_dir}" | sed 's/binpkg-//'`
fi

target_triplet=`gcc -v 2>&1 | grep "Target:" | awk '{print $2}' | tr '-' ' '`

[ -z ${LIBC_NAME} ] && LIBC_NAME=`echo "${target_triplet}" | awk '{print $4}'`
uclibc_ld_count=`ls /lib/ld-* 2>/dev/null | grep uClibc | wc -l`
[ $uclibc_ld_count -ne 0 ] && LIBC_NAME="uclibc"
[ -z ${LIBC_NAME} ] && LIBC_NAME="unknown"

if [ -z "${BINPACKAGES_DIR}" ]; then
  BINPACKAGES_DIR="${PACKAGES_DIR}"/binpkg-"${ARCH_NAME}"
  [ -d "${BUILD_FARM}/${BINPACKAGES_DIR}" ] || BINPACKAGES_DIR="${PACKAGES_DIR}"/binpkg
fi
if [ ! -d "${BUILD_FARM}/${BINPACKAGES_DIR}" ]; then
  echo "ERROR: binary packages directory '${BINPACKAGES_DIR}' doesn't exist. Abort."
  exit 1
fi
cd "${BUILD_FARM}/${BINPACKAGES_DIR}"

# locate packages
# [note: --printf option (for --printf="<%y> %s %n\n") isn't currently supported in busybox's stat]
list_of_packages=`find . -maxdepth 1 -type f -name "*.binpkg.*" -print0 | \
                  xargs -0 stat -c "<%y> %s %n" 2>/dev/null | sed '/^$/d'`
[ -z "${list_of_packages}" ] || \
    list_of_packages=`echo "${list_of_packages}" | grep -v ".list" | sort -r`
list_of_kernels=`find . -maxdepth 1 -type f -name "*.kernelpkg.*" -print0 | \
                 xargs -0 stat -c "<%y> %s %n" 2>/dev/null | sed '/^$/d'`
[ -z "${list_of_kernels}" ] || \
    list_of_kernels=`echo "${list_of_kernels}" | grep -v ".list" | sort -r`

full_list_of_pkgs=""
if [ ! -z "${list_of_packages}" ]; then
  full_list_of_pkgs="${list_of_packages}"
elif [ ! -z "${list_of_kernels}" ]; then
  full_list_of_pkgs="${list_of_kernels}"
fi
if [ ! -z "${list_of_kernels}" ] && [ ! -z "${list_of_packages}" ]; then
    full_list_of_pkgs=`printf "%s\n%s" "${list_of_kernels}" "${list_of_packages}"`
fi

if [ -z "${full_list_of_pkgs}" ]; then
  printf "'%s': no packages found\n" "${BUILD_FARM}/${BINPACKAGES_DIR}"
  _fmat_str="%s: %d\n"
  [ -z "${NOCOLOR_OPT}" ] && _fmat_str="%s: \033[1;47;1m\033[1m%d\033[0m\n"
  printf "${_fmat_str}" "Total number of packages" 0
  exit 0
fi

package_number=1

# check for installed bc binary
bc_binary_found="yes"
which bc >/dev/null 2>&1 || type bc >/dev/null 2>&1 || bc_binary_found=""

echo "${full_list_of_pkgs}" | while read the_package
do
  size_of_pkg=`echo "${the_package}" | cut -d'>' -f2 | cut -d' ' -f2`
  date_of_pkg=`echo "${the_package}" | cut -d'<' -f2 | cut -d'>' -f1 | cut -d' ' -f1`
  file_name_of_pkg=`echo "${the_package}" | cut -d'/' -f2`
  title_of_pkg=`echo "${file_name_of_pkg}" | cut -d'-' -f1`
  version_of_pkg=`echo "${file_name_of_pkg}" | cut -d'-' -f2- | \
                  sed -e 's/.tar//' -e 's/.gz//' -e 's/.bz2//' \
                      -e "s/-${ARCH_NAME}//" -e 's/-${LIBC_NAME}//' \
                      -e 's/.binpkg//' -e 's/.kernelpkg//' -e 's/v//' | \
                  sed 's/-uclibc//'`
  is_kernelpkg=`echo "${file_name_of_pkg}" | grep "\.kernelpkg\."`
  [ ! -z "${is_kernelpkg}" ] && title_of_pkg="kernel:${title_of_pkg}"
  
  # vip: very important package
  is_vip="no"
  if [ "${title_of_pkg}" = "uclibc" ] || \
       [ "${title_of_pkg}" = "gcc" ] || [ "${title_of_pkg}" = "gcc_snapshot" ] || \
         [ "${title_of_pkg}" = "binutils" ] || \
           [ ! -z "${is_kernelpkg}" ]; then
    is_vip="yes"
  fi
  
  printf "[#%.4d]  " $package_number
  
  if [ ! -z "${DEFAULT_FORMAT_OPT}" ]; then
    if [ ! -z ${bc_binary_found} ]; then
      pkg_kb_size=`echo scale=2 \; ${size_of_pkg}/1024 | bc`
      printf "%10.1f%s" "${pkg_kb_size}" " KiB"
    else
      printf "%10.d" "${size_of_pkg}"
    fi
    printf "  "
    printf "%s" "${date_of_pkg}"
    printf "  "
    _fmat_str="%s v%s (%s)"
    [ -z "${NOCOLOR_OPT}" ] && _fmat_str="\033[1;47;1m\033[1m%s\033[0m \033[1;32mv%s\033[0m (%s)"
    [ -z "${NOCOLOR_OPT}" ] && [ "${is_vip}" = "yes" ] && \
      _fmat_str="\033[37;41;1m\033[1m%s\033[0m \033[1;36mv%s\033[0m (%s)"
    printf "${_fmat_str}" "${title_of_pkg}" "${version_of_pkg}" "${file_name_of_pkg}"
  else
    if [ ! -z "${SIZE_OPT}" ]; then
      if [ ! -z ${bc_binary_found} ]; then
        pkg_kb_size=`echo scale=2 \; ${size_of_pkg}/1024 | bc`
        printf "%10.1f%s  " "${pkg_kb_size}" " KiB"
      else
        printf "%10.d  " "${size_of_pkg}"
      fi
    fi
    if [ ! -z "${DATE_OPT}" ]; then
      printf "%s  " "${date_of_pkg}"
    fi
    _fmat_str="%s "
    [ -z "${NOCOLOR_OPT}" ] && _fmat_str="\033[1;47;1m\033[1m%s\033[0m "
    [ -z "${NOCOLOR_OPT}" ] && [ "${is_vip}" = "yes" ] && _fmat_str="\033[37;41;1m\033[1m%s\033[0m "
    printf "${_fmat_str}" "${title_of_pkg}"
    if [ ! -z "${VERSION_OPT}" ]; then
      _fmat_str="v%s "
      [ -z "${NOCOLOR_OPT}" ] && _fmat_str="\033[1;32mv%s\033[0m "
      [ -z "${NOCOLOR_OPT}" ] && [ "${is_vip}" = "yes" ] && _fmat_str="\033[1;36mv%s\033[0m "
      printf "${_fmat_str}" "${version_of_pkg}" 
    fi
    if [ ! -z "${FNAME_OPT}" ]; then
      _fmat_str="(%s)"
      printf "${_fmat_str}" "${file_name_of_pkg}"
    fi
  fi
  
  printf "\n"
  package_number=`expr $package_number '+' 1`
done

_fmat_str="%s: %d\n"
[ -z "${NOCOLOR_OPT}" ] && _fmat_str="%s: \033[1;47;1m\033[1m%d\033[0m\n"
printf "${_fmat_str}" "Total number of packages" `echo "${full_list_of_pkgs}" | wc -l`
