#!/bin/bash
read mdz fractal settings version
if [[ "xmdz" != "x$mdz" || "xfractal" != "x$fractal" || "xsettings" != "x$settings" || "x0.1.2" != "x$version" ]]
then
  echo "ERROR not an mdz settings file" 1>&2
  exit 1
fi
read comment
if [ "x# http://jwm-art.net/mdz/" != "x$comment" ]
then
  echo "ERROR unrecognized, no jwm comment" 1>&2
  exit 1
fi
read settings
if [ "xsettings" != "x$settings" ]
then
  echo "ERROR not an mdz settings file" 1>&2
  exit 1
fi
read family mandelbrot
if [[ "xfamily" != "x$family" || "xmandelbrot" != "x$mandelbrot" ]]
then
  echo "ERROR not family mandelbrot" 1>&2
  exit 1
fi
read fractal mandelbrot
if [[ "xfractal" != "x$fractal" || "xmandelbrot" != "x$mandelbrot" ]]
then
  echo "ERROR not fractal mandelbrot" 1>&2
  exit 1
fi
read depth Iterations
if [[ "xdepth" != "x$depth" ]]
then
  echo "ERROR no depth" 1>&2
  exit 1
fi
read aspect AspectRatio
if [[ "xaspect" != "x$aspect" ]]
then
  echo "ERROR no aspect" 1>&2
  exit 1
fi
read colourscale ColourScale
if [ "xcolour-scale" != "x$colourscale" ]
then
  echo "ERROR no colour-scale" 1>&2
  exit 1
fi
read colourinterpolate ColourInterpolate
if [ "xcolour-interpolate" != "x$colourinterpolate" ]
then
  echo "ERROR no colour-interpolate" 1>&2
  exit 1
fi
read multiprecision
read multirounding
read precision
read cx Re
if [[ "xcx" != "x$cx" && "x#cx" != "x$cx" ]]
then
  echo "ERROR no cx" 1>&2
  exit 1
fi
read cy Im
if [[ "xcy" != "x$cy" && "x#cy" != "x$cy" ]]
then
  echo "ERROR no cy" 1>&2
  exit 1
fi
Im="-$Im"
Im="${Im#--}"
read size Radius
if [[ "xsize" != "x$size" && "x#size" != "x$size" ]]
then
  echo "ERROR no size" 1>&2
  exit 1
fi
read xmin
read xmax
read ymax
read paletteoffset PaletteOffset
if [ "xpalette-offset" != "x$paletteoffset" ]
then
  echo "ERROR no palette-offset" 1>&2
  exit 1
fi
palette=""
while [ "xpalette" != "x$palette" ]
do
  read palette
done
read data
Colors=""
ColorsNext=""
PaletteCount=0
if [[ "xpalette" == "x$palette" && "xdata" == "x$data" ]]
then
  while read R G B
  do
    if [[ "x" != "x$R$G$B" ]]
    then
      PaletteCount="$((PaletteCount + 1))"
      Colors="${ColorsNext}"
      ColorsNext="$ColorsNext$B,$G,$R,"
    fi
  done
  PaletteCount="$((PaletteCount - 1))"
  IterDiv="$(ghci -e "${PaletteCount} / 1024 / ${ColourScale}")"
  ColorOffset="$(ghci -e "mod (floor (${PaletteOffset} * 1024 / ${PaletteCount})) 1024")"
else
  echo "WARNING unrecognized palette, skipping" 1>&2
  ColorOffset=0
  Colors="255,255,255,0,0,255,0,0,0,255,0,0,"
  IterDiv=1
fi
Zoom="$(ghci -e "4.0/${Radius}")"
if [[ "xyes" == "x$ColourInterpolate" ]]
then
  Smooth=1
  Flat=0
else
  Smooth=0
  Flat=1
fi
Size=1440
ImageWidth="$(ghci -e "round (${Size} * sqrt ${AspectRatio})")"
ImageHeight="$(ghci -e "round (${Size} / sqrt ${AspectRatio})")"
if (( ImageWidth > ImageHeight ))
then
  Zoom="$(ghci -e "${Zoom} * ${AspectRatio}")"
fi
cat << EOF | sed "s/$/\r/g"
Re: $Re
Im: $Im
Zoom: $Zoom
Iterations: $Iterations
IterDiv: $IterDiv
SmoothMethod: 0
SmoothingMethod: 0
BailoutRadiusPreset: 1
BailoutRadiusCustom: 2
BailoutNormPreset: 1
BailoutNormCustom: 2
ColorMethod: 0
Differences: 0
ColorOffset: $ColorOffset
ColorPhaseStrength: 0
Colors: $Colors
InteriorColor: 0,0,0,
Smooth: $Smooth
Flat: $Flat
MultiColor: 0
BlendMC: 0
MultiColors: 
Power: 2
FractalType: 0
Slopes: 0
SlopePower: 50
SlopeRatio: 20
SlopeAngle: 45
real: 1
imag: 1
SeedR: 0
SeedI: 0
FactorAR: 1
FactorAI: 0
Period: 0
TextureEnabled: 0
TextureMerge: 1
TexturePower: 200
TextureRatio: 100
TextureFile: 
RotateAngle: 0
StretchAngle: 0
StretchAmount: 0
Version: 2150100
ImageWidth: $ImageWidth
ImageHeight: $ImageHeight
JitterSeed: 1
EOF
