#!/bin/bash
#
# Copyright 2012-2014 "Korora Project" <dev@kororaproject.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

#
# INTIALISE
#
OPT_VERBOSE=
OPT_CHECKOUT_ONLY=
OPT_INCLUDE_DEPRECATED=


#
# FUNCTIONS
#
function usage {
  _EXIT_VAL=${1:-0}

  cat << EOF
Usage: $0 list [-vc]

Options:
  -c   Show checked out packages only
  -v   Show verbose output
  -?   Show this usage
  -V   Show version

Long Options:
  --checkout            Same as -c
  --include-deprecated  Include deprecated packages
  --verbose             Same as -v
  --help                Same as -?
  --version             Same as -V

Package status codes:
  -    Available but not checked out
  S    Synchronised with upstream
  M    Local modifications

Package upstream status codes:
       Package is not checked out
  -    Upsream available but not checkout out.
  S    Synchronised with upstream
  M    Local modifications

EOF

  exit ${_EXIT_VAL};
}

#
# PARSE COMMAND LINE
#

function parse_args {
  CMD_LINE=$(getopt -n$0 -u --longoptions="checkout include-deprecated verbose version help" "c v V ?" $@)
  [ ${?} -ne 0 ] && usage 1

  set -- ${CMD_LINE}

  while [ $# -gt 0 ]
  do
    case "$1" in
      -c|--checkout)
        OPT_CHECKOUT_ONLY=1
        ;;
      --include-deprecated)
        OPT_INCLUDE_DEPRECATED=1
        ;;
      -v|--verbose)
        OPT_VERBOSE=1
        ;;
      -V|--version)
        version 0
        ;;
      --help)
        usage 0
        ;;
      --)
        shift
        break
        ;;
      -*)
        usage 1
        ;;
      *)
        break
    esac
    shift
  done

  # remaining arguments are packages
  PACKAGES=""
  if [ $# -eq 0 ]
  then
    echo "Listing all packages/kickstarts ..."
  else
    while [ $# -gt 0 ]
    do
      PACKAGES="${PACKAGES} $1"
      shift
    done
  fi
}


#
# MAIN
#

function main {
  list_config_validate "${PACKAGES}"
  if [ ${?} -gt 0 ]
  then
    _pushd "${WORKING_CONFIG_DIR}"
    _CURRENT_BRANCH=$(_git_branch_active)
    _popd

    _info "Active configuration branch: ${_CURRENT_BRANCH}"

    echo

    _info "Packages:"

    # load the configuration
    for F in $(list_config_available "${PACKAGES}")
    do
      load_config ${F} || continue

      # check for package deprecation, skip unless we're including them
      if [ "${KP_DEPRECATED}" == "yes" -a "${OPT_INCLUDE_DEPRECATED}" != "1" ]
      then
        _debug "Skipping deprecated package: ${KP_NAME}"
        continue
      fi

      get_sync_status || continue

      [ -n "${OPT_CHECKOUT_ONLY}" -a "${KP_PACKAGE_SYNC_STATE}" == "-" ] && continue

      # preserve whitespace in our output
      IFS="%"

      if [ -z ${OPT_VERBOSE} ]
      then
        _info $(printf " %s%s  %-32s\n" "${KP_PACKAGE_SYNC_STATE}" "${KP_UPSTREAM_SYNC_STATE}" "${KP_NAME}")
      else
        _info $(printf " %s%s  %-32s\t%-16s\t%-6s\t%s\n" "${KP_PACKAGE_SYNC_STATE}" "${KP_UPSTREAM_SYNC_STATE}" "${KP_NAME}" "${KP_VERSION}-${KP_RELEASE}" "${KP_BRANCH}" "${KP_URL}")
      fi

      # reset the input field seperator
      unset IFS
    done

    echo
  fi

  list_kickstart_validate "${PACKAGES}"
  if [ ${?} -gt 0 ]
  then

    _info "Releases:"
    for L in $(list_kickstart_available ${PACKAGES})
    do
      _info " -   ${L}"
    done
  fi
}
