#!/bin/ksh

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

# NsCDE: colormgr shell script
# ColorMgr FvwmScript backgroud worker

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

# Defaults for -t and -e.
PreviewMode=0
EchoInfo=0

# Load common functions
if [ -f "${NSCDE_ROOT}/libexec/style_managers.shlib" ]; then
   . ${NSCDE_ROOT}/libexec/style_managers.shlib
else
   echo "Error: cannot source ${NSCDE_ROOT}/libexec/style_managers.shlib."
   exit 1
fi

#
# Local ColorMgr specific functions
#

# Sanity check while importing new palette
function CheckPalette
{
   egrep '^#[[:xdigit:]]{12}' $1 | wc -l
}

# Generate full 40 colors from the palette in 80x74 icon presented in ColorMgr
function GenFullPalette
{
   PalettePath="$1"
   mkdir -p ${FVWM_USERDIR}/tmp || exit 1

   if [ -s "${NSCDE_ROOT}/lib/progbits/FullPalette.xpm" ]; then
      ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PalettePath} -n $Ncolors \
       -i ${NSCDE_ROOT}/lib/progbits/FullPalette.xpm \
       -c > ${FVWM_USERDIR}/tmp/_fullpalette.xpm

      echo ${FVWM_USERDIR}/tmp/_fullpalette.xpm
   else
      echo "Error: Cannot open ${NSCDE_ROOT}/lib/progbits/FullPalette.xpm"
      exit 3
   fi
}

# In Apply mode, generate colors in pixmaps which are program elements (-P)
function ProgBits
{
   # Notice about WSM* pixmaps:
   # South East margin of WSM buttons implemented as icon of ItemDraw.
   # We cannot use rectangle in dynamic desk WSM

   mkdir -p ${FVWM_USERDIR}/icons/NsCDE || exit 1
   mkdir -p ${FVWM_USERDIR}/icons/CDE || exit 1

   # Type b, subtype 2
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -o ${FVWM_USERDIR}/icons/NsCDE -g xpm \
       -i ${NSCDE_ROOT}/lib/progbits/type_b2 \
       -P 2 -b

   # Type b, subtype 3
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -o ${FVWM_USERDIR}/icons/NsCDE -g xpm \
       -i ${NSCDE_ROOT}/lib/progbits/type_b3 \
       -P 3 -b

   # Type b, subtype 5
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -o ${FVWM_USERDIR}/icons/NsCDE -g xpm \
       -i ${NSCDE_ROOT}/lib/progbits/type_b5 \
       -P 5 -b

   # Type b, subtype 6
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -o ${FVWM_USERDIR}/icons/NsCDE -g xpm \
       -i ${NSCDE_ROOT}/lib/progbits/type_b6 \
       -P 6 -b

   # Type b, subtype 7
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -o ${FVWM_USERDIR}/icons/NsCDE -g xpm \
       -i ${NSCDE_ROOT}/lib/progbits/type_b7 \
       -P 7 -b

   # Type c
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -o ${FVWM_USERDIR}/icons/NsCDE -g xpm \
       -i ${NSCDE_ROOT}/lib/progbits/type_c \
       -c

   # Freedesktop icon type (type c)
   mkdir -p ""${HOME}"/.icons/NsCDE/tmp"

   # actions
   mkdir -p "${HOME}"/.icons/NsCDE/{48x48,32x32,24x24,22x22,16x16}/actions
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
    -n $Ncolors -p ${PaletteNamePath} \
    -o "${HOME}/.icons/NsCDE/tmp" -g xpm \
    -i ${NSCDE_ROOT}/lib/progbits/icon-theme/actions \
    -c
   if (($? == 0)); then
      for xpmf in ${HOME}/.icons/NsCDE/tmp/*.xpm
      do
         xpmf="${xpmf##*/}"
         desthint="${xpmf%%-*}"
         namehint="${xpmf#*-}"
         mv "${HOME}/.icons/NsCDE/tmp/${xpmf}" "${HOME}/.icons/NsCDE/${desthint}/actions/${namehint}"
      done
   fi

   # status
   mkdir -p "${HOME}"/.icons/NsCDE/{48x48,32x32,24x24,22x22,16x16}/status
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
    -n $Ncolors -p ${PaletteNamePath} \
    -o "${HOME}/.icons/NsCDE/tmp" -g xpm \
    -i ${NSCDE_ROOT}/lib/progbits/icon-theme/status \
    -c
   if (($? == 0)); then
      for xpmf in ${HOME}/.icons/NsCDE/tmp/*.xpm
      do
         xpmf="${xpmf##*/}"
         desthint="${xpmf%%-*}"
         namehint="${xpmf#*-}"
         mv "${HOME}/.icons/NsCDE/tmp/${xpmf}" "${HOME}/.icons/NsCDE/${desthint}/status/${namehint}"
      done
   fi

   rmdir "${HOME}/.icons/NsCDE/tmp"

   # Front Panel Stock CDE icons
   for fpicon in Fphome.l.pm Fpterm.l.pm Fpprnt.l.pm \
       Fpapps.l.pm Fphelp.l.pm FpCM.l.pm Fppenpd.l.pm \
       Fpstyle.l.pm
   do
      ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -i ${NSCDE_ROOT}/share/icons/CDE/$fpicon -P 5 \
       -b > ${FVWM_USERDIR}/icons/CDE/$fpicon
   done

   # Default fallback background
   ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
    -p ${PaletteNamePath} -n $Ncolors \
    -i ${NSCDE_ROOT}/share/templates/BGdefault -P 3 \
    -b > ${FVWM_USERDIR}/.BGdefault
}

