#!/bin/bash

#luamd64_1610.sh 2016-10-27 rocketmouse

flavour=ubuntu-mate
release=16.10
hms=$(date "+%Y%m%d-%H%M%S")

use()
{
cat<<EOF

Usage: ./$(basename "$0") [FLAVOUR [RELEASE]]
'luamd64' gets the 'l'atest 'u'buntu desktop flavour 'amd64' iso for you.

  The default flavour is ubuntu-mate.
  The default release is 16.10.

Other desktop flavours available for at least 16.10

  ubuntu
  kubuntu
  lubuntu
  ubuntu-gnome
  ubuntukylin
  ubuntustudio
  xubuntu

Root can't run this script.

EOF
}

key ()
{
  printf "\033[7m\n"
  echo $@
  printf "\033[0m\n\n"
  read -n1 -p"Press a key to continue "
  printf "\n\n"
  $@
}

use

[ $(id -u) -ne 0 ] || exit

case $1 in "");;
  *) flavour=$1
     case $2 in "");;
       *) release=$2;;
     esac;;
esac
iso=$release-desktop

key mkdir /tmp/$flavour-$iso-amd64-$hms
key cd /tmp/$flavour-$iso-amd64-$hms
case $flavour in
  ubuntu)
    key wget http://releases.ubuntu.com/$release/{$flavour-$iso-amd64.iso,SHA256SUMS{,.gpg}}
    ;;
  *)
    case $flavour in
      ubuntustudio)
        iso=$release-dvd
        ;;
    esac
    key wget http://cdimage.ubuntu.com/$flavour/releases/$release/release/{$flavour-$iso-amd64.iso,SHA256SUMS{,.gpg}}
    ;;
esac
key gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys FBB75451 EFE21092
key gpg --list-keys --with-fingerprint FBB75451 EFE21092
key gpg --verify SHA256SUMS.gpg SHA256SUMS
printf "\n\033[7m\nThe following validation might take a while\nsha256sum -c SHA256SUMS 2>&1 | grep OK     \033[0m\n\n"
read -n1 -p"Press a key to finish "
printf "\n\n"
sha256sum -c SHA256SUMS 2>&1 | grep OK
status=$?
if [ $status -ne 0 ]; then
  echo "$flavour-$iso-amd64.iso: Validation failed"
fi
echo

exit $status
