#!/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_USB=
OPT_ISO=1
OPT_CACHE_ONLY=

OPT_RELEASE_VERSION=27
OPT_RELEASE_MINOR_VERSION=
OPT_RELEASE_CODENAME="Squirt"
OPT_RELEASE_BASEARCH="x86_64"
OPT_RELEASE_TITLE="Korora"
OPT_RELEASE_DESKTOP=
OPT_RELEASE_DIR=
OPT_RELEASE_BETA=

#
# PRIVATE FUNCTIONS
#

function _template_inflate {
  _DST_FILE=${1}
  _SRC_FILE=${2}

  if [ -r ${_SRC_FILE} ]
  then
    cp "${_SRC_FILE}" "${_DST_FILE}"
  fi

  # substitute variables
  sed -i -e "s@%%KP_KICKSTART_DIR%%@${WORKING_DIR}/conf/kickstart.d@g" \
         -e "s@%%KP_REPOSITORY%%@file://${WORKING_DIR}/repository@g" \
         -e "s@\$basearch@${OPT_RELEASE_BASEARCH}@g" \
         -e "s@\$releasever@${OPT_RELEASE_VERSION}@g" \
         -e "s@%%KP_BASEARCH%%@${OPT_RELEASE_BASEARCH}@g" \
         -e "s@%%KP_VERSION%%@${OPT_RELEASE_VERSION}@g" ${_DST_FILE}

  # check for includes in our template
  grep "^%include" ${_DST_FILE} | while read I F
  do
    _FI="/tmp/$(basename ${F}).${RANDOM}"

    # replace include with our inflated version
    sed -i "s@$F@${_FI}@" ${_DST_FILE}

    # ensure includes are inflated
    _template_inflate "${_FI}" "${F}"
  done
}

#
# FUNCTIONS
#

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

  cat << EOF
Usage: $0 release [options] [release ... releaseN]

Options:
  -b   Set this as a beta release
  -t   Set the release title
  -r   Set the release version
  -m   Set the minor release version
  -n   Set the release codename
  -a   Set the release architecture
  -d   Set the desktop (e.g. GNOME)
  -C   Use cached packages only
  -v   Show verbose output
  -?   Show this usage
  -V   Show version

