#!/usr/bin/bash
#
# Copyright 2012-2013 "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/>.
#

VERSION="0.1.0"

#
# PRE-INIT
#
SUB_COMMAND=""
CONFIG_FILE=""
_CALLED=$(dirname "${0}")

# assume first parameter to be the subcommand
while [ $# -gt 0 ] && [ -z ${SUB_COMMAND} ]
do
  case "$1" in
    init|--init)
      SUB_COMMAND=init
      ;;
    list|--list)
      SUB_COMMAND=list
      ;;
    help|--help)
      SUB_COMMAND=help
      ;;
    build|--build)
      SUB_COMMAND=build
      ;;
    repository|--repository)
      SUB_COMMAND=repository
      ;;
    sync|--sync)
      SUB_COMMAND=sync
      ;;
    checkout|--checkout)
      SUB_COMMAND=checkout
      ;;
    release|--release)
      SUB_COMMAND=release
      ;;
    upstream|--upstream)
      SUB_COMMAND=upstream
      ;;
    --config*)
      if [ ${#1} -eq 8 ]
      then
        CONFIG_FILE=$2
        shift
      else
        CONFIG_FILE="${1/--config=/}"
      fi
      ;;
    *)
      echo "Unknown command specified: $1"
      exit 1
      ;;
  esac
  shift
done

# set to help if no sub command found
[ -z "${SUB_COMMAND}" ] && SUB_COMMAND=help

#
# DEFAULTS
#

[ -z ${LIB_DIR} ]     && LIB_DIR="/usr/share/lib/kp/lib"
[ -z ${CONF_DIR} ]    && CONF_DIR="/usr/share/lib/kp/conf"
[ -z ${WORKING_DIR} ] && WORKING_DIR="/var/lib/kp"
[ -z ${RELEASE_DIR} ] && RELEASE_DIR="/tmp"
[ -z ${LOG_DIR} ]     && LOG_DIR="/usr/share/lib/kp/log"

[[ -z ${CONFIG_FILE} ]] && [[ -f "${_CALLED}/kp.conf" ]] && CONFIG_FILE="${_CALLED}/kp.conf"

#
# INCLUDES
#

if [ ! -r "${CONFIG_FILE}" -a "${SUB_COMMAND}" != "init" ]
then
  echo "FATAL: No configuration file found."
  exit 1
else
  source "${CONFIG_FILE}"
fi

if [ ! -d "${LIB_DIR}" -a "${SUB_COMMAND}" != "init" ]
then
  echo "FATAL: Invalid LIB_DIR defined."
  exit 1
else
  source "${LIB_DIR}/kp_core"
fi

if [ ! -d "${LOG_DIR}" -a "${SUB_COMMAND}" != "init" ]
then
  echo "FATAL: Invalid LOG_DIR defined. Did you run --init?"
  exit 1
fi

# build working subdirectories
WORKING_CONFIG_DIR="${WORKING_DIR}/conf"
WORKING_PACKAGES_DIR="${WORKING_DIR}/packages"
WORKING_REPOSITORY_DIR="${WORKING_DIR}/repository"
WORKING_KICKSTART_DIR="${WORKING_DIR}/kickstart"
WORKING_RELEASE_DIR="${WORKING_DIR}/release"
WORKING_RELEASE_CACHE_DIR="${WORKING_DIR}/release/cache"
WORKING_RELEASE_TMP_DIR="${WORKING_DIR}/release/tmp"

SCRIPT=${0##*/}

if [ ! -d "${WORKING_DIR}" -a "${SUB_COMMAND}" != "init" ]
then
  _error "Working directory doesn't not exist. Have you run \"kp --init\"?"
  exit 1
fi


# variables based on config
# default to git:// for git protocol unless otherwise specified
GIT_URL="${GIT_URL_GIT}"

# if we're using a developer account we can't use git://
if [ -n "${KP_DEV_ACCOUNT}" -a "${GIT_PROTOCOL}" == "git" ]
then
    _error "Developers cannot use git://, set GIT_PROTOCOL to either 'ssh' or 'http' in your config"
    exit 1
# if we're anonymous we can't use ssh://
elif [ -z "${KP_DEV_ACCOUNT}" -a "${GIT_PROTOCOL}" == "ssh" ]
then
    _error "SSH access is only available to developers, set GIT_PROTOCOL to either 'git' or 'http' in your config"
    exit 1
fi

case "${GIT_PROTOCOL}" in
  git)
    GIT_URL="${GIT_URL_GIT}"
    ;;
  http|https)
    GIT_PROTOCOL="https"
    GIT_URL="${GIT_URL_HTTP}"
    ;;
  ssh)
    GIT_URL="${GIT_URL_SSH}"
    ;;
  *)
    GIT_PROTOCOL="git"
    GIT_URL="${GIT_URL_GIT}"
    if [ "${SUB_COMMAND}" == "checkout" ]
    then
      _warn "Unknown protocol specified, falling back to default of git://"
    fi
    ;;
esac


#
# INCLUDES
#


CALL_PATH=$(dirname $0)

source "${LIB_DIR}/kp_${SUB_COMMAND}" $@

NOW=$(date +'%s')
NOW_LITERAL=$(date '+%Y-%m-%d %H:%M:%S')

LOG_FILE="${LOG_DIR}/kp-${SUB_COMMAND}-${NOW}.log"
if [ "${SUB_COMMAND}" != "init" ]
then
  touch $LOG_FILE
  _log "Logging started: ${NOW_LITERAL}"
fi

#
# GLOBAL DEPENDANCY CHECKS
#


#
# MAIN
#

# start the main function sourced above
parse_args $@

main
