#!/bin/bash

if [ "$1" ]; then
  TEXT="$1"
else
  echo "USAGE random_banner.sh TEXT"
  exit 1
fi

# A basic text banner
  echo "$TEXT" > temp_text
  ls /usr/X11R6/lib/X11/fonts/TTF/*.ttf > temp_font_list
  FONT=$(./random_line.sh temp_font_list)
  rm temp_font_list
  FGCOLOR=$(./random_line.sh /usr/X11R6/lib/X11/rgb.txt | cut -f 3- | sed 's/[ \t]*//g')

  CONVERTSTRING=" -background none"
  CONVERTSTRING="$CONVERTSTRING"" -fill ""$FGCOLOR"
  if [ "$(($RANDOM % 2))" = 1 ]; then
    STROKECOLOR=$(./random_line.sh /usr/X11R6/lib/X11/rgb.txt | cut -f 3- | sed 's/[ \t]*//g')
    CONVERTSTRING="$CONVERTSTRING"" -strokewidth 1"
    CONVERTSTRING="$CONVERTSTRING"" -stroke ""$STROKECOLOR"
  fi

  CONVERTSTRING="$CONVERTSTRING"" -font ""$FONT"
  CONVERTSTRING="$CONVERTSTRING"" -pointsize ""$(($RANDOM % 40 + 30))"
  CONVERTSTRING="$CONVERTSTRING"" label:@temp_text"
  CONVERTSTRING="$CONVERTSTRING"" -gravity center Banner.png"

#  echo $CONVERTSTRING
  convert $CONVERTSTRING
  rm temp_text

# Some special effects for the text?
# Like, say, a wave?
  if [ "$(($RANDOM % 8))" = 1 ]; then
    WAVESTRING="Banner.png -matte -background none -wave "
    WAVESTRING="$WAVESTRING""$(($RANDOM % 5 + 3))"x"$(($RANDOM % 200 + 200))"" Banner.png"
    convert $WAVESTRING
  fi
# Perhaps some shade?
  if [ "$(($RANDOM % 2))" = 1 ]; then
# Maybe with a blur?
    if [ "$(($RANDOM % 2))" = 1 ]; then
      BLURSTRING="Banner.png -blur 0x""$(($RANDOM % 4 + 3))"" Banner.png"
      convert $BLURSTRING
    fi
    SHADESTRING="Banner.png -shade ""$(($RANDOM % 180 + 0))""x""$(($RANDOM % 40 + 5))"" Banner.png"
    convert $SHADESTRING
# Since we shaded it, it's gray now, so tint it back.
    TINTSTRING="Banner.png -fill ""$FGCOLOR"" -tint ""$(($RANDOM % 80 + 60)) Banner.png"
    convert $TINTSTRING
  fi


# Get the size of the text banner, then pad it a little
  IMGSIZE=$(identify Banner.png | awk '{print $3}')
  IMGX=$(echo $IMGSIZE | awk -Fx '{print $1}')
  IMGY=$(echo $IMGSIZE | awk -Fx '{print $2}')
  IMGX=$(($IMGX + $(($IMGX / 10))))
  IMGY=$(($IMGY + $(($IMGY / 30))))
  IMGSIZE=$IMGX"x"$IMGY

# Make a background to fit the banner
  case $(($RANDOM % 4 +1)) in
    1)
      ./generate_background_solid.sh $IMGSIZE
      ;;
    2)
      ./generate_background_gradient.sh $IMGSIZE
      ;;
    3)
      ./generate_background_pattern.sh $IMGSIZE
      ;;
    4)
      ./generate_background_noise.sh $IMGSIZE
      ;;
  esac

  composite -gravity center Banner.png background.png Banner.png
  rm background.png

# Chance of bevel or frame
  if [ "$(($RANDOM % 2))" = 1 ]; then
    ./bevel.sh Banner.png
  fi
  if [ "$(($RANDOM % 2))" = 1 ]; then
    ./frame.sh Banner.png
  fi

exit 0
