#!/bin/bash


IMG_LIST_FOR_WEB=image_list_for_both.txt
IMG_LIST_FOR_PDF=image_list_for_pdf.txt
PATH_FILE=orig_dir.txt
ERROR_LOG=error.log
FILE_NAME_SUFFIX_LOG=file_name_suffix.log
#rm -f $ERROR_LOG
#bmp :  BMP3
#BMP :  BMP3
#gif :  GIF
#GIF :  GIF
#gif :  JPEG
#jpeg :  JPEG
#jpe :  JPEG
#jpg :  JPEG
#JPG :  JPEG
#png :  JPEG
#PNG :  PNG
#tif :  TIFF
#TIF :  TIFF
TARGET_MAX_WIDTH=520
TARGET_MAX_HEIGHT=720
PIC_DIR=pic
if [ X"$1" = X""  ] ; then
   echo "Please give a dir for parameter"
   exit
else
   echo "to check $1"
fi


if ! [ -d $1  ] ; then
   echo "the dir doesn't exist, exit"
   echo "the dir $1 doesn't exist, exit"  >>  $ERROR_LOG
   exit
fi


if ! [ -e $1/${IMG_LIST_FOR_WEB}  ] ; then
   echo "the image list file: $1/${IMG_LIST_FOR_WEB} doesn't exist, exit"
   echo "the image list file: $1/${IMG_LIST_FOR_WEB} doesn't exist, exit"  >>  $ERROR_LOG
   exit
fi
	    if  [ -d $1/$PIC_DIR ] ;then
                rm -rf $1/$PIC_DIR
            fi
	    if ! [ -e $1/$PIC_DIR ] ;then
		    mkdir $1/$PIC_DIR
	    elif ! [ -d $1/$PIC_DIR ] ;then
		    echo "not a dir,but a file name,exit"
		    echo "not a dir,but a file name,exit"   >>  $ERROR_LOG
		    exit
	    fi


cd $1


rm -f $IMG_LIST_FOR_PDF

PATH_PREFIX=`cat ${PATH_FILE}`
PATH_PREFIX=`echo "$PATH_PREFIX"`
if ! [ -d "$PATH_PREFIX"  ] ; then
   echo "the path of image files: $PATH_PREFIX doesn't exist, exit"
   echo "the path of image files: $PATH_PREFIX doesn't exist, exit"  >> ../$ERROR_LOG
   exit
else
   echo "the path: ${PATH_PREFIX} exist, to check under it"
fi


TOTALNUM=`wc -l $IMG_LIST_FOR_WEB | awk '{print $1}'`

NUM=0

echo "$TOTALNUM files"

