#!/bin/sh

# This script is run as root before showing login widget.
# Emplacement des datas:
#
# Partie importante pour la location des fichiers	
configdir="/root/xdm"
themedir="Config/xdm.theme"
bgdir="/root/xdm/Backgrounds"
bg=""
bg1="juliettetaka2.jpg"
bg2="Debian_beautiful.jpg"
bg3="Jessie.jpg"
# Utilisable:
#bg4=""
#bg5=""
#bg6=""
#...
# Fin location
# On gnre un nombre
bgset=`date +%N | sed 's/^0*//g'`
bgset=$(($bgset*10/999999999));
# Fin nombre
XCOMPMGR=`pidof xcompmgr`
XSET=0
sed_fn()
{
# les commandes sed doivent vous inciter  faire attention aux squences de caractere dans les fichiers:
# Ex: "WIDGET_POS<espace>num". si non a ne marche pas.
# Prendre en compte un changement effectif:
 sed \
	-e "s/\(WIDGET_POS \)[0-9]/\1$1/" \
	$configdir/$themedir -i.bak
  echo "LastBackground: $2" > $configdir/Background.tmp
}
if pidof xcompmgr
then
  killall -9 xcompmgr
fi
# Probleme avec les dual screen, xinerama.
#su thomas -c "xrandr -q1 --current &>/home/thomas/xrandr.txt"
#[ -z $(su thomas -c xrandr | sed -n 's/^HDMI1 \(connected\).*/\1/p') ] \
#  && su thomas -c "xrandr --output HDMI1 --off"

# Les encadrements permettent la selection
# partie commente non teste, c'est la partie encore en plus bas dans un switch
#case $bgset
#in
#7|8|9|10)	sed_fn 1 "$bg2"
#;;
#3|4|5|6)	sed_fn 0 "$bg1"
#;;
#0|1|2)		sed_fn 2 "$bg3"
#;;
#esac
if [ $bgset -gt 6 ]
then
  sed_fn 1 "$bg2"
elif [ $bgset -gt 2 ]
then
  sed_fn 0 "$bg1"
else
  sed_fn 2 "$bg3"
fi
bg="$(sed 's/LastBackground: //' $configdir/Background.set)"
hsetroot -fill $bgdir/$bg
# Pour l'apparence avec bgset < 6
# seul bg2 n'utilise pas xcompmgr
#if [ $bg = $bg3 ] || [ $bg = $bg1 ]
#then
  if which xcompmgr
  then
    xcompmgr &
    #if [ $bg = $bg3 ]
    #then
#	XSET=0.25
    #else
#	XSET=0.45
#    fi
    /usr/bin/transset-df -n xlogin 0.35
  fi
#fi
# Le changement est effectu, au prochain dmarrage a se vera.
mv $configdir/Background.tmp $configdir/Background.set
