910e435
#!/bin/bash
910e435
910e435
_SYSCONFDIR=${sysconfdir:-/etc}
910e435
_PREFIX=${prefix:-/usr}
910e435
_DATADIR=${datadir:-${_PREFIX}/share}
910e435
910e435
ADDIR=${ADDIR:-${install_prefix}${_SYSCONFDIR}/xscreensaver}
910e435
CONFDIR=${CONFDIR:-${install_prefix}${_DATADIR}/xscreensaver/hacks.conf.d}
910e435
ADFILE=${ADFILE:-$ADDIR/XScreenSaver.ad}
910e435
910e435
fix_hackconf ()
910e435
{
910e435
	if [ ! -s $1 ] ; then
5d19ea9
		echo "$1 is empty" >&2
910e435
		echo ""
910e435
		return 1
910e435
	fi
910e435
910e435
	tmpconf=`mktemp /tmp/hackconf.XXXXXXXX`
910e435
	tmpconf_1=`mktemp /tmp/hackconf.XXXXXXXX`
910e435
	tmpconf_2=`mktemp /tmp/hackconf.XXXXXXXX`
910e435
	tmpconf_3=`mktemp /tmp/hackconf.XXXXXXXX`
910e435
	sed -n -e '$p' $1 > $tmpconf_1
910e435
	sed -e '$d' $1 > $tmpconf_2
910e435
	for file in $tmpconf_1 $tmpconf_2 ; do
910e435
		if ( [ -s $file ] && grep -q '\\n\(\|\\\)[^\\].*$' $file ) ; then
910e435
			rm -f $tmpconf $tmpconf_1 $tmpconf_2 $tmpconf_3
910e435
			echo ""
910e435
			echo "ignoring $1" >&2
910e435
			return 1
910e435
		fi
910e435
	done
1dc16b2
	if ! grep -q '\\n[\\]*$' $tmpconf_1 ; then
1dc16b2
		sed -i -e 's|\(^.*$\)|\1 \\n\\|' $tmpconf_1
910e435
	fi
910e435
	sed -i -e 's|\\n$|\\n\\|' $tmpconf_1
910e435
	while [ -s $tmpconf_2 ] ; do
910e435
		sed -n -e '1p' $tmpconf_2 > $tmpconf_3
910e435
		sed -i -e '1d' $tmpconf_2
5d19ea9
		if ! grep -q '\([ \t]\\$\|\\n\\$\)' $tmpconf_3 ; then
910e435
			rm -f $tmpconf $tmpconf_1 $tmpconf_2 $tmpconf_3
910e435
			echo ""
910e435
			echo "ignoring $1" >&2
910e435
			return 1
910e435
		fi
910e435
		cat $tmpconf_3 >> $tmpconf
910e435
	done
910e435
	cat $tmpconf_1 >> $tmpconf
910e435
	rm -f $tmpconf_1 $tmpconf_2 $tmpconf_3
910e435
	echo $tmpconf
910e435
	return 0
910e435
	
910e435
}
910e435
910e435
for suffix in header tail ; do
910e435
	if [ ! -r $ADDIR/XScreenSaver.ad.$suffix ] ; then
910e435
		echo "$ADDIR/XScreenSaver.ad.$suffix missing"
910e435
		exit 1
910e435
	fi
910e435
done
910e435
910e435
tmpfile=`mktemp /tmp/XScreenSaver.ad.XXXXXXXX`
5fd4999
4555d1c
cat > $tmpfile <
4555d1c
! Don't edit this file directly by yourself!!
4555d1c
! This file is not meant to be edited directly.
4555d1c
!
4555d1c
! Instread, please edit /etc/xscreensaver/XScreenSaver.ad.header,
4555d1c
! /etc/xscreensaver/XScreenSaver.ad.tail and add files under
4555d1c
! /usr/share/xscreensaver/hacks.conf.d if you want.
4555d1c
!
4555d1c
! Then call /usr/sbin/update-xscreensaver-hacks to 
4555d1c
! update this file.
4555d1c
!
4555d1c
EOF
5fd4999
5fd4999
cat $ADDIR/XScreenSaver.ad.header >> $tmpfile
910e435
910e435
for hackconf in $CONFDIR/*.conf ; do
910e435
	hackconf_fixed=`fix_hackconf $hackconf`
910e435
	if [ -n "$hackconf_fixed" ] ; then
1dc16b2
		cat $hackconf_fixed >> $tmpfile
910e435
		rm -f $hackconf_fixed
910e435
	fi
910e435
done
910e435
sed -i -e '$s|\\n\\$|\\n|' $tmpfile
910e435
910e435
cat $ADDIR/XScreenSaver.ad.tail >> $tmpfile
910e435
install -c -p -m 644 $tmpfile $ADFILE
910e435
rm -f $tmpfile
910e435
910e435