#!/bin/csh
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License

# First see if a swirl is running.
# Run ps through some greps.  Grep out this process (parent pid=$$)
# the 7ffe6000 is so we ignore processes in the middle of an exec (WCHAN field)
# this shit doesn't work either :(

#skip the if clause
if ( 0 == 1 ) then
#if ( `ps -lu $USER | grep swirl | grep -v grep | grep -v 7ffe6000  \
      | grep -v $$ | wc -l` != 0 ) then

#  echo Swirl already running.  Forget it.
  exit

  echo Found one, gonna kill it!

# Sometimes we will get multiple swirl processes this way.
# This is because the swirl running will need to fork and exec to
# run xsetroot.  If we catch it between fork and exec we get two :(
# Lets just try to kill the first one and assume we did good :)

  set tmp=`ps -lu $USER | grep swirl | grep -v grep | grep -v $$`
  echo $tmp
  echo ---------
  # try the first line first (assuming we have two processes)
  set try=`echo $tmp | cut -c 11-15`
  echo First try:  $try
#  set output=`kill -0 $try`
#  echo OUTPUT: $output
#  if ( "$output" == "$try\: No such process" ) echo Nope
  exit
  echo
  echo $tmp | cut -c 11-15
  exit
# Kill swirl and clean up
#  kill -9 $tmp
#  xsetroot -solid blue
#  exit
endif

# else, there is no swirl...  so start one up.

set hexdigits="0 1 2 3 4 5 6 7 8 9 A B C D E F"
set highhexdigits="1 3 5 6 7 8 9 A B C D E F"
set revhexdigits="F E D C B A 9 8 7 6 5 4 3 2 1 0"
set revhighhexdigits="F E D C B A 9 8 7 6 5 3 1"

again_loop:

# First up to blue and back
foreach a ( $hexdigits )
  foreach b ( $hexdigits )
    xsetroot -solid rgb:0/0/"$a""$b"00
  end
end
foreach a ( $revhexdigits )
  foreach b ( $revhexdigits )
    xsetroot -solid rgb:0/0/"$a""$b"00
  end
end
# then green and back
foreach a ( $hexdigits )
  foreach b ( $hexdigits )
    xsetroot -solid rgb:0/"$a""$b"00/0
  end
end
foreach a ( $revhexdigits )
  foreach b ( $revhexdigits )
    xsetroot -solid rgb:0/"$a""$b"00/0
  end
end

goto again_loop