while [ $NUM -lt $TOTALNUM ] ; do
    let NUM+=1

    FILENAME=`cat $IMG_LIST_FOR_WEB | sed -n ''$NUM','$NUM' p'`
    echo "${FILENAME}"

    if ! [ -e "$PATH_PREFIX/$FILENAME"  ] ; then
       echo "the image file: $PATH_PREFIX/${FILENAME} doesn't exist, exit"
       echo "the image file: $PATH_PREFIX/${FILENAME} doesn't exist, exit"   >> ../$ERROR_LOG
       exit
    fi

    FILENAME_SUFFIX=`echo "${FILENAME}" | sed -e 's/\(.*\)\.\(.*\)$/\2/g'`
    FILENAME_NO_SUFFIX=`echo "${FILENAME}" | sed -e 's/\(.*\)\.\(.*\)$/\1/g'`

    if [ X"${FILENAME_SUFFIX}" = X"" ] ; then
       echo "the suffix of image file: $PATH_PREFIX/${FILENAME} doesn't exist, exit"
       echo "the suffix of image file: $PATH_PREFIX/${FILENAME} doesn't exist, exit"   >> ../$ERROR_LOG
       exit
    fi

    IMAGE_TYPE=`identify -format "%m" "$PATH_PREFIX/$FILENAME"`
    if [ X"${IMAGE_TYPE}" = X"" ] ; then
       echo "the format of image file: $PATH_PREFIX/${FILENAME} doesn't exist, exit"
       echo "the format of image file: $PATH_PREFIX/${FILENAME} doesn't exist, exit"   >> ../$ERROR_LOG
       exit
    fi

    #echo "$FILENAME_SUFFIX :  $IMAGE_TYPE"  >>   ../${FILE_NAME_SUFFIX_LOG}



    if [ X"$IMAGE_TYPE" = X"JPEG" ]  ||  [ X"$IMAGE_TYPE" = X"PNG" ] ; then
	    DST_FILENAME_SUFFIX=$FILENAME_SUFFIX
	    DST_FILENAME_NO_SUFFIX="$FILENAME_NO_SUFFIX"
    else
	    DST_FILENAME_SUFFIX="png"
	    DST_FILENAME_NO_SUFFIX="$FILENAME_NO_SUFFIX""-rename$NUM"
    fi
    IS_THERE_SPACE=`echo "$FILENAME_NO_SUFFIX" | grep -e " "`
    if [ X"$IS_THERE_SPACE" = X"" ] ; then
	    DST_FILENAME_NO_SUFFIX="$DST_FILENAME_NO_SUFFIX"
    else
	    DST_FILENAME_NO_SUFFIX="$FILENAME_NO_SUFFIX""-rename$NUM"
	    DST_FILENAME_NO_SUFFIX=`echo "$DST_FILENAME_NO_SUFFIX" | sed -e 's/ /-/g'`
    fi
    IS_THERE_DOT=`echo "$FILENAME_NO_SUFFIX" | grep -e "\."`
    if [ X"$IS_THERE_DOT" = X"" ] ; then
	    DST_FILENAME_NO_SUFFIX="$DST_FILENAME_NO_SUFFIX"
    else
	    DST_FILENAME_NO_SUFFIX="$FILENAME_NO_SUFFIX""-rename$NUM"
	    DST_FILENAME_NO_SUFFIX=`echo "$DST_FILENAME_NO_SUFFIX" | sed -e 's/\./-/g'`
    fi
    DST_FILE_NAME="${DST_FILENAME_NO_SUFFIX}"".""${DST_FILENAME_SUFFIX}"
    #echo "$DST_FILE_NAME" 
    #echo "$DST_FILE_NAME"  >>  "$IMG_LIST_FOR_PDF"  


    SRCDIR="$PATH_PREFIX"
    DSTDIR="$PIC_DIR"
    FILE_WH=`identify -format "%wx%h" "$SRCDIR/${FILENAME}"`
    X_POS=`expr index $FILE_WH "x"`
    FILE_W=${FILE_WH:0:$X_POS-1}
    FILE_H=${FILE_WH:$X_POS}

    echo "  $FILE_W x $FILE_H"
    SRC_PATH="$SRCDIR"/"${FILENAME}"
    DST_PATH="$DSTDIR"/"${DST_FILE_NAME}"

    if  ! [ $FILE_W -gt $TARGET_MAX_WIDTH ] && ! [ $FILE_H -gt $TARGET_MAX_HEIGHT ] ; then
      if [ X"$IMAGE_TYPE" = X"JPEG" ]  ||  [ X"$IMAGE_TYPE" = X"PNG" ] ; then
              echo "   small pic of jpg/png, do nothing and copy it"
              cp   "$SRC_PATH"  "$DST_PATH"
      else
              echo "   small pic of NON-jpg/png, convert it"
              convert   "$SRC_PATH"  "$DST_PATH"
      fi
    elif   [ $FILE_W -gt $TARGET_MAX_WIDTH ] && ! [ $FILE_H -gt $TARGET_MAX_HEIGHT ] ; then
              convert -resize   $TARGET_MAX_WIDTH   "$SRC_PATH"  "$DST_PATH"
              DST_FILE_WH=`identify -format "%wx%h" "${DST_PATH}"`
              echo "   converted to be $DST_FILE_WH"
    elif  ! [ $FILE_W -gt $TARGET_MAX_WIDTH ] &&  [ $FILE_H -gt $TARGET_MAX_HEIGHT ] ; then
              convert -resize  x$TARGET_MAX_HEIGHT   "$SRC_PATH"  "$DST_PATH"
              DST_FILE_WH=`identify -format "%wx%h" "${DST_PATH}"`
              echo "   converted to be $DST_FILE_WH"
    elif   [ $FILE_W -gt $TARGET_MAX_WIDTH ] &&   [ $FILE_H -gt $TARGET_MAX_HEIGHT ] ; then
              VAL1=`expr $FILE_H \* $TARGET_MAX_WIDTH`
              VAL2=`expr $FILE_W \* $TARGET_MAX_HEIGHT`

	      if ! [ $VAL1 -gt $VAL2 ] ; then
               convert -resize   $TARGET_MAX_WIDTH   "$SRC_PATH"  "$DST_PATH"
               DST_FILE_WH=`identify -format "%wx%h" "${DST_PATH}"`
               echo "   converted to be $DST_FILE_WH"
	      else
               convert -resize x$TARGET_MAX_HEIGHT   "$SRC_PATH"  "$DST_PATH"
               DST_FILE_WH=`identify -format "%wx%h" "${DST_PATH}"`
               echo "   converted to be $DST_FILE_WH"
	      fi

    fi

    if  [ -e "$DST_PATH" ] ; then
        echo "$DST_FILE_NAME"  >>  "$IMG_LIST_FOR_PDF"  
    else
        echo "convert $SRC_PATH failed"  >> ../$ERROR_LOG
    fi

done
