#!/usr/bin/env bash
# Copyright Ken Fallon - Released into the public domain. http://creativecommons.org/publicdomain/ 
#============================================================

file_dir="/var/IA/uploads"

#$allowed_extensions = "flac", "opus", "ogg", "spx", "mp3", "jpg", "png", "json", "srt", "tsv", "txt", "vtt";

if [ -d "${file_dir}" ]
then
  cd "${file_dir}"
  echo '"episode_id","filename","extension","size", "sha1sum", "mime_type", "file_type"' | tee "${file_dir}/assets.csv"
  find "${file_dir}" -type f \( -iname "hpr*.wav" -o -iname "hpr*.flac" -o -iname "hpr*.opus" -o -iname "hpr*.ogg" -o -iname "hpr*.spx" -o -iname "hpr*.mp3" -o -iname "hpr*.jpg" -o -iname "hpr*.png" -o -iname "hpr*.json" -o -iname "hpr*.srt" -o -iname "hpr*.tsv" -o -iname "hpr*.txt" -o -iname "hpr*.vtt" \) | while read this_file
  do
    this_file_basename=$( basename "${this_file}" )
    this_file_dirname=$( dirname "${this_file}" )
    this_file_prefix="${this_file_basename%.*}"
    this_file_extension="${this_file_basename##*.}"
    if [ "$( echo "${this_file_prefix}" | grep -Ec '^hpr[0-9]{4}' )" -ne "1" ]
    then
      continue
    fi
    
    this_file_episode_id="$( echo "${this_file_prefix}" | sed 's/hpr//g' | awk -F '_' '{print $1}' )"
    if [ "${this_file_episode_id}" -lt "1" ]
    then
      continue
    fi
    
    if [ "${this_file_episode_id}" -gt "99999" ]
    then
      continue
    fi

    this_file_size="$( ls -al "${this_file}" | awk '{print $5}' )"
    this_file_sha1sum="$( sha1sum "${this_file}" | awk '{print $1}' )"
    this_file_mime_type=$( file --dereference --brief --mime "${this_file}" )
    this_file_file_type=$( file --dereference --brief "${this_file}" )
    
    echo "${this_file_episode_id},\"${this_file_basename}\",\"${this_file_extension}\",\"${this_file_size}\",\"${this_file_sha1sum}\",\"${this_file_mime_type}\",\"${this_file_file_type}\"" | tee --append "${file_dir}/assets.csv"
    
  done
fi

if [ -s "${file_dir}/assets.csv" ]
then
  cat "${file_dir}/assets.csv" | csvtojson | jq '{"assets":[.[]]}' | tee "${file_dir}/assets.json"
fi

if [ -s "${file_dir}/assets.json" ]
then
  curl --netrc-file $HOME/.netrc --verbose --request POST https://hub.hackerpublicradio.org/cms/assets.php --data-ascii @"${file_dir}/assets.json" --header "Content-Type: application/json"
fi
