#!/bin/ksh

#
# This file is a part of the NsCDE - Not so Common Desktop Environment
# Author: Hegel3DReloaded
# Licence: GPLv3
#

# Initialize default values for some variables
PrintFnSize=0

if [ -z $NSCDE_ROOT ]; then
   echo "No \$NSCDE_ROOT set. Exiting."
   exit 3
fi

if [ -z $FVWM_USERDIR ]; then
   echo "No \$FVWM_USERDIR set. Exiting."
   exit 4
fi

if [ -z $NSCDE_FONT_DPI ]; then
   NSCDE_FONT_DPI=96
fi

function Defaults
{
   if [ -z $FnSpacing ]; then
      FnSpacing="variable"
   fi

   if [ -z $FnType ]; then
      FnType="normal"
   fi

   if [ -z $FnSize ]; then
      FnSize="medium"
   fi
}

function GetFont
{
   Defaults

   if [ "x$FnFile" == "x" ]; then
      sysfncffile="${NSCDE_ROOT}/config/NsCDE-Font-${NSCDE_FONT_DPI}dpi.conf"
      if [ -s "${FVWM_USERDIR}/NsCDE-Font-${NSCDE_FONT_DPI}dpi.conf" ]; then
         fncnffile="${FVWM_USERDIR}/NsCDE-Font-${NSCDE_FONT_DPI}dpi.conf"
         avoiddoublegrep=0
      else
         fncnffile="$sysfncffile"
         avoiddoublegrep=1
      fi
   else
      if [ -s "$FnFile" ]; then
         fncnffile="$FnFile"
         sysfncnffile="$FnFile"
      else
         echo "Error: Cannot read $FnFile"
         exit 3
      fi
   fi

   RequestedFont="font.${FnSpacing}.${FnType}.${FnSize}"
   Catch=$(egrep "$RequestedFont" "$fncnffile")
   if (($? != 0)); then
      if (($avoiddoublegrep == 1)); then
         Catch=$(egrep "$RequestedFont" "$sysfncffile")
      fi
   fi

   Catch2="${Catch/InfoStoreAdd font./}"
   Catch3="${Catch2#*\"}"
   GetSize=$(echo "${Catch3/\"/}" | $NSCDE_ROOT/bin/ised -c "s/\(.*\)\(size=\)\(.*\)\(.*\)\?/\3/g" -o -f -)

   if [ "x$FnMaxSize" == "x" ]; then
      if (($PrintFnSize == 0)); then
         echo "${Catch3/\"/}"
      else
         echo $GetSize
      fi
   else
      export LC_ALL=C
      echo $GetSize | egrep -q '^([[:digit:]]+)(\.[[:digit:]]+)?$'
      if (($? == 0)); then
         if (($GetSize > $FnMaxSize)); then
            if (($PrintFnSize == 0)); then
               echo "${Catch3/\"/}" | $NSCDE_ROOT/bin/ised -c "s/\(.*\)\(size=\)\([[:digit:]]\+\)\(.*\)\?/\1\2${FnMaxSize}\4/g" -o -f -
            else
               echo $FnMaxSize
            fi
         else
            if (($PrintFnSize == 0)); then
               echo "${Catch3/\"/}"
            else
               echo $GetSize
            fi
         fi
      else
         if (($PrintFnSize == 0)); then
            echo "${Catch3/\"/}"
         else
            echo $GetSize
         fi
      fi
   fi
}

while getopts f:vmt:s:SZ:h Option
do
   case $Option in
      f)
         FnFile="$OPTARG"
      ;;
      d)
         NSCDE_FONT_DPI="$OPTARG"
      ;;
      m)
         FnSpacing="monospaced"
      ;;
      v)
         FnSpacing="variable"
      ;;
      t)
         FnType="$OPTARG"
      ;;
      s)
         FnSize="$OPTARG"
      ;;
      S)
         PrintFnSize=1
      ;;
      Z)
         if [ "$NSCDE_FIX_SCRIPT_FONTSIZE" != "0" ]; then
            FnMaxSize="$OPTARG"
         fi
      ;;
      h)
         echo "Usage: ${0##*/} [ -f <file> ] [ -v(ariable) | -m(onospaced) ] [ -t <normal | bold | italic> ] [ -s <small | medium | large> ] [ -S ] [ -Z maxsize ] [ -h ]"
         exit 0
      ;;
   esac
done

GetFont

