#! /bin/sh

xss_ad_dir=/etc/X11/xscreensaver
xss_ad=/etc/X11/app-defaults/XScreenSaver

exit_handler()
{
    local rc=$?
    trap '' EXIT
    [ -z "$tmpfile" ] || rm -f "$tmpfile"
    exit $rc
}

trap exit_handler SIGHUP SIGINT SIGQUIT SIGTERM EXIT

tmpfile="$(mktemp "$xss_ad.XXXXXXXXXX")"

[ -f "$xss_ad_dir/xscreensaver.top" ] && cat "$xss_ad_dir/xscreensaver.top" > "$tmpfile"
for i in $xss_ad_dir/hack.d/*.xss; do
    [ -f "$i" ] && cat "$i" >> "$tmpfile"
done
[ -f "$xss_ad_dir/xscreensaver.bottom" ] && cat "$xss_ad_dir/xscreensaver.bottom" >> "$tmpfile"

mv -f "$tmpfile" "$xss_ad" || exit 1

trap '' EXIT
exit 0
