#!/bin/bash
#################################################################
#this script was posted from longtom on: March 01, 2011			#
#as rotator, in PCLinuxOS-Forums.								#
#Designed for PCLinuxOS-LXDE.									#
#Added some commands and rename it as lxrotator, and wrote		#
#a GUI for it.													#
#Licence: GPLv2													#
#Author: longtom												#
#Second author: D.Meiß-Wilhelm (Leiche) 						#
#Don't remove this header										#
#################################################################
Encoding=UTF-8
# i18n - Internationalization -------------------------------------------
export TEXTDOMAIN=rotator_gui
export TEXTDOMAINDIR="/usr/share/locale"
# only one instance of lxrotator should be running ----------------------
RUN=`ps -e | grep -c lxrotator`
echo $"Instance is $RUN"
if [ $RUN -gt 2 ];then
	echo $"lxrotator is always running... exit"
	exit 0
fi
# some varables for handling --------------------------------------------
DIR=wallpaper
FLOOR=1
RANGE=`ls -1 "$DIR"/*.* | wc | awk '// {print $1}'`
SET=10
number=0
systray=0
delay=30
# /end variables --------------------------------------------------------
# check is systray avtivated, if true start rotator_tray ----------------
if [ $systray = 1 ]; then
	/usr/share/lxrotator/rotator_tray &
fi	
# here starts the lxrotator ---------------------------------------------
while [ 1 -eq 1 ]; do
   
   number=$RANDOM
   while [ "$number" -le $FLOOR ]; do
        number=$RANDOM
   done
   let "number %= $RANGE"  # Scales $number down within $RANGE.
   COUNTER=1
   for X in "$DIR"/*.*
   do
      if [ $number -eq $COUNTER ]; then 
		if [ "$XDG_CURRENT_DESKTOP" == "LXQt" ]; then
		 pcmanfm-qt -w "$X"
		 else
         pcmanfm -w "$X" 
         fi
         #if no wbar running, nothing will be happen
         if [ `ps -e | grep -c wbar` = 1 ]; then
         killall wbar & sleep 0.5 && exec wbar > /dev/null 2>&1 & 
         fi
      fi
   COUNTER=$(($COUNTER+1))
   done
   COUNTER=1
   sleep "$SET"m
done
