#-----------------------------------------------------------------------
# File:         fns-init
# Version:      1.0.6
# Licence:      GPL 2
#
# Description:  start file of Fvwm-Nightshade
#
# Author:       Thomas Funk <t.funk@web.de>
# Created:      09/07/2012
# Changed:      12/13/2014
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Set Menu icon size dependent of .FvwmForm-Desktop-Config
#-----------------------------------------------------------------------
DestroyFunc SetMenuIconSize
AddToFunc   SetMenuIconSize
+ I PipeRead "if test -f ${FVWM_USERDIR}/.fns-menu.cfg; then \
                Size=`cat ${FVWM_USERDIR}/.fns-menu.cfg |grep IconSize| awk '{ print $$$3 }'`; \
              else \
                Size=`cat ${FNS_SYSTEMDIR}/.fns-menu.cfg |grep IconSize| awk '{ print $$$3 }'`; \
              fi; \
              if test -z $Size; then \
                echo InfoStoreAdd MenuIconSize 24x24; \
              else \
                echo InfoStoreAdd MenuIconSize ${Size}x${Size}; \
              fi"

SetMenuIconSize

#-----------------------------------------------------------------------
# Prints a debug message in .xsession-errors
#-----------------------------------------------------------------------
DestroyFunc FuncPrintDbgMsg
AddToFunc   FuncPrintDbgMsg
+ I Test (EnvMatch FNS_DEBUG 1) PipeRead `echo "[Fvwm-Nightshade][Dbg]: $0" >> ${HOME}/.xsession-errors`

#-----------------------------------------------------------------------
# Prints a message in .xsession-errors
#-----------------------------------------------------------------------
DestroyFunc FuncPrintMsg
AddToFunc   FuncPrintMsg
+ I PipeRead `echo "[Fvwm-Nightshade][$0]: $1" >> ${HOME}/.xsession-errors`

#-----------------------------------------------------------------------
# This function looks for specified file in the user's directory (~/.fvwm-nightshade/),
# then system directory (/usr/[local]/share/fvwm-nightshade) and loads 
# the first one it finds. 
#-----------------------------------------------------------------------
DestroyFunc Load
AddToFunc Load
+ I Test (f $[FVWM_USERDIR]/$0) FuncPrintMsg "Load" "$0 found in userdir"
+ I TestRc (Match) Read $[FVWM_USERDIR]/$0
+ I TestRc (Match) FuncPrintMsg "Load" "read of $0 in userdir finished"
+ I TestRc (Match) Break 1
+ I Test (f $[FNS_SYSTEMDIR]/$0) FuncPrintMsg "Load" "$0 found in systemdir"
+ I TestRc (Match) Read $[FNS_SYSTEMDIR]/$0
+ I TestRc (Match) FuncPrintMsg "Load" "read of $0 in systemdir finished"
+ I TestRc (Match) Break 1
+ I Test (f $0) FuncPrintMsg "Load" "$0 found"
+ I TestRc (Match) Read $0
+ I TestRc (Match) FuncPrintMsg "Load" "read of $0 finished"
+ I TestRc (Match) Break 1
+ I TestRc (!Match) FuncPrintMsg "Load" "$0 not found"

#-----------------------------------------------------------------------
# This function set FNS_THEMEDIR. First it looks in the user's directory (~/.fvwm-nightshade/),
# then system directory (/usr/[local]/share/fvwm-nightshade) if the used theme is there
#-----------------------------------------------------------------------
DestroyFunc FuncSetThemeDir
AddToFunc FuncSetThemeDir
+ I Test (f $[FVWM_USERDIR]/themes/$0) SetEnv FNS_THEMEDIR $[FVWM_USERDIR]/themes/$0
+ I TestRc (!Match) Test (f $[FNS_SYSTEMDIR]/themes/$0) SetEnv FNS_THEMEDIR $[FNS_SYSTEMDIR]/themes/$0

#-----------------------------------------------------------------------
# This function replaces the Restart function of Fvwm to ensure that
# while restarting the Fvwm-Nightshade init script will launched also
#-----------------------------------------------------------------------
DestroyFunc FuncRestart
AddToFunc FuncRestart
+ I Restart fvwm -f $[FNS_INITFILE]

#-----------------------------------------------------------------------
# This function specifies a colon separated list of directories in which 
# to search for sounds.
# Further, a '+' in the path is expanded to the previous value of the path,
# allowing appending or prepending to the path easily.
# It will add the new paths to $FNS_SOUNDPATH environment variable
#-----------------------------------------------------------------------
DestroyFunc FnsSoundPath
AddToFunc   FnsSoundPath
+ I SetEnv newPaths $0
+ I PipeRead 'if [ `echo $newPaths |grep -e ^+: |grep -e :+\$` ]; then \
                echo SetEnv newPaths `echo $newPaths |sed -e "s#:+##"`; \
              fi'
+ I PipeRead 'echo SetEnv FNS_SOUNDPATH `echo $newPaths |sed -e "s#+#$FNS_SOUNDPATH#"`'
#+ I PipeRead 'export FNS_SOUNDPATH=`echo $newPaths |sed -e "s#+#$FNS_SOUNDPATH#"`'
+ I UnsetEnv newPaths

#-----------------------------------------------------------------------
# Load main config
#-----------------------------------------------------------------------
Load config


