#!/bin/bash

# Find the files generated at an earlier time
old_files=`ls -l | grep " 2019 " | grep -v thumb | head -1000`
# Strip away everything but the file name
cips_files=`echo $old_files | awk '{ for(i=1; i<=NF; i++) { print $i}}' | grep cips`
# Get rid of the geolocation part of the file name and everything else to the right of that
cips_stems=(`echo $cips_files | sed -e s'/_[EW][01][0-9][0-9]_[NS][0-9][0-9].png//g'`)
# Sequence through these one at a time
for i in "${cips_stems[@]}"
do
  if [[ $i =~ .*2019-21[3-9].* ]]; then continue ; fi
  if [[ $i =~ .*2019-22[012].* ]]; then continue; fi
  echo "$i"
  file_names=(`ls "${i}"*`)
  if [ ${#file_names[@]} == 4 ]; then
    extra_files=`ls -l "${i}"* | grep " 2019 "`
    surplus_files=(`echo $extra_files | awk '{ for(i=1; i<=NF; i++) { print $i}}' | grep cips`)
    for j in "${surplus_files[@]}"
    do
#      rm -v $j
      ls -l $j
    done
  else
    echo ${#file_names[@]}
  fi
done

