#!/bin/ash
#Barry Kauler 2009
#the background image is at /usr/share/backgrounds/default.jpg
#this may have the wrong dimensions to suit either a 'normal' or a 'widescreen' monitor.
#for now, keep this simple, assume image is suitable for normal screen, truncate vertically
#if widescreen.
#v424 fix maths.
#v424 move ORIGINAL-* images to /usr/share/backgrounds_original
#110831 support png images.
#111013 Karl Godt: 'dc' can return , instead of . in numeric values, for non-english locale.
#130326 Karl Godt: Added MODE option to be able to swith between math by BK and math by /bin/sh.
#130327 Doubble Quotes around Files and Directories to support Spaces.
#130327 Karl Godt: Logfile is /tmp/backg.log.
#130327 wjaguar: Rewrote unnecessarily tortuous math & string handling

LOG=/tmp/backg.log
exec 1>>$LOG 2>&1
: << 'COMMENTED_OUT' # Mode not used anymore
[ "$DEBUG" ] && echo "DEBUG=$DEBUG" 
#below set this if wanted (un-comment by deleting the prceeding # (number-sign):
#MODE=old 
[ "$MODE" ] || {
	case "$1" in
	"-m" ) MODE=$2 ; shift 2 ;;
	"-m"* ) MODE=${1#-m} ; shift 1 ;;
	"--mode="* ) MODE=${1#--mode=} ; shift 1 ;;
	esac
	FILENAME=$1
}
echo "MODE=$MODE"
echo "FILENAME=$FILENAME"
COMMENTED_OUT

#accept optional image file as passed param...
[ -f /usr/share/backgrounds/default.png ] && IMAGEFILE="/usr/share/backgrounds/default.png"
[ -f /usr/share/backgrounds/default.jpg ] && IMAGEFILE="/usr/share/backgrounds/default.jpg"
#[ $1 ] && IMAGEFILE="$1"
[ "$*" ] && IMAGEFILE="$@"
[ "$IMAGEFILE" = "" ] && exit
echo "IMAGEFILE=$IMAGEFILE"
#v423 precaution, if someone has chosen the backup original...
[ "$IMAGEFILE" != "${IMAGEFILE#ORIGINAL}" ] && exit

[ -f "$IMAGEFILE" ] || { echo "$0: $IMAGEFILE is not a file ..?"; exit; }

IMAGEPATH=${IMAGEFILE%/*}
[ "$IMAGEPATH" = "$IMAGEFILE" ] && IMAGEPATH=`pwd`
mkdir -p "${IMAGEPATH}_original" #v424
IMAGEBASE=${IMAGEFILE##*/}
case $IMAGEBASE in
*.jpg|*.jpeg|*.JPG|*.JPEG) IMAGEPNM='jpegtopnm'
	PNMIMAGE="pnmtojpeg -quality=85" ;;
*.png|*.PNG) IMAGEPNM='pngtopnm'
	PNMIMAGE="pnmtopng" ;;
*) echo "$0: Only support for PNG and JPG files" 
	exit ;;
esac
echo "IMAGEPATH=$IMAGEPATH IMAGEBASE=$IMAGEBASE IMAGEPNM=$IMAGEPNM"

# Safety check
which $IMAGEPNM >/dev/null 2>&1 || { echo "No $IMAGEPNM utility!" ; exit ; }
which ${PNMIMAGE%% *} >/dev/null 2>&1 || { echo "No $PNMIMAGE utility!" ; exit ; }

#first boot, maybe trying different video modes, so this causes reset each time...
if [ ! -f "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" ];then #v424
	cp -f "$IMAGEFILE" "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" #v424
else
	cp -f "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" "$IMAGEFILE" #restore. v424
fi

read IMAGEHORIZ IMAGEDIMS IMAGEVERT IMAGEDIMS << EOF
`$IMAGEPNM "$IMAGEFILE" 2>/dev/null | pamfile 2>&1 | grep 'stdin:' | cut -f 2 -d ','`
EOF
echo "IMAGEHORIZ=$IMAGEHORIZ IMAGEVERT=$IMAGEVERT"

read ROOTDIMS ROOTHORIZ ROOTVERT << EOF
`xwininfo -root | grep ' \-geometry ' | cut -f 1 -d '+' | tr 'x' ' '`
EOF

#v423 some math gymnastics involved here..
IMGVERTSCALED=$(( ( $ROOTHORIZ * $IMAGEVERT ) / $IMAGEHORIZ ))
NEWVERT=$(( ( $IMAGEHORIZ * $ROOTVERT ) / $ROOTHORIZ )) #v424 fix.
 
echo "IMGVERTSCALED=$IMGVERTSCALED NEWVERT=$NEWVERT" #TEST

#this method cuts equal amount from top and bottom...
if [ "$NEWVERT" -gt "0" ] && [ $NEWVERT -lt $IMAGEVERT ] #precaution
then
	PAMCUT=pamcut
	which $PAMCUT >/dev/null 2>&1 || PAMCUT=pnmcut #older netpbm.
	which $PAMCUT >/dev/null 2>&1 || exit # no netpbm at all
	CUTVERT=$(( $IMAGEVERT - $NEWVERT ))
	$IMAGEPNM "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" | \
		$PAMCUT -left=0 -top=$(( $CUTVERT / 2 )) -height=$NEWVERT | \
		$PNMIMAGE > "$IMAGEFILE"
fi

###END###
