#!/usr/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_KEEP=
OPT_RELEASE=21


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

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

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

Long Options:
  --keep                Same as -k
  --verbose             Same as -v
  --help                Same as -?
  --version             Same as -V

EOF

  exit ${_EXIT_VAL};
}

#
# PARSE COMMAND LINE
#

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

  set -- ${CMD_LINE}

  while [ $# -gt 0 ]
  do
    case "$1" in
      -k|--keep)
        OPT_KEEP=1
        ;;
      -v|--verbose)
        OPT_VERBOSE=1
        ;;
      -V|--version)
        version 0
        ;;
      --help)
        usage 0
        ;;
      --)
        shift
        break
        ;;
      -*)
        usage 1
        ;;
      *)
        break
    esac
    shift
  done
}


#
# MAIN
#

function main {
  DATE="$(date +%s)"

  _info "Checking for versions..."
  echo

  for F in $(list_config_available "anaconda firstboot kde-settings korora-backgrounds korora-release shared-mime-info")
  do
    load_config ${F} || continue

    x=${KP_NAME}
    if [ ${KP_NAME} == "korora-backgrounds" ]
    then
      x="heisenbug-backgrounds"
    elif [ ${KP_NAME} == "korora-release" ]
    then
      x="fedora-release"
    fi

    _info "${KP_NAME}"

    #mkdir -p tmp-${DATE}/$x/{stable,testing}
    #cd tmp-${DATE}/$x/stable

    F_URL=$(yumdownloader --urls --releasever ${OPT_RELEASE} --source --disablerepo=korora $x | grep "rpm$")
    F_FILE=${F_URL##*/}
    F_VERSION=$(echo ${F_FILE} | awk -F"$x-" '{ print $2 }' | sed -re 's/\.fc[0-9]+//' -e 's/\.src\.rpm//')

    FT_URL=$(yumdownloader --urls --releasever ${OPT_RELEASE} --source --enablerepo=updates-testing --disablerepo=korora --disablerepo=korora-testing $x | grep "rpm$")
    FT_FILE=${FT_URL##*/}
    FT_VERSION=$(echo ${FT_FILE} | awk -F"$x-" '{ print $2 }' | sed -re 's/\.fc[0-9]+//' -e 's/\.src\.rpm//')

    _info "  - ${KP_VERSION}-${KP_RELEASE} - Korora"
    _info "  - ${F_VERSION} - Fedora (stable)"
    _info "  - ${FT_VERSION} - Fedora (testing)"
    echo
  done

  if [ ${OPT_KEEP} ]
  then
    echo "Files kept at tmp-${DATE}/"
#  else
#    rm -Rf tmp-${DATE}
  fi
}
