#!/bin/sh

# This script runs fvwm with fvwm-themes config.  It should be run under X.
# Put a call to this script as the last line of your .Xclients or .xinitrc.

prefix=/usr/pkg
: ${ROOT_PREFIX=$DESTDIR}
: ${ROOT_PREFIX=}

: ${FVWM_USERDIR=$HOME/.fvwm}
rcName=themes-rc
rcFile=$FVWM_USERDIR/$rcName
rcName2=themes-rc-2
rcFile2=$FVWM_USERDIR/$rcName2
themesName=themes
themesDir=$FVWM_USERDIR/$themesName
currentThemeName=current
currentThemeDir=$themesDir/$currentThemeName
startDir=`pwd`
isNewSession=0

# Arguments
fvwmArgs=""
whoseArgs="my"
start=yes
### to be autoconfigured
fvwmExe="fvwm"
session="main"

while [ "x$1" != "x" ]; do
	case "$1" in
	--)
		whoseArgs="fvwm" ;;
	*)
		if [ "$whoseArgs" = "fvwm" ]; then
			fvwmArgs="$fvwmArgs $1"
		else
			case "$1" in
			--no-start|-no-start|-n)
				start=no ;;
			--fvwm|-fvwm|--fvwm-exe|-fvwm-exe|-e)
				shift
				fvwmExe="$1" ;;
			--session|-session|-s)
				# fvwm-2.2.x adds -s on 'Restart fvwm-themes-start'
				if [ $# -gt 1 ]; then
					shift
	 				if [ "$1" = "--" ]; then
						whoseArgs="fvwm"
					else
						session="$1"
					fi
				fi ;;
			esac
		fi ;;
	esac
	shift
done

realCurrentThemeName="current-$session"
realCurrentThemeDir="$themesDir/$realCurrentThemeName"
destRcFile2="$themesName/$realCurrentThemeName/$rcName2"

[ -d $FVWM_USERDIR ] || mkdir $FVWM_USERDIR
# create the symbolic links
# POSIX says that we can have symlink to a not existing file.
# However some 2.3.x Linux kernels and may be 2.4.x do not support this???
[ -d $themesDir ] || mkdir $themesDir
[ -d $themesDir/personal ] || mkdir $themesDir/personal
cd $themesDir || echo "Fatal error, can't chdir $themesDir"
if [ ! -d $realCurrentThemeName ]; then
	mkdir $realCurrentThemeName
	isNewSession=1
fi
rm -rf $currentThemeName
ln -s $realCurrentThemeName $currentThemeName
cd $FVWM_USERDIR
if [ ! -f $destRcFile2 ]; then
	touch $destRcFile2
	isNewSession=1
fi
rm -f $rcName2
ln -s $destRcFile2 $rcName2

HEADERS_LINE=""
test -f $rcFile && HEADERS_LINE="`head -1 $rcFile 2>/dev/null`"

# check whether this is the first run
test "`echo $HEADERS_LINE | cut -d' ' -f3`" = "0.7.0"
isNewVersion="$?"
if [ "$isNewVersion" = 1 -o "$isNewSession" = 1 ]; then
	if [ "$isNewVersion" = 1 ]; then
		echo '[FVWM-Themes]: Welcome to FVWM Themes 0.7.0!'
		xmessage -g 400x100+0+0 -xrm "*form*background:rgb:c0/c0/a0" -xrm "*form*okay*background:rgb:90/90/7a" -xrm "*textSink*font:lucidasans-18" -xrm "*form*message*background:rgb:f0/f0/c0" 'Welcome to FVWM Themes 0.7.0!' 2>/dev/null &
	else
		echo "[FVWM-Themes]: Initial $session FVWM Themes session"
	fi
	${prefix}/bin/fvwm-themes-config --reset
	${prefix}/bin/fvwm-themes-config --load @personal 2>/dev/null
else
	# different build id may mean that themes or prefix were changed,
	# be safe and refresh in this case
	if [ "`echo $HEADERS_LINE | cut -d' ' -f4-`" != "build 20-Dec-2025 12:52:08" ]; then
		echo "[FVWM-Themes]: A new 0.7.0 build detected; refreshing configuration"
		${prefix}/bin/fvwm-themes-config --fresh
	fi
fi

# check that /usr/pkg/bin is in $PATH and first for fvwm-config
if [ "`which fvwm-config 2>/dev/null`" != "/usr/pkg/bin/fvwm-config" ]; then
	echo "[FVWM-Themes]: There is no correct fvwm-config in your PATH; prepended"
	PATH="/usr/pkg/bin:$PATH"
	export PATH
fi

# check that ${prefix}/bin is in $PATH and first for fvwm-themes-config
if [ "`which fvwm-themes-config 2>/dev/null`" != "${prefix}/bin/fvwm-themes-config" ]; then
	echo "[FVWM-Themes]: There is no correct fvwm-themes-config in your PATH; prepended"
	PATH="${prefix}/bin:$PATH"
	export PATH
fi

cd $startDir
echo "[FVWM-Themes]: Starting FVWM under the $session fvwm-themes session"
if [ "$start" = "yes" ]; then
	exec $fvwmExe -f "$rcName" $fvwmArgs
fi
