#!/bin/bash
# The script is copyrighted under the GPL v3. 2014 by Dominique Michel.
# It is not part of FVWM-Crystal and no gvaranty of any kind is made.
# You can freely redistribute it and export it as long you comply with the terms of the GPL v.3 license.
#
# pdf2jpg: Convert all the pdf files in the current directory into separated jpg files.
#
# usage : 'pdf2jpg' to get some help
#
# With 'pdf2jpg r'each pictures of <name*>.pdf will be extracted into a serie of files of
# the form <name*>-nnn.jpg
# where nnn is the file number beginning at 000 for the first picture.
# Much faster in many cases but it will not do the text and in some cases, 
# you will get unwanted results with the pictures.
#
# 'pdf2jpg p" will extract the pages as they are appearing in a pdf viewer.
#
# 'pdf2jpg rot <angle>' will rotate all the jpeg files by "angle" degrees.
# 'pdf2jpg f' will flip the jpeg files.
# Usefull in conjunction with the r option.
#
# Be aware that this scipt can create a huge amount of huge temporary files.
# That implies that you can run out of diskspace.
#
# dependency : imagemagick, xpdf or poppler

# Convert the pdf
convertpdf() {
	ls | grep .pdf | while read i
	do
	    echo "Converting $i to pictures using pdftoppm."
	    name=`basename "$i" .pdf`
	    name="$name "
	    pdftoppm "$i" "$name"
	done

}

convertraw() {
	ls | grep .pdf | while read i
	do
	    echo "Extracting the raw pictures from $i."
	    name=`basename "$i" .pdf`
	    pdfimages -j "$i" "$name"
	done

}

# Convert the ppm pics if any
convertpic() {
	ls | grep .ppm | while read i
	do
	    name=`basename "$i" .ppm`
	    echo "Converting $i to $name.jpg"
	    convert "$i" "$name".jpg
	    echo "Deleting $i"
	    rm "$i"
	done
	ls | grep .pbm | while read i
	do
	    name=`basename "$i" .pbm`
	    echo "Converting $i to $name.jpg"
	    convert "$i" "$name".jpg
	    echo "Deleting $i"
	    rm "$i"
	done
	ls | grep .pgm | while read i
	do
	    name=`basename "$i" .pgm`
	    echo "Converting $i to $name.jpg"
	    convert "$i" "$name".jpg
	    echo "Deleting $i"
	    rm "$i"
	done
	
}

# flip the pics vertically
flippic() {
	ls | grep .jpg | while read i
	do
	    echo "Flipping $i"
	    convert -flip "$i" "$i"
	done
}

# rotate the pics
rotatepic() {
	ls | grep .jpg | while read i
	do
	    echo "Rotate $i"
	    convert -rotate "$ANGLE" "$i" "$i"
	done
}

# usage
usage() {
echo "Usage: `basename $0` p will extract as it the whole content of the pdf files in the current directory."
echo "Usage: `basename $0` r will extract the raw pictures from the pdf files in the current directory."
echo "Usage: `basename $0` f will flip the jpeg files in the current directory."
echo "Usage: `basename $0` rot <angle> will rotate the jpeg files in the current directory by <angle> degres clockwise."
exit 65
}

# # # # # # # # # # # # # # # # # # # # # # # # # # #
# Do something now
if [ -z "$1" ]
then
  usage
fi
if [[ "$1" = "p" ]]; then
    convertpdf
    convertpic
fi
if [[ "$1" = "r" ]]; then
    convertraw
    convertpic
fi
if [[ "$1" = "f" ]]; then
    flippic
fi
if [[ "$1" = "rot" ]]; then
    ANGLE="$2"
    rotatepic
fi
