#!/bin/bash
#
# pcdtojpg.sh - converts a photo cd into jpg's 
#
# (c) 27 Sep 2004: version 0.0.3 Carsten Brandhorst <cb@qapla.org>
#	http://www.qapla.org/pcdtojpg/
#
# currently limited to a hardcoded resolution of 1536x1024
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

version=0.0.3
current=`pwd`

# check in convert is installed
convert=`which convert`
if [ "${convert}" == "" ];
  then echo "convert not found. Please install ImageMagick.";
       echo "";
       exit 1;
fi

echo ""
echo "pcdtojpg.sh version $version - converts a Photo CD into 1536x1024"
echo " jpgs's and saves them to ~/photocd"
echo ""

# where to put the final jpg's
echo "Plase enter where you would like to store the images (default: ~/photocd/):"
read target
if [ "${target}" = "" ];
  then
    echo "Using default (~/photocd/)";
    target=~/photocd;
fi

# check if the target for the jpgs exists, if not - create it
if test -e ${target};
  then echo "Saving in $target";
else mkdir $target;
  echo "Saving in $target"; 
fi

# let the user select the mountpoint of the drive containing the photocd
echo "Please type in your Photo CD location:"
read location
if [ "${location}" = "" ];
  then 
    echo "You did not specify the Photo CD location, so i will use /cdrom";
    location=/cdrom;
  else if test -e ${location};
	 then echo "Using $location";
	 else echo "$location does not exist. I will use /cdrom";
	      location=/cdrom;
       fi

fi

# check if already mounted
check=`grep $location /etc/mtab`
if [ "${check}" == "" ];
  then mount $location || failed=1;
    if [ "${failed}" == "1" ];
    then echo "You typed in an invalid mountpoint for the Photo CD. Abort.";
	 echo "";
	 exit 1;
    fi
  else if test ! -e $location/photo_cd/images/;
  then echo "mountpoint does not contain a vaild Photo CD. Abort.";
       echo "";
       exit 1;
  fi
fi


# do the convertion
# let the user choose the resolution somewhen
cd $location/photo_cd/images 
for x in *.pcd
do 
  convert -size 1536x1024 "$x" $target/`echo "$x" | cut -d"." -f1`.jpg
  echo "Creating `echo "$x" | cut -d"." -f1`.jpg" 
done
echo "Done."
echo ""

# go back to directory where we started
cd $current

