#! /usr/bin/env nix-shell
#! nix-shell -i bash -p jq wget

rm -r javascript/generated
hfile="src/Eventlog/AssetVersions.hs"
echo "-- Generated by ./update-assets.sh" > $hfile
echo "module Eventlog.AssetVersions where" >> $hfile

for row in $(cat versions.json | jq -r '.[] | @base64'); do
  _jq(){
    echo ${row} | base64 --decode | jq -r ${1}
  }

  version=$(_jq '.version')
  raw_url=$(_jq '.url')
  raw_name=$(_jq '.hname')
  url=${raw_url/<>/$version}
  wget -P javascript/generated $url
  echo "${raw_name}Version,${raw_name}URL :: String" >> $hfile
  echo "${raw_name}Version = \"$version\"" >> $hfile
  echo "${raw_name}URL = \"$url\"" >> $hfile

done

tmp_dir=$(mktemp -d)
wget -P $tmp_dir https://datatables.net/media/images/sort_asc.png
wget -P $tmp_dir https://datatables.net/media/images/sort_desc.png
wget -P $tmp_dir https://datatables.net/media/images/sort_both.png

b1=$(cat $tmp_dir/sort_asc.png | base64 -w0)
b2=$(cat $tmp_dir/sort_desc.png | base64 -w0)
b3=$(cat $tmp_dir/sort_both.png | base64 -w0)

echo "table.dataTable thead .sorting_asc { background-image: url(data:image/png;base64,$b1) !important }"  > javascript/generated/overide_images.css
echo "table.dataTable thead .sorting_desc { background-image: url(data:image/png;base64,$b2) !important }"  >> javascript/generated/overide_images.css
echo "table.dataTable thead .sorting { background-image: url(data:image/png;base64,$b3) }"  >> javascript/generated/overide_images.css

