#!/bin/sh

usage() {
  cat <<__EOF__
usage: $(basename $0) [-hl] [-u user] [-X args] [-d args]
-h        print this help text
-l        print list of files to download
-u user   download as authenticated user
-X args   extra arguments to pass to xargs
-d args   extra arguments to pass to the download program

__EOF__
}

username=
xargsopts=
prompt=
list=
while getopts hlu:xX:d: option; do
  case $option in
  h)
    usage
    exit
    ;;
  l) list=yes ;;
  u)
    prompt=yes
    username="$OPTARG"
    ;;
  X) xargsopts="$OPTARG" ;;
  d) download_opts="$OPTARG" ;;
  ?)
    usage
    exit 2
    ;;
  esac
done

cat <<__EOF__
      Please note that running this script will result in the entire data collection to be downloaded, i.e. 3.97 GB in 41 files.

      To refine the selection and download, visit the following URL:

           https://archive.eso.org/scienceportal/home?data_collection=SUPER

      and specify the relevant additional query constraints.

      Do you want to proceed with running this script (y/N)?
__EOF__

read continue_prompt

if [ "$continue_prompt" != "y" ]; then
  exit 0;
fi

if test -z "$xargsopts"; then
  #no xargs option speficied, we ensure that only one url
  #after the other will be used
  xargsopts='-L 1'
fi

if test -n "$prompt" -a -z "$list"; then
  trap 'stty echo 2>/dev/null; echo "Cancelled."; exit 1' INT HUP TERM
  stty -echo 2>/dev/null
  printf 'Password: '
  read password
  echo ''
  stty echo 2>/dev/null
fi

# use a tempfile to which only user has access
tempfile=$(mktemp /tmp/dl.XXXXXXXX 2>/dev/null)
test "$tempfile" -a -f $tempfile || {
  tempfile=/tmp/dl.$$
  (umask 077 && : >$tempfile)
}
trap 'rm -f $tempfile' EXIT INT HUP TERM

echo "auth_no_challenge=on" >$tempfile
# older OSs do not seem to include the required CA certificates for ESO
echo "check-certificate=off" >>$tempfile
if [ -n "$prompt" ]; then
  echo "--http-user=$username" >>$tempfile
  echo "--http-password=$password" >>$tempfile

fi
WGETRC=$tempfile
export WGETRC

unset password

if test -n "$list"; then
  cat
else
  xargs $xargsopts wget $download_opts
fi <<'__EOF__'
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.774
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.776
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.778
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.780
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.782
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.784
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.786
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.788
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.790
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.792
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.794
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.796
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.798
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.800
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.802
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.804
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.806
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.808
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.810
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.812
https://dataportal.eso.org/dataPortal/file/ADP.2026-02-10T14:40:19.814
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.117
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.119
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.121
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.123
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.125
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.127
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.129
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.131
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.133
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.135
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.137
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.139
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.141
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.143
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.145
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.147
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.149
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.151
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.153
https://dataportal.eso.org/dataPortal/file/ADP.2021-03-23T14:55:45.155
__EOF__
