#!/bin/sh

#Album plugin for Redmine
#2015.01.12 kanata

#setting
IMAGE_PATH="/opt/alminium/public/images/album/a-painter-and-a-black-cat"
IMAGE_CONVERT_PATH="/opt/alminium/public/images/album/a-painter-and-a-black-cat/thumbnail"
IMAGE_OWNER="apache:apache"

#make thumbnail
IMAGE_LIST=`ls ${IMAGE_PATH}/*.jpg | xargs -i basename {}`
if [ `ls ${IMAGE_PATH}|grep -v thumbnail|wc -l` != `ls ${IMAGE_CONVERT_PATH}|wc -l` ]
then
	for WORD in ${IMAGE_LIST}
	do
		if [ ! -r ${IMAGE_CONVERT_PATH}/${WORD%jpg}png ]
		then
			JUDGE_ORG=`identify -ping ${IMAGE_PATH}/${WORD}|awk '{print $3}'`
			JUDGE_W=`echo ${JUDGE_ORG}|cut -d x -f 1`
			JUDGE_H=`echo ${JUDGE_ORG}|cut -d x -f 2`
			if [ ${JUDGE_W} -gt ${JUDGE_H} ]
			then
				convert -geometry 80x60 ${IMAGE_PATH}/${WORD} ${IMAGE_CONVERT_PATH}/${WORD%jpg}png
			else

				convert -geometry 60x80 ${IMAGE_PATH}/${WORD} ${IMAGE_CONVERT_PATH}/${WORD%jpg}png
			fi
		fi
	done
fi

chown ${IMAGE_OWNER} ${IMAGE_PATH}/*.jpg
chown ${IMAGE_OWNER} ${IMAGE_CONVERT_PATH}/*.png

exit 0
