#!/bin/bash

# Version 0.1

# Generate a loading GIF animation
# Copyright (C) 2007  Aurelio A. Heckert <aurium # gmail dot com>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Features:
#  - Make a 60x60 GIF with 32 frames (with no options)

# Whish List:
#  - Set width and height
#  - Set the loading color
#  - Set the background color
#  - Make an indexed final file automaticaly

n=0

for ang in $( seq 22.5 22.5 360 ); do
  ang=$( echo -n $ang | sed 's/,/./' )

  for i in 0 1; do
    let n++
    num=$n
    while [ ${#num} -lt 3 ]; do num="0$num" ;done
  
    #h=$(( ($i*2)+8 ))
    #o=$(( ($i*25)+25 ))
    h=$(( ($i*3)+8 ))
    o=$(( ($i*45)+25 ))
    echo -e "$num \t ang: $ang \t h: $h \t o: 0.$o"
    
    sed "
      s/#angle#/$ang/;
      s/id=\"rect16\" height=\"8\"/id=\"rect16\" height=\"$h\"/
      s/\(id=\"rect16\".* opacity=\)\"0.25\"/\1\"0.$o\"/
      " loading.svg > loading.tmp.svg
    inkscape -b 'ffffffff' -e frame$num.png loading.tmp.svg >/dev/null
    if [ $num = 001 ]; then
      convert frame$num.png \
              -colors 20 +dither \
              -transparent '#ffffff' frame$num.gif
    else
      convert frame$num.png \
              -map frame001.png +dither \
              -transparent '#ffffff' frame$num.gif
    fi
  
  done

done

convert -dispose Background -delay 7 -loop 0 \
        frame*.gif loading.gif

rm frame* loading.tmp.svg

echo '
 Done!
 You can optimize this gif converting to 20 colors indexed image on GIMP.
'

