
unset SKIP_SYNC

DATEDIR=$(date +%Y-%m-%d)
if [ -e /var/lib/groovix/times-per-day/$DATEDIR/60-download-screensavers ] ; then
	#echo "already ran 60-download-screensavers today, not running again"
	echo "already ran 60-download-screensavers today, but running again"

	# DO NOT SKIP - NOW THAT WE CHECK FOR CHANGES WE CAN RUN THIS MORE THAN ONCE IN CASE THEY PURPOSEFULLY CHANGED THEM MID-DAY !
	#oSKIP_SYNC="true"
else
	mkdir -p /var/lib/groovix/times-per-day/$DATEDIR
	touch /var/lib/groovix/times-per-day/$DATEDIR/60-download-screensavers
fi

if [ -z "$SKIP_SYNC" ] ; then

################################################
##### CUSTOM SCREENSAVERS
if [ -n "$GX_CUSTOM_SCREENSAVER_IMAGES_URL_DIR" ] || [ -n "$GX_CUSTOM_SCREENSAVER_IMAGES_URL_LIST" ] ; then
  #get screensaver images
  #must clear cache
  rm /var/lib/groovix/gxscreensaver/.xscreensaver-getimage.cache

  #get rid of old stuff
  rm -rf /var/lib/groovix/screensaver/images/defaults
  rm -rf /var/lib/groovix/screensaver/images.dontuse/
  rm -rf /var/lib/groovix/screensaver/images/custom.bad
  mkdir -p /var/lib/groovix/screensaver/images.dontuse/
  mv /var/lib/groovix/screensaver/images/custom /var/lib/groovix/screensaver/images.dontuse/custom.last
  mkdir /var/lib/groovix/screensaver/images/custom

  cd  /var/lib/groovix/screensaver/images/custom
  mv /var/lib/groovix/screensaver/download-list.txt /var/lib/groovix/screensaver/download-list.txt.old

  if [ -n "$GX_CUSTOM_SCREENSAVER_IMAGES_URL_DIR" ] ; then
    wget --tries=2 --timeout=60 $GX_CUSTOM_SCREENSAVER_IMAGES_URL_DIR -O /var/lib/groovix/screensaver/download-list.txt
    diff /var/lib/groovix/screensaver/download-list.txt /var/lib/groovix/screensaver/download-list.txt.old
    if [ $? -eq 0 ] ; then
      #no diff - no need to get new images
      echo "images have not changed, not downloading"
    else
      echo "images have changed, downloading..."
      wget --tries=2 --timeout=60 -P /var/lib/groovix/screensaver/images/custom/ --reject index.html --accept jpg,JPG,png,PNG,gif,GIF --no-parent -r -l 1 "$GX_CUSTOM_SCREENSAVER_IMAGES_URL_DIR"
    fi 
  fi
  if [ -n "$GX_CUSTOM_SCREENSAVER_IMAGES_URL_LIST" ] ; then
    wget --tries=2 --timeout=60 $GX_CUSTOM_SCREENSAVER_IMAGES_URL_LIST -O /var/lib/groovix/screensaver/download-list.txt
    diff /var/lib/groovix/screensaver/download-list.txt /var/lib/groovix/screensaver/download-list.txt.old
    if [ $? -eq 0 ] ; then
      #no diff - no need to get new images
      echo "images have not changed, not downloading"
    else
      echo "images have changed, downloading..."
      for T in `cat /var/lib/groovix/screensaver/download-list.txt`; do
          wget --tries=1 --timeout=10 $T
      done
    fi
  fi
  rm  /var/lib/groovix/screensaver/images/custom/*/robots.txt
  #in case no images found, revert back to old custom images from last boot
  #if they just want one for signage as opposed to screensaver let them have just one - no longer #need at least 2 to switch between
  NUMIMAGES=`find /var/lib/groovix/screensaver/images/custom/   -type f  | egrep -i "\.(jpg|png|gif)$" | wc -l`
  if  [ $NUMIMAGES -lt 1 ] ; then
        mv /var/lib/groovix/screensaver/images/custom/ /var/lib/groovix/screensaver/images/custom.bad/
        mv /var/lib/groovix/screensaver/images.dontuse/custom.last/  /var/lib/groovix/screensaver/images/custom
  fi
  #in case no images in old custom images, revert back to defaults
  NUMIMAGES=`find /var/lib/groovix/screensaver/images/custom/   -type f  | egrep -i "\.(jpg|png|gif)$" | wc -l`
  if  [ $NUMIMAGES -lt 1 ] ; then
        ln -s  /var/lib/groovix/screensaver/defaults/  /var/lib/groovix/screensaver/images/defaults
  fi
else
  echo no GX_CUSTOM_SCREENSAVER_IMAGES_URL_DIR or GX_CUSTOM_SCREENSAVER_IMAGES_URL_LIST defined
fi
##### CUSTOM SCREENSAVERS
################################################


fi
