#!/bin/bash

#-----------------
# <url> : base URL
#-----------------
url=https://order.class.noaa.gov

if [ $# -lt 1 ]; then
  echo
  echo "#####################################"
  echo "#                                   #"
  echo "#  parameter <order_number> needed  #"
  echo "#                                   #"
  echo "#####################################"
  echo
  exit
fi
order=${1}

if [ ! -d ./${order} ]; then

  mkdir ${order}
  cd ${order}

  #----------------------
  # get 'index.html' file
  #----------------------
  wget -q ${url}/public/${order}/001/

  #-------------------
  # extract file names
  #-------------------
  cat index.html | grep "/downloads/public/${order}/001/" | awk -F'"' '{print $2}' | awk -F'/' '{print $6}' > files

  #-------------------
  # download each file
  #-------------------
  echo
  for ff in $(cat files); do
    wget -q ${url}/downloads/public/${order}/001/${ff}
    ls -l ${ff}
  done
  echo

  #------------
  # count files
  #------------
  wc -l files
  rm -f index.html files
  
  ls | wc -l
  echo
  
else

  echo "#### ${order} already exists"

fi

exit
