#!/bin/bash

#  This program is copyright 1995 Sam Trenholme

#  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 2 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, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#  I would have rather have written this in tcsh instead of bourne shell
#  [my Berkeley UNIX upbringing], but I can not guarentee that every
#  Linux/UNIX box out there has tcsh installed, since it is an optional
#  installation in both the Debian and the widely-used Slackware 
#  distributions, while bash is required in both of these distributions.

#  Bash is closer to the GNU/GPL spirit, anyway

echo "MAIN" > /tmp/petal_selection

XINC="0.25"
YINC="1"
RIPPLE="0.03"
WIDTH="0.03"

NUM_SIDES="12"
ANGLE="33"

# Now, admittably, the following code is rather inefficient...
# it would be more efficient to do an exec every time we change the menu we 
# are in, so we don't have to trudge through every possible menu, instead
# using a different shell script for every possible menu.

# A non-hierarchial menu is an example of a case where "goto"s are a good idea
# because standard structured program does not handle special cases of
# "cross-links" arranged in a web very efficiently. Gotos are better, and
# are easy enough to compile.

while [ "$SELECTION" != "DONE" ]; do 

  SELECTION="`cat /tmp/petal_selection`"

  if [ "$SELECTION" = "MAIN" ]; then  
    dialog --title "Flower generation program menu" --menu "\nPlease enter \
your selection with either the arrow keys or the '+', '-',\nand \
'TAB' keys\n" 15 76 4 \
    "PETAL" "Modify the parameters of the petal" \
    "FLOWER" "Modify the parameters of the flower" \
    "VIEW" "View the parameters of the flower" \
    "DONE" "Output the flower to a pov scene file" 2> /tmp/petal_selection
  fi

  if [ "$SELECTION" = "VIEW" ]; then
     dialog --msgbox "\nThe values are as follows:\n\nPetal parameters:\n\
XINC: "$XINC"\nYINC: "$YINC"\nRIPPLE: "$RIPPLE"\nWIDTH: "$WIDTH"\n\
\nFlower parameters:\nNUM_SIDES: "$NUM_SIDES"\nANGLE: "$ANGLE"\n" 17 38
     echo "MAIN" > /tmp/petal_selection
  fi

  if [ "$SELECTION" = "PETAL" ]; then
     dialog --title "Petal parameters menu" --menu "\nPlease choose which \
parameter of the petals you wish to modify\n" 16 76 5 \
     "XINC" "The resolution of the flower along the X axis" \
     "YINC" "The resolution of the flower along the Y axis" \
     "RIPPLE" "The height of the ripples of the flower" \
     "WIDTH" "The width of the petal in the flower" \
     "MAIN"  "Back to the main menu" 2> /tmp/petal_selection
   fi

   if [ "$SELECTION" = "XINC" ]; then
      dialog --title "X resolution" --inputbox "Please type in the size of \
triangles used to render flowers along \nthe X-axis. Lower numbers give you \
higher resolution petals, at the \nexpense of more memory needed by POV to \
render the flower." 9 76 2> /tmp/petal_xinc
     XINC="`cat /tmp/petal_xinc`"
     rm /tmp/petal_xinc
     echo "PETAL" > /tmp/petal_selection
   fi

   if [ "$SELECTION" = "YINC" ]; then
      dialog --title "Y resolution" --inputbox "Please type in the size of \
triangles used to render flowers along \nthe Y-axis. Lower numbers give you \
higher resolution petals, at the \nexpense of more memory needed by POV to \
render the flower." 9 76 2> /tmp/petal_yinc
     YINC="`cat /tmp/petal_yinc`"
     rm /tmp/petal_yinc
     echo "PETAL" > /tmp/petal_selection
   fi

   if [ "$SELECTION" = "RIPPLE" ]; then
      dialog --title "Petal ripple" --inputbox "Please type in the size of \
the 'ripples' in the flower. Numbers around\n0.03 give more natural-looking \
flowers, however, higher numbers\nare useful for special effects." \
9 76 2> /tmp/petal_ripple
      RIPPLE="`cat /tmp/petal_ripple`"
      rm /tmp/petal_ripple
      echo "PETAL" > /tmp/petal_selection
   fi

   if [ "$SELECTION" = "WIDTH" ]; then
      dialog --title "Petal width" --inputbox "Please type in the width of \
the petals in the flower. Numbers around \n0.03 give natural-looking \
flowers. Higer numbers\n are good for 'bowl' effects." \
9 76 2> /tmp/petal_width
      WIDTH="`cat /tmp/petal_width`"
      rm /tmp/petal_width
      echo "PETAL" > /tmp/petal_selection
   fi

   if [ "$SELECTION" = "FLOWER" ]; then
      dialog --title "Flower parameters menu" --menu "\nPlease choose which \
parameter of the flower you wish to modify\n" 14 76 3 \
      "NUM_SIDES" "Change the number of petals on the flower" \
      "ANGLE" "Change the angle of the petals on the flower" \
      "MAIN" "Back to the main menu" 2> /tmp/petal_selection
   fi

   if [ "$SELECTION" = "NUM_SIDES" ]; then
      dialog --title "Number of sides" --inputbox "Please type in the number \
of petals on the flower. Odd numbers means she loves me, even numbers mean \
she loves me not." 9 76 2> /tmp/petal_num_sides
      NUM_SIDES="`cat /tmp/petal_num_sides`"
      rm /tmp/petal_num_sides
      echo "FLOWER" > /tmp/petal_selection
   fi

   if [ "$SELECTION" = "ANGLE" ]; then
      dialog --title "Angle of petals" --inputbox "Please type in the angle \
of the petals on the flower. An angle of 0 gives a flat flower, an angle of \
90 gives a flower with petals facing vertically upwards." 9 76 \
      2> /tmp/petal_angle
      ANGLE="`cat /tmp/petal_angle`"
      rm /tmp/petal_angle
      echo "FLOWER" > /tmp/petal_selection
   fi

   #if the user selects "cancel"

   if [ "$SELECTION" = "" ]; then
      SELECTION="DONE"
   fi

   if [ "$SELECTION" = "DONE" ]; then
      dialog --title "Output to POV?" --yesno "\nDo you wish to output the \
flower you have designed to a set of\nfiles that POV can draw a flower from?" \
8 76
      if [ $? = 0 ]; then
         echo making POV files... please wait
         petal -x$XINC -y$YINC -r$RIPPLE -w$WIDTH -tds1 -op2i > petal.inc
         flower -a$ANGLE -s$NUM_SIDES -tds1 -op2s > flower.pov
         dialog --msgbox "\nThe POV files have been created and are in the \
following files:\npetal.inc: The petal to the flower\nflower.pov: the flower \
itself." 9 76
      fi
      echo exiting
      rm -f /tmp/petal*
   fi

done

exit

