#!/bin/bash
# xplanet-gnome.sh shell script v0.4
# shows Earth on your Gnome desktop with current lighting conditions,i.e. day and night
# ChangeLog
# Version 0.4
# >  If rotating counterclockwise it now reset LONGITUDE if it <= -360°
# Version 0.3
# >  Added comments
# >  Rotating planet (one frame per minute default)
# >  Background option included
# >  Debugging option/output
# >  When changing background it generates first the background and deletes then the old file

# Remove "#" to activate clouds (need the script)
#perl $HOME/.xplanet/download_clouds.pl 

# set the delay (default 1 minute)
DELAY=1m
# set the delay if the globus isn't rotating (default 30 minutes)
# could be removed if rtating is always on
NOROTATINGDELAY=30m

# Background file
#BACKGROUND=
PREFIX=$HOME/.xplanet/images/
OUTPUT=xplanet.png
APPEND=2
# sets the numbers of longitudes added each "frame"
# "rotating speed" - if 0 then rotating is deactivated [-360 .. 360]
SPEED=10
# geometry of the desktop
GEOMETRY=800x640
# between 0 and 359 (left/right)
LONGITUDE=11
# between -90 and 90 (Southpole .. Northpole)
LATITUDE=54
# default is no projection,i.e. render a globe
# rectangular is the flat world map. also try ancient, azimuthal,  mercator,..
#PROJECTION=rectangular  

# if debugging is activated the console prints echos ["on" / "off"]
DEBUGGING="off"

#rename background image so Gnome realises image has changed - thx to dmbasso

if [ "$*" != "" ]; then
  let "LONGITUDE=$1"
fi

if [ $LONGITUDE -ge 360 ] || [ $LONGITUDE -le 360 ]; then
  let "LONGITUDE%=360"
  if [ $DEBUGGING = "on" ]; then
    echo "Set LONGITUDE on LONGITUDE modulo 360"
  fi
fi

if [ -e "$PREFIX$OUTPUT" ]; then
   RMFILE="$PREFIX$OUTPUT"
   OUTPUT="$APPEND$OUTPUT"
else
   RMFILE="$PREFIX$APPEND$OUTPUT"
fi
if [ $DEBUGGING = "on" ]; then
  echo "Changed file and file to removed"
  echo "New filename: $PREFIX$OUTPUT"
fi

if [ -z $PROJECTION ]; then 
  if [ -z $BACKGROUND ]; then
    xplanet -num_times 1 -output "$PREFIX$OUTPUT" -geometry $GEOMETRY -longitude $LONGITUDE -latitude $LATITUDE
  else
    xplanet -num_times 1 -output "$PREFIX$OUTPUT" -geometry $GEOMETRY -longitude $LONGITUDE -latitude $LATITUDE -background $BACKGROUND
  fi
else
  if [ -z $BACKGROUND ]; then
    xplanet -num_times 1 -output "$PREFIX$OUTPUT" -geometry $GEOMETRY -longitude $LONGITUDE -latitude $LATITUDE -projection $PROJECTION
  else
    xplanet -num_times 1 -output "$PREFIX$OUTPUT" -geometry $GEOMETRY -longitude $LONGITUDE -latitude $LATITUDE -projection $PROJECTION -background $BACKGROUND
  fi
fi

if [ $DEBUGGING = "on" ]; then
  echo "Logitude: $LONGITUDE"
  echo "Latitude: $LATITUDE"
fi

#update Gnome backgound
echo "Changed image ($LONGITUDE °/ $LATITUDE °)"
gconftool -t str -s /desktop/gnome/background/picture_filename "$PREFIX$OUTPUT"
#update LXDE (Lubuntu) backgound
#pcmanfm -w "$PREFIX$OUTPUT"

# delete old file
if [ -e "$RMFILE" ]; then
  rm $RMFILE
  if [ $DEBUGGING = "on" ]; then
    echo "File removed: $RMFILE"
  fi
else
  echo "File set to removed doesn't exists: $RMFILE"
fi

# if speed is not 0 it's rotating
if [ $SPEED != 0 ]; then
  # add speed to longitude
  let "LONGITUDE+=$SPEED"
else
  # set the no-rot-delay if rotating is deactivating
  # doesn't needed so much updates
  DELAY= $NOROTATINGDELAY
fi

# waiting the delay time
sleep $DELAY
exec $0 $LONGITUDE
