#!/bin/bash

BASE=`pwd | tr " " "_"`
BASE=`basename $BASE`

echo -n "Suffix type of images (png/jpg/...) ? "
read SUFF
if test "$SUFF" = "" 
then
  SUFF=png
fi

echo -n "Radix name of pages ? "
read RADIX

echo -e "\
/* Xpaint-image */\n\
\n\
#define PERCENT 100.0\n\
#define GRAY 1\n\
#define PAPERX 596\n\
#define PAPERY 842\n\
#define SHIFTX 0.0       /* percent of page width */\n\
#define SHIFTY 0.0       /* percent of page height */\n\
\n\
#include <Xpaint.h>\n\
\n\
typedef struct \n\
{   \n\
  float wpercent, hpercent;\n\
  int wbbox, hbbox, width, height, rx, ry, wsubdiv, hsubdiv, orient, gray;\n\
}\n\
PageInfo;\n\
\n\
\n\
/* \n\
 * The key-word \"ImageCreate\" is reserved for user-defined image routines;\n\
 * Such a routine should create and process an image.\n\
 * \n\
 * Pixels are unsigned char arrays p[0]=red, p[1]=green, p[2]=blue\n\
 * (thus each value should be in the range 0..255)\n\
 *\n\
 * In the example below, op = output pixel.\n\
 * The procedure below just creates an image by mapping given functions\n\
 * as red, green, blue components...\n\
 */\n\
\n\
Image * ImageCreate()\n\
{\n\
    Image * input;\n\
    \n\
    PageInfo pinfo;\n\
\n\
    input = ImageFromFile(\"xpaint_input\");\n\
   \n\
    /* Don't forget this otherwise xpaint may crash if input does not exist */\n\
    if (!input) \n\
    {\n\
       	       printf(\"Read failure : 'xpaint_input' !!\0134n\");\n\
               exit(0);\n\
    }\n\
     \n\
    pinfo.width = input->width;\n\
    pinfo.height = input->height;\n\
    pinfo.rx = (int) (SHIFTX * 0.01 * (float)PAPERX);\n\
    pinfo.ry = (int) (SHIFTY * 0.01 * (float)PAPERY);\n\
    pinfo.wsubdiv = 1;\n\
    pinfo.hsubdiv = 1;\n\
    pinfo.orient = 0;\n\
    pinfo.wpercent = PERCENT * (float)PAPERX/((float)pinfo.width);\n\
    pinfo.hpercent = PERCENT * (float)PAPERY/((float)pinfo.height);\n\
    printf(\"Image size : %d x %d\", pinfo.width, pinfo.height);\n\
    printf(\"  -->  resizing  %4.2f %% x %4.2f %%\0134n\",\n\
	   pinfo.wpercent, pinfo.hpercent);\n\
    pinfo.wbbox = PAPERX;\n\
    pinfo.hbbox = PAPERY;\n\
    pinfo.gray = 0;\n\
\n\
    WriteResizedPS(\"xpaint_output.ps\", input, &pinfo);\n\
    exit(0);\n\
    return NULL;\n\
}\n\
" > /tmp/xpaint_print_script.c

emacs -fn 9x15 /tmp/xpaint_print_script.c

if test "$GRAY" = "o" || test "$GRAY" = "O" ;
then
  GRAY="O"
else
  GRAY="N"
fi

FIRST=First
COUNT=0
i=0

echo "Starting $BASE.pdf ..."
echo ""

while [ 1 ] 
do
  if test "$[$i<10]" == "1"
  then
    j=0$i
    k=00$i
  else
    j=$i
    if test "$[$i<100]" == "1"
    then
      k=0$i
    else
      k=$i
    fi
  fi
  FILE="$RADIX"$i.$SUFF
  if ! [ -r "$FILE" ]
  then
    FILE="$RADIX"$j.$SUFF  
  fi
  if ! [ -r "$FILE" ]
  then
    FILE="$RADIX"$k.$SUFF  
  fi 
  if ! [ -r "$FILE" ]
  then
     COUNT=$[$COUNT+1]
     if test "$[$COUNT<20]" == "1"
     then
        i=$[$i+1]	
        continue	
     else
        echo "No more pages seem to be there. Job over."
	echo ""
	echo "Check file $BASE.pdf"
	echo ""	
	break
     fi
  fi  
  COUNT=0
  echo "Xpaint : processing $FILE  as PDF file ..."
  rm -f xpaint_input  
  ln -s "$FILE" xpaint_input
  xpaint -iconic /tmp/xpaint_print_script.c 2>/dev/null
  mv xpaint_output.ps __page_$j.ps
  ps2pdf __page_$j.ps
  rm -f __page_$j.ps  __page_new*

  if test "$FIRST" == "First"
  then
    FIRST=""
    mv -f __page_$j.pdf $BASE.pdf
    PREV=__page_$j
  else
    mv -f $BASE.pdf $PREV.pdf
    pdfconcat -o $BASE.pdf $PREV.pdf __page_$j.pdf 2>&1 1>/dev/null
    PREV=__page_$FIRST-$j
  fi
  i=$[$i+1]
done

rm -f __page*
rm -f xpaint_input
rm -f /tmp/xpaint_print_script.c