#!/bin/bash

root_path='/opt/gnssro/POD_DATA/cosmic2/LEOPOD'
today=`date -u +"%Y-%m-%d"`
d1=$(date -d "$today" +%s)

subs=( leoAtt leoClk leoOrb podRnx )

for d in "${subs[@]}"; do
  ldir=(`ls ${root_path}/$d`)
  for date_str in "${ldir[@]}"; do
     year="${date_str%%.*}"
     doy="${date_str##*.}"

     # 2. Get the date
     dd=$(date -d "${year}-01-01 +$((doy - 1)) days" +"%Y-%m-%d")
     d2=$(date -d "$dd" +%s)
     # Calculate the difference and divide by seconds in a day
     days=$(( (d1 -d2) / 86400 ))
     if (( $days > 6 )); then
       if [ "$d" == "leoOrb" ]; then
             #echo "rm -fr ${root_path}/$d/${date_str}/*.KIN"
             #echo "rm -fr ${root_path}/$d/${date_str}/*.FIN"
             rm -fr ${root_path}/$d/${date_str}/*.KIN
       else
         #echo "rm -fr ${root_path}/$d/${date_str}"
         rm -fr ${root_path}/$d/${date_str}
       fi
    fi
  done

done

