#! /bin/bash
#
# Service operations for a JMRI test release
# Argument is release version number, e.g. 4.5.8
# - Download release files from Jenkins and unpack
# - Display the checksums
# - Display sample text for releaselist file
# - Display sample text for JMRI release note

# The script can't know the full name of the individual files, as they contain the
# Git SHA of the commit.

# if files present, then use them
macfile=release/JMRI.${1}+*.dmg
linfile=release/JMRI.${1}+*.tgz
winfile=release/JMRI.${1}+*.exe

if [ -r $macfile ] && [ -r $linfile ] && [ -r $winfile ]; then
    echo "found files, skipping curl"
else
    # Download release from Jenkins and unpack
    if ! curl -o release.zip "https://builds.jmri.org/jenkins/job/testreleases/job/$1/lastSuccessfulBuild/artifact/*zip*/archive.zip"; then
        exit
    fi
    rm -f release/JMRI.${1}*
    unzip -j -d release release.zip
fi

cd release

# Decide command to create checksums
if [ -x /usr/bin/hdiutil ]
then
  SYSTEM=MACOSX
else
  SYSTEM=LINUX
fi
if [ "$SYSTEM" = "MACOSX" ]
then
  SHA="shasum -a 256"
else
  SHA="sha256sum"
fi

# Create and display the checksums
$SHA JMRI*

# Display sample text for releaselist file
echo
echo "releaselist:"
echo '----------------------------------------------------------------------------------'
echo "  ${1}:"
echo "   tgz: "\""https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.tgz`"\"
echo "   dmg: "\""https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.dmg`"\"
echo "   exe: "\""https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.exe`"\"
echo '----------------------------------------------------------------------------------'

echo
echo

# Display sample text for JMRI release note
echo "release note:"
echo '----------------------------------------------------------------------------------'
echo '<ul>'
echo '<li>OS X / macOS: <a'
echo "	href="\""https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.dmg`"\"">https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.dmg`</a><br/>"
echo  "  sha256: `$SHA *.dmg| awk '{print $1}'` </li>"
echo
echo '<li>Windows: <a'
echo "	href="\""https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.exe`"\"">https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.exe`</a><br/>"
echo "   sha256: `$SHA *.exe| awk '{print $1}'`</li>"
echo
echo '<li>Linux: <a'
echo "	href="\""https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.tgz`"\"">https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.tgz`</a><br/>"
echo "   sha256: `$SHA *.tgz| awk '{print $1}'`</li>"
echo '</ul>'
echo '----------------------------------------------------------------------------------'

echo
echo

# Display sample text for GitHub release note
echo "Github note body:"
echo '----------------------------------------------------------------------------------'
echo
echo "[Release notes](https://jmri.org/releasenotes/jmri${1}.shtml)"
echo
echo 'Checksums:'
echo
echo 'File | SHA256 checksum'
echo '---|---'
echo "[`ls *.dmg`](https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.dmg`) | `$SHA *.dmg| awk '{print $1}'`"
echo "[`ls *.exe`](https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.exe`) | `$SHA *.exe| awk '{print $1}'`"
echo "[`ls *.tgz`](https://github.com/JMRI/JMRI/releases/download/v${1}/`ls *.tgz`) | `$SHA *.tgz| awk '{print $1}'`"
echo
echo '----------------------------------------------------------------------------------'

