#! /bin/bash
#
# debian/repack
# Part of the Debian package ‘libjs-zxcvbn’.
# Modified by Tiago Daitx for use with the Debian package 'openjdk-6'.
#
# Copyright © 2013–2014 Ben Finney <ben+debian@benfinney.id.au>
# This is free software; see the end of this file for license terms.

# Convert the pristine upstream source to the Debian upstream source.
#
# This program is designed for use with the ‘uscan(1)’ tool, as the
# “action” parameter for the ‘debian/watch’ configuration file.

set -o errexit
set -o errtrace
set -o pipefail
set -o nounset

exec 1>&2

function usage() {
    local progname=$(basename $0)
    printf "$progname --upstream-version VERSION FILENAME\n"
    printf "To avoid downloading OpenJDK, Cacao, or JamVM sources set the TARBALLDIR environment variable and put the tarballs there.\n"
}

if [ $# -ne 3 ] ; then
    usage
    exit 1
fi

program_dir="$(dirname "$(realpath --strip "$0")")"

package_name=$(dpkg-parsechangelog --show-field=Source)
upstream_version="$2"
downloaded_file="$3"

working_dir="$(mktemp -d -t)"
exit_sigspecs="ERR EXIT SIGTERM SIGHUP SIGINT SIGQUIT"

function cleanup_exit() {
    exit_status=$?
    trap - $exit_sigspecs

    rm -rf "${working_dir}"
    printf "Cleaned up working directory ‘${working_dir}’\n"

    exit $exit_status
}
trap cleanup_exit $exit_sigspecs

function get_source() {
  local src_name="$1"
  local src_zip="$2"
  local src_url="$3"
  local src_dst="$4"
  local tarballdir="${TARBALLDIR-}"
  # Use sources from TARBALLDIR if they exist, download them otherwise
  if [ -e "${tarballdir}/${src_zip}" ]; then
    printf "Copying ${src_name}:\n"
    cp -v "${tarballdir}/${src_zip}" "${src_dst}"
  else
    # download OpenJDK 6 source
    printf "Downloading ${src_name}:\n"
    wget "${src_url}" -O "${src_dst}/${src_zip}"
  fi
}

printf "Unpacking pristine upstream source ‘${downloaded_file}’:\n"
tar -xf "${downloaded_file}" -C "${working_dir}"

upstream_source_dirname=$(ls -1 "${working_dir}")
upstream_source_dir="${working_dir}/${upstream_source_dirname}"

# get URLs, filenames, and checksum from IcedTea Makefile.am
source <(head -n18 ${upstream_source_dir}/Makefile.am | tr '()' '{}' | tr -d ' ')
source <(fgrep 'OPENJDK_SRC_ZIP = ' ${upstream_source_dir}/Makefile.am | tr '()' '{}' | tr -d ' ')

# Override JamVM as we use a newer version
JAMVM_SHA256SUM=76428e96df0ae9dd964c7a7c74c1e9a837e2f312c39e9a357fa8178f7eff80da
JAMVM_SRC_ZIP=jamvm-2.0.0.tar.gz
JAMVM_URL=http://icedtea.classpath.org/download/drops/jamvm/jamvm-${JAMVM_VERSION}.tar.gz

# Get OpenJDK, Cacao, and JamVM sources
get_source OpenJDK "${OPENJDK_SRC_ZIP}" "${OPENJDK_URL}${OPENJDK_SRC_ZIP}" "${upstream_source_dir}"
get_source Cacao "${CACAO_SRC_ZIP}" "${CACAO_URL}" "${upstream_source_dir}"
get_source JamVM "${JAMVM_SRC_ZIP}" "${JAMVM_URL}" "${upstream_source_dir}"

# check their SHA256SUM
sha256sum -c <<CHKSUM
${OPENJDK_SHA256SUM}  ${upstream_source_dir}/${OPENJDK_SRC_ZIP}
${CACAO_SHA256SUM}  ${upstream_source_dir}/${CACAO_SRC_ZIP}
${JAMVM_SHA256SUM}  ${upstream_source_dir}/${JAMVM_SRC_ZIP}
CHKSUM

target_version=6${OPENJDK_VERSION}-${upstream_version}
tarball_basename="${package_name}_${target_version}.orig"
tarball_file="${working_dir}/${tarball_basename}.tar.xz"

repack_dir="${working_dir}/${tarball_basename}"

orig_file="$(dirname "${downloaded_file}")/${tarball_basename}.tar.xz"

printf "Repackaging upstream source from ‘${upstream_source_dir}’ to ‘${repack_dir}’:\n"
mv "${upstream_source_dir}" "${repack_dir}"

printf "Rebuilding upstream source tarball:\n"
tar -C "${working_dir}" -cJf "${tarball_file}" "${tarball_basename}"

printf "Moving completed upstream tarball to ‘${orig_file}’:\n"
rm -v "${downloaded_file}"
mv "${tarball_file}" "${orig_file}"

printf "Done. Calling uupdate\n"
uupdate --upstream-version ${target_version} "${orig_file}"

# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# “Software”), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# The Software is provided “as is”, without warranty of any kind,
# express or implied, including but not limited to the warranties of
# merchantability, fitness for a particular purpose and noninfringement.
# In no event shall the authors or copyright holders be liable for any
# claim, damages or other liability, whether in an action of contract,
# tort or otherwise, arising from, out of or in connection with the
# Software or the use or other dealings in the Software.


# Local variables:
# coding: utf-8
# mode: sh
# indent-tabs-mode: nil
# End:
# vim: fileencoding=utf-8 filetype=bash :
