#!/bin/sh
#(c) Copyright Barry Kauler March 2013, bkhome.org.
#License GPL3 2013 (see /usr/share/doc/legal).
#hack to fix font sizes. Puppy has moved from 78 to 96 dpi.
#can be called from a running puppy, standalone, from ppm, or from woof.
#called from 3builddistro (woof), or /usr/local/petget/hacks-postinstall.sh.
#130326 first release.
#130329 changed stat --format=%Y to stat -c %Y, so busybox applet will work.

GTKRCS=""
JWMTHEMES=""
ROXPREFIX=""
RUNNINGIN="standalone"
[ -d ../rootfs-skeleton ] && [ -d rootfs-complete ] && RUNNINGIN="woof"
[ $1 ] && RUNNINGIN="single" #maybe from ppm when install a package.
case $RUNNINGIN in
 standalone) #execute in a running puppy, no passed param.
  GTKRCS="`ls /usr/share/themes/*/gtk-2.0/gtkrc | grep -v ' ' | tr '\n' ' '`"
  XRESFILE="/root/.Xresources"
  JWMTHEMES="`ls /root/.jwm/themes/*-jwmrc | tr '\n' ' '` /root/.jwm/jwmrc-theme"
  ROXPREFIX="/"
 ;;
 woof)
  #can be run in Woof, called from 3builddistro...
  GTKRCS="`ls rootfs-complete/usr/share/themes/*/gtk-2.0/gtkrc | grep -v ' ' | tr '\n' ' '`"
  XRESFILE="rootfs-complete/root/.Xresources"
  JWMTHEMES="`ls rootfs-complete/root/.jwm/themes/*-jwmrc | tr '\n' ' '` rootfs-complete/root/.jwm/jwmrc-theme"
  ROXPREFIX="rootfs-complete/"
 ;;
 single) #called from ppm.
  eval "$@" #ex: GTKRCS=/usr/share/themes/Citrus/gtk-2.0/gtkrc
  XRESFILE="/root/.Xresources"
 ;;
esac
JWMPREFIX="$ROXPREFIX"

XFTDPI=`grep '^Xft.dpi:' ${XRESFILE} | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`
if [ $XFTDPI -ge 96 ];then

 if [ "$GTKRCS" ];then
  #fix gtk2 themes...
  for AGTKRC in $GTKRCS
  do
   echo -n "" > /tmp/agtkthemefixed
   #MODIFYSECS=`stat --format=%Y $AGTKRC`
   MODIFYSECS=`stat -c %Y $AGTKRC` #130329
   #only hack old themes...
   if [ $MODIFYSECS -lt 1364287598 ];then #2013, March 26, 4.47pm
    echo "Fixing: ${AGTKRC}"
    cat ${AGTKRC} |
    while read ALINE
    do
     ALINEx="$(echo -n "$ALINE" | sed -e 's%Sans 11%Sans 9%' -e 's%Sans 12%Sans 10%' -e 's%Sans 13%Sans 11%' -e 's%Sans 14%Sans 12%')"
     echo "$ALINEx" >> /tmp/agtkthemefixed
    done
    sync
    cp -f /tmp/agtkthemefixed ${AGTKRC}
   fi
  done
 fi
 
 #fix jwm themes...
 if [ "$JWMTHEMES" ];then
  for AJWMTHEME in $JWMTHEMES
  do
   echo -n "" > /tmp/ajwmthemefixed
   #MODIFYSECS=`stat --format=%Y ${AJWMTHEME}`
   MODIFYSECS=`stat -c %Y ${AJWMTHEME}` #130329
   if [ $MODIFYSECS -lt 1364287598 ];then #2013, March 26, 4.47pm
    echo "Fixing: ${AJWMTHEME}"
    cat $AJWMTHEME |
    while read ALINE
    do
     ALINEx="$(echo -n "$ALINE" | sed -e 's%Sans-11%Sans-9%' -e 's%Sans-12%Sans-10%' -e 's%Sans-13%Sans-11%' -e 's%Sans-14%Sans-12%' -e 's%Sans-15%Sans-13%' -e 's%Sans-16%Sans-14%')"
     echo "$ALINEx" >> /tmp/ajwmthemefixed
    done
    sync
    cp -f /tmp/ajwmthemefixed $AJWMTHEME
   fi
  done
 fi

 if [ "$JWMPREFIX" ];then
  if [ -f ${JWMPREFIX}etc/xdg/templates/_root_.jwmrc ];then
   echo -n "" > /tmp/jwmrctemplatefixed
   #MODIFYSECS=`stat --format=%Y ${JWMPREFIX}etc/xdg/templates/_root_.jwmrc`
   MODIFYSECS=`stat -c %Y ${JWMPREFIX}etc/xdg/templates/_root_.jwmrc`
   if [ $MODIFYSECS -lt 1364287598 ];then #2013, March 26, 4.47pm
    echo "Fixing: ${JWMPREFIX}etc/xdg/templates/_root_.jwmrc"
    cat ${JWMPREFIX}etc/xdg/templates/_root_.jwmrc |
    while read ALINE
    do
     ALINEx="$(echo -n "$ALINE" | sed -e 's%Sans-11%Sans-9%' -e 's%Sans-12%Sans-10%' -e 's%Sans-13%Sans-11%' -e 's%Sans-14%Sans-12%' -e 's%Sans-15%Sans-13%' -e 's%Sans-16%Sans-14%')"
     echo "$ALINEx" >> /tmp/jwmrctemplatefixed
    done
    sync
    cp -f /tmp/jwmrctemplatefixed ${JWMPREFIX}etc/xdg/templates/_root_.jwmrc
    if [ "$RUNNINGIN" = "standalone" ];then
     fixmenus
     #jwm -restart
    fi
   fi
  fi
 fi
  
 #fix rox-filer...
 if [ "$ROXPREFIX" ];then
  echo -n "" > /tmp/aroxthemefixed
  if [ -f ${ROXPREFIX}root/.config/rox.sourceforge.net/ROX-Filer/Options ];then
   #MODIFYSECS=`stat --format=%Y ${ROXPREFIX}root/.config/rox.sourceforge.net/ROX-Filer/Options`
   MODIFYSECS=`stat -c %Y ${ROXPREFIX}root/.config/rox.sourceforge.net/ROX-Filer/Options`
   if [ $MODIFYSECS -lt 1364287598 ];then #2013, March 26, 4.47pm
    echo "Fixing: ${ROXPREFIX}root/.config/rox.sourceforge.net/ROX-Filer/Options"
    cat ${ROXPREFIX}root/.config/rox.sourceforge.net/ROX-Filer/Options |
    while read ALINE
    do
     ALINEx="$(echo -n "$ALINE" | sed -e 's%Sans 11%Sans 9%' -e 's%Sans 12%Sans 10%' -e 's%Sans 13%Sans 11%' -e 's%Sans 14%Sans 12%')"
     echo "$ALINEx" >> /tmp/aroxthemefixed
    done
    sync
    cp -f /tmp/aroxthemefixed ${ROXPREFIX}root/.config/rox.sourceforge.net/ROX-Filer/Options
   fi
  fi
 fi

 if [ "$RUNNINGIN" = "standalone" ];then
  echo "Please restart X to see changes."
 fi

fi

###END###