# Regenerate X resources database file
function Xresources
{
   mkdir -p ${FVWM_USERDIR}/tmp
   if (($PreviewMode == 0)); then
      ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -i ${NSCDE_ROOT}/share/templates/Xdefaults \
       -c > ${FVWM_USERDIR}/Xdefaults

      if [ ! -s "${FVWM_USERDIR}/Xdefaults.fontdefs" ]; then
         cat ${NSCDE_ROOT}/share/templates/Xdefaults.fontdefs > "${FVWM_USERDIR}/Xdefaults.fontdefs"
      fi

      if [ ! -s "${FVWM_USERDIR}/Xdefaults.mouse" ]; then
         cat ${NSCDE_ROOT}/share/templates/Xdefaults.mouse > "${FVWM_USERDIR}/Xdefaults.mouse"
      fi

      if [ ! -f "${FVWM_USERDIR}/Xdefaults.local" ]; then
         mkdir -p "${FVWM_USERDIR}"
         touch "${FVWM_USERDIR}/Xdefaults.local"
      fi

      mkdir -p ${FVWM_USERDIR}/app-defaults

      ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -o "${FVWM_USERDIR}/app-defaults" -g tmpl \
       -i ${NSCDE_ROOT}/share/templates/app-defaults \
       -c
      for tmpl in ${FVWM_USERDIR}/app-defaults/*.tmpl
      do
         mv "$tmpl" $(echo "$tmpl" | sed 's/\.tmpl//g')
      done

      if [ -d "${FVWM_USERDIR}/templates/app-defaults" ]; then
         ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
          -p ${PaletteNamePath} -n $Ncolors \
          -o "${FVWM_USERDIR}/app-defaults" -g tmpl \
          -i "${FVWM_USERDIR}/templates/app-defaults" \
          -c
         for tmpl in ${FVWM_USERDIR}/app-defaults/*.tmpl
         do
            mv "$tmpl" $(echo "$tmpl" | sed 's/\.tmpl//g')
         done
      fi
   else
      ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -i ${NSCDE_ROOT}/share/templates/Xdefaults \
       -c > ${FVWM_USERDIR}/tmp/Xdefaults-Preview

      if (($EchoInfo == 1)); then
         echo "${FVWM_USERDIR}/tmp/Xdefaults-Preview"
      fi
   fi
}

# Regenerate user's NsCDE-Colorset.conf
function FvwmColorsets
{
   if (($PreviewMode == 0)); then
      ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -f > ${FVWM_USERDIR}/NsCDE-Colorset.conf
   else
      mkdir -p ${FVWM_USERDIR}/tmp || exit 1

      ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
       -p ${PaletteNamePath} -n $Ncolors \
       -f > ${FVWM_USERDIR}/tmp/NsCDE-Colorset-Preview.conf

      if (($EchoInfo == 1)); then
         echo "${FVWM_USERDIR}/tmp/NsCDE-Colorset-Preview.conf"
      fi
   fi
}

# Sync existing backdrops according to new palette and number of colors
function Backdrops
{
   mkdir -p ${FVWM_USERDIR}/backdrops || exit 1

   # Initial
   if [ ! -s "${FVWM_USERDIR}/NsCDE-Backdrops.conf" ]; then
      mkdir -p "${FVWM_USERDIR}/tmp"
      FvwmCommand 'Exec exec echo $[infostore.desknum] > "$[FVWM_USERDIR]/tmp/_ndesks"'
      sync
      sleep 1
      DeskNo=$(<"${FVWM_USERDIR}/tmp/_ndesks")
      rm "${FVWM_USERDIR}/tmp/_ndesks"
      mkdir -p "${FVWM_USERDIR}/backer"
      > "${FVWM_USERDIR}/NsCDE-Backdrops.conf"
      while (($DeskNo != 0))
      do
         egrep "^Colorset 3${DeskNo}" "${NSCDE_ROOT}/config/NsCDE-Backdrops.conf" | \
         sed 's/\$\[NSCDE_ROOT\]\/share\/defaults\/backer/\$\[FVWM_USERDIR\]\/backer/g' \
          >> "${FVWM_USERDIR}/NsCDE-Backdrops.conf"
         cp -f ${NSCDE_ROOT}/share/defaults/backer/Desk${DeskNo}-*pm "${FVWM_USERDIR}/backer/"
         ((DeskNo = DeskNo - 1))
      done
   fi

   # Desks: 31 32 33 34 35 36 37 38
   # Variants: 3, 5, 6, 7, 3, 5, 6, 7
   if [ -s "${FVWM_USERDIR}/NsCDE-Backdrops.conf" ]; then
      egrep '^Colorset 3(1|2|3|4|5|6|7|8) TiledPixmap \$\[FVWM_USERDIR\]\/' ${FVWM_USERDIR}/NsCDE-Backdrops.conf | \
       sort -k2 -n | sed 's/\.pm$//g' | while IFS=" " read csetmark deskcset pixmark backdrop
      do
          basebackdrop="${backdrop##*/}"

          if (($Ncolors == 4)); then
             Variant=3
          else
             case $deskcset in
                31) Variant=3 ;;
                32) Variant=5 ;;
                33) Variant=6 ;;
                34) Variant=7 ;;
                35) Variant=3 ;;
                36) Variant=5 ;;
                37) Variant=6 ;;
                38) Variant=7 ;;
             esac
          fi

          GetBackdrop "${basebackdrop##*Desk?-}"
          if (($PreviewMode == 0)); then
             ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
              -p ${PaletteNamePath} -n $Ncolors \
              -i ${BackdropNamePath} -P $Variant \
              -b > ${FVWM_USERDIR}/backer/${basebackdrop}.pm
          else
             EchoInfo=0
             mkdir -p ${FVWM_USERDIR}/tmp || exit 1
             ${NSCDE_ROOT}/libexec/nscde_palette_colorgen.py \
              -p ${PaletteNamePath} -n $Ncolors \
              -i ${BackdropNamePath} -P $Variant \
              -b > ${FVWM_USERDIR}/tmp/backer-${basebackdrop}.pm
             echo ${FVWM_USERDIR}/tmp/backer-${basebackdrop}.pm
          fi
      done
   fi
}

