#!/bin/ash
#Barry Kauler 2009
#called from /usr/sbin/set_bg, /root/.xinitrc, /usr/local/sfsget/dir2sfs
#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 switch between math by BK and math by /bin/sh.
#130327 Double Quotes around Files and Directories to support Spaces.
#130327 Karl Godt: Logfile is /tmp/backg.log.
#130327 wjaguar: Rewrote unnecessarily tortuous math & string handling
#140101 BK: leave originals in /usr/share/backgrounds. create subdirs for truncated images. see also set_bg, .xinitrc.
#140107 fix if image not in subdir.
#20220215 read /etc/uimanager. cut sides if img ratio wider.
#20220222 fix jwm wallpaper path is wrong.
#20220309 fix exit.

LOG=/tmp/backg.log
exec 1>>$LOG 2>&1

. /etc/uimanager #20220215 UI_DESK_MANAGER='jwm' #or rox

#get current image...
#this will be: /usr/share/backgrounds/<aspect ratio>/<image file>
curIMAGEFILE=""
if [ $1 ];then
 curIMAGEFILE="$1"
elif [ "$UI_DESK_MANAGER" == "jwm" ];then #20220215
 curIMAGEFILE="$(grep '<Background' /root/.jwm/jwmrc-personal | cut -f 2 -d '>' | cut -f 1 -d '<')"
fi
[ ! "$curIMAGEFILE" ] && [ -f /root/Choices/ROX-Filer/PuppyPin ] && curIMAGEFILE="`grep '<backdrop' /root/Choices/ROX-Filer/PuppyPin | cut -f 2 -d '>' | cut -f 1 -d '<'`"
[ ! "$curIMAGEFILE" ] && [ -s /root/.config/wallpaper/bg_img ] && curIMAGEFILE="`cat /root/.config/wallpaper/bg_img`"
[ ! "$curIMAGEFILE" ] && curIMAGEFILE="`ls -1 /usr/share/backgrounds/default.* | head -n 1`"
[ ! "$curIMAGEFILE" ] && curIMAGEFILE="`ls -1 /usr/share/backgrounds/*.jpg | head -n 1`"
IMAGEBASE=${curIMAGEFILE##*/}
[ ! -f /usr/share/backgrounds/$IMAGEBASE ] && cp -f "$curIMAGEFILE" /usr/share/backgrounds/ #precaution.

read ROOTDIMS ROOTHORIZ ROOTVERT << EOF
`xwininfo -root | grep ' \-geometry ' | cut -f 1 -d '+' | tr 'x' ' '`
EOF
#create subdir for truncated images...
ASPECTRATIO=`expr $ROOTHORIZ \* 100 \/ $ROOTVERT` #as a percentage. ex: 1280x800 screen is 160%
mkdir -p /usr/share/backgrounds/${ASPECTRATIO}

if [ -f /usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE} ];then
 #truncated image already exists.
 echo "Already exists: /usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE}"
 echo "/usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE}" > /tmp/qwallpaper_reshaped #tell set_bg, this is the one to use.
 
 #20220222 jwm or rox may have wrong path...
 if [ "$UI_DESK_MANAGER" == "jwm" ];then
  grep -q "<Background.*/${ASPECTRATIO}/" /root/.jwm/jwmrc-personal
  if [ $? -ne 0 ];then
   exit 11 #see /root/.xinitrc, will update /root/.jwm/jwmrc-personal
  fi
 fi
 if [ "$UI_DESK_MANAGER" == "rox" ];then
  grep -q "<backdrop.*/${ASPECTRATIO}/" /root/Choices/ROX-Filer/PuppyPin
  if [ $? -ne 0 ];then
   exit 11 #see /root/.xinitrc, will update /root/Choices/ROX-Filer/PuppyPin
  fi
 fi
 
 exit 10
fi

case $IMAGEBASE in
 *.jpg|*.jpeg|*.JPG|*.JPEG)
  IMAGEPNM='jpegtopnm'
  PNMIMAGE="pnmtojpeg -quality=95" #20220215 bump from 85
 ;;
 *.png|*.PNG)
  IMAGEPNM='pngtopnm'
  PNMIMAGE="pnmtopng"
 ;;
 *)
  echo "Only support for PNG and JPG files" 
  exit 4
 ;;
esac

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

read IMAGEHORIZ IMAGEDIMS IMAGEVERT IMAGEDIMS << EOF
`$IMAGEPNM /usr/share/backgrounds/$IMAGEBASE 2>/dev/null | pamfile 2>&1 | grep 'stdin:' | cut -f 2 -d ','`
EOF

#20220215 find out whether img needs to be truncated vert or horiz...
imgASPECTRATIO=`expr $IMAGEHORIZ \* 100 \/ $IMAGEVERT` #ex: 1280x800 image is 160%

if [ $imgASPECTRATIO -gt $ASPECTRATIO ];then #20220215
 #cut equal amount from each side...
 NEWHORIZ=$(( ( $IMAGEVERT * $ROOTHORIZ ) / $ROOTVERT ))
 if [ "$NEWHORIZ" -gt "0" ] && [ $NEWHORIZ -lt $IMAGEHORIZ ] #precaution
 then
  PAMCUT=pamcut
  which $PAMCUT >/dev/null 2>&1 || PAMCUT=pnmcut #older netpbm.
  which $PAMCUT >/dev/null 2>&1 || exit 7 # no netpbm at all
  CUTHORIZ=$(( $IMAGEHORIZ - $NEWHORIZ ))
  $IMAGEPNM "/usr/share/backgrounds/$IMAGEBASE" | \
		$PAMCUT -top=0 -left=$(( $CUTHORIZ / 2 )) -width=$NEWHORIZ | \
		$PNMIMAGE > /usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE}
  fsync /usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE}
 fi
elif [ $imgASPECTRATIO -eq $ASPECTRATIO ];then #20220215
 if [ ! -f /usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE} ];then
  cp -a -f /usr/share/backgrounds/$IMAGEBASE /usr/share/backgrounds/${ASPECTRATIO}/
 fi
 #echo "/usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE}" > /tmp/qwallpaper_reshaped #set_bg will read this.
 #exit 10 #nothing to do. 20220309 fix.
else
 #v423 some math gymnastics involved here..
 IMGVERTSCALED=$(( ( $ROOTHORIZ * $IMAGEVERT ) / $IMAGEHORIZ ))
 NEWVERT=$(( ( $IMAGEHORIZ * $ROOTVERT ) / $ROOTHORIZ )) #v424 fix.
 #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 7 # no netpbm at all
  CUTVERT=$(( $IMAGEVERT - $NEWVERT ))
  $IMAGEPNM "/usr/share/backgrounds/$IMAGEBASE" | \
		$PAMCUT -left=0 -top=$(( $CUTVERT / 2 )) -height=$NEWVERT | \
		$PNMIMAGE > /usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE}
  fsync /usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE} #20220215
 fi
fi

#140107 fix if image not in subdir...
if [ ! -f /usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE} ];then
 echo "/usr/share/backgrounds/${IMAGEBASE}" > /tmp/qwallpaper_reshaped #set_bg will read this.
else
 echo "/usr/share/backgrounds/${ASPECTRATIO}/${IMAGEBASE}" > /tmp/qwallpaper_reshaped #set_bg will read this.
fi
exit 11

###END###