Long Options:
  --release-title     Same as -t
  --release-version   Same as -r
  --release-codename  Same as -n
  --release-arch      Same as -a
  --desktop           Same as -d
  --cache-only        Same as -C
  --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="beta release-title: release-version: minor-version: release-codename: release-arch: desktop: verbose cache-only version help" "b t: r: m: n: a: d: C l v V ?" $@)
  [ ${?} -ne 0 ] && usage 1

  set -- ${CMD_LINE}

  while [ $# -gt 0 ]
  do
    case "$1" in
      -b|--beta)
        OPT_RELEASE_BETA=" Beta"
        shift
        ;;
      -t|--release-title)
        OPT_RELEASE_TITLE="$2"
        echo $2 $3
        shift
        ;;
      -r|--release-version)
        OPT_RELEASE_VERSION="$2"
        shift
        ;;
      -m|--minor-version)
        OPT_RELEASE_MINOR_VERSION="$2"
        shift
        ;;
      -n|--release-codename)
        OPT_RELEASE_CODENAME="$2"
        shift
        ;;
      -a|--release-arch)
        OPT_RELEASE_BASEARCH="$2"
        shift
        ;;
      -d|--desktop)
        OPT_RELEASE_DESKTOP="$2"
        shift
        ;;
      -D)
        OPT_RELEASE_DIR="$2"
        shift
        ;;
      -C|--cache-only)
        OPT_CACHE_ONLY=1
        ;;
      -v|--verbose)
        OPT_VERBOSE=1
        ;;
      -V|--version)
        version 0
        ;;
      --help)
        usage 0
        ;;
      --)
        shift
        break
        ;;
      -*)
        usage 1
        ;;
      *)
        break
    esac
    shift
  done

  # remaining arguments are releases
  RELEASES=
  if [ $# -eq 0 ]
  then
    echo "Building all releases ..."
  else
    while [ $# -gt 0 ]
    do
      RELEASES="${RELEASES} $1"
      shift
    done
  fi
}


#
# MAIN
#

function main {
  # local dependency checks
  _LIVECD_TOOLS=$(livecd-creator --help 2>/dev/null)
  if [ ${?} -ne 0 ]
  then
    _error "Can't find livecd-creator. Is it installed?\nTry dnf install livecd-tools"
    exit 1
  fi

  # must be root to run
  if [ "$(id -u)" -ne 0 ]
  then
    _error "You must be root to build a release ISO."
    exit 1
  fi

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

    _OPTS=

    [ ! -z ${OPT_VERBOSE} ] && _OPTS="${_OPTS} --verbose"
    [ ! -z ${OPT_CACHE_ONLY} ] && _OPTS="${_OPTS} --cache-only"

    # build all requested releases
    for R in $(list_kickstart_available "${RELEASES}")
    do

      _FILE="korora-${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION}${OPT_RELEASE_BETA/\ B/-b}-${OPT_RELEASE_BASEARCH}-${R/korora-/}"

      # auto-detect desktop name if not set
      if [ -z "${OPT_RELEASE_DESKTOP}" ]
      then
        DESKTOP=${R/korora-/}
        OPT_RELEASE_DESKTOP=${DESKTOP^^}
      fi

      #
      _info "Preparing to build: $R"
      _info "  - Title:        ${OPT_RELEASE_TITLE}"
      _info "  - Codename:     ${OPT_RELEASE_CODENAME}"
      _info "  - Version:      ${OPT_RELEASE_VERSION}"
      _info "  - Architecture: ${OPT_RELEASE_BASEARCH}"
      _info "  - Desktop:      ${OPT_RELEASE_DESKTOP}"
      _info "  - FS Label:     ${_FILE}"


      # create working directory
      _RANDOM=${RANDOM}
      _TEMPLATE_FLATTENED="/tmp/${R}-flattened.ks.${_RANDOM}"
      _TEMPLATE="/tmp/${R}.ks.${_RANDOM}"

      _info "Preparing release template: ${_TEMPLATE}"

      pushd "${WORKING_DIR}/conf/kickstart.d/"

      # flattening template
      _info "Flattening template as: ${_TEMPLATE_FLATTENED}"
      ksflatten -c "${R}.ks" -o "${_TEMPLATE_FLATTENED}"

      _info "Inflating template as: ${_TEMPLATE}"
      _template_inflate "${_TEMPLATE}" "${_TEMPLATE_FLATTENED}"
      popd

      # create the CD
      _info "Creating release ..."
      echo setarch="$OPT_RELEASE_BASEARCH" livecd-creator ${_OPTS} --config="${_TEMPLATE}" --fslabel="${_FILE}" --title="${OPT_RELEASE_TITLE} ${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION}${OPT_RELEASE_BETA} (${OPT_RELEASE_CODENAME})" --product="${OPT_RELEASE_TITLE} ${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION} ${OPT_RELEASE_BASEARCH} ${OPT_RELEASE_DESKTOP}" --cache="${WORKING_RELEASE_CACHE_DIR}" --tmpdir="${WORKING_RELEASE_TMP_DIR}" --logfile="${LOG_FILE}"
      setarch="$OPT_RELEASE_BASEARCH" livecd-creator ${_OPTS} --config="${_TEMPLATE}" --fslabel="${_FILE}" --title="${OPT_RELEASE_TITLE} ${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION}${OPT_RELEASE_BETA} (${OPT_RELEASE_CODENAME})" --product="${OPT_RELEASE_TITLE} ${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION} ${OPT_RELEASE_BASEARCH} ${OPT_RELEASE_DESKTOP}" --cache="${WORKING_RELEASE_CACHE_DIR}" --tmpdir="${WORKING_RELEASE_TMP_DIR}" --logfile="${LOG_FILE}"

#      echo livemedia-creator --ks="${_TEMPLATE}" --logfile="${LOG_FILE}" --no-virt --resultdir="${WORKING_RELEASE_TMP_DIR}/../iso/${_FILE}" --project="${OPT_RELEASE_TITLE} ${OPT_RELEASE_BASEARCH} ${OPT_RELEASE_DESKTOP}${OPT_RELEASE_BETA}" --make-iso --volid "${OPT_RELEASE_TITLE,,}-${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION}${OPT_RELEASE_BETA/\ B/-b}-${OPT_RELEASE_BASEARCH,,}-${OPT_RELEASE_DESKTOP,,}" --iso-only --iso-name "${_FILE}.iso" --releasever "${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION}" --title="${OPT_RELEASE_TITLE} ${OPT_RELEASE_BASEARCH} ${OPT_RELEASE_DESKTOP}${OPT_RELEASE_BETA}" --macboot
#      livemedia-creator --ks="${_TEMPLATE}" --logfile="${LOG_FILE}" --no-virt --resultdir="${WORKING_RELEASE_TMP_DIR}/../iso/${_FILE}" --project="${OPT_RELEASE_TITLE} ${OPT_RELEASE_BASEARCH} ${OPT_RELEASE_DESKTOP}${OPT_RELEASE_BETA}" --make-iso --volid "${OPT_RELEASE_TITLE,,}-${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION}${OPT_RELEASE_BETA/\ B/-b}-${OPT_RELEASE_BASEARCH,,}-${OPT_RELEASE_DESKTOP,,}" --iso-only --iso-name "${_FILE}.iso" --releasever "${OPT_RELEASE_VERSION}${OPT_RELEASE_MINOR_VERSION}" --title="${OPT_RELEASE_TITLE} ${OPT_RELEASE_BASEARCH} ${OPT_RELEASE_DESKTOP}${OPT_RELEASE_BETA}" --macboot

      # clean up our inflated template file
      #rm -f "${_TEMPLATE}" "${_TEMPLATE_FLATTENED}"
    done
  fi
}