# Main getopt loop.
# Options:
# -C is for checking pallete sanity
# -c is for specifying number of colors to use (4 or 8)
# -t is for preview performance (places temporary files in FVWM_USERDIR/tmp
# -e is echo mode - information for FvwmScript ColorMgr and BackdropMgr
# -p generates palette menu
# -g retrives palette path, files variables and echoes if in echo mode
# -n returns palette name by value number from FvwmScript List widget
# -N opposite of -n
# -f Generates dynamic icon with all colors from the palette in ColorMgr
# -F Generates Fvwm Colorset definitions in user's NsCDE-Colorset.conf
# -P Regenerates in user's icon/NsCDE dir bits and pieces which are part of fvwm programs
# -b Syncronizes existing backdrops with new palette color sheme
# -X Syncronizes X resources file with new palette color sheme
# -h Help
while getopts C:c:tepg:n:N:f:FPbXh Option
do
   case $Option in
      C)
         CheckPalette $OPTARG
      ;;
      c)
         Ncolors="$OPTARG"
      ;;
      t)
         export PreviewMode=1
      ;;
      e)
         export EchoInfo=1
      ;;
      p)
         GeneratePaletteMenu
      ;;
      g)
         GetPalette "$OPTARG"
      ;;
      n)
         GetPaletteByNumber $OPTARG
      ;;
      N)
         GetPaletteByName $OPTARG
      ;;
      f)
         GenFullPalette $OPTARG
      ;;
      F)
         FvwmColorsets
      ;;
      P)
         if (($PreviewMode == 0)); then
            ProgBits
         else
            echo "Not performing pixmap modification in preview mode."
         fi
      ;;
      b)
         Backdrops
      ;;
      X)
         Xresources
      ;;
      h)
         echo "Usage: ${0##*/} <options> ..."
      ;;
   esac
done

