#!/bin/sh

. /opt/ds/etc/ds.env

XORG_LOG=/var/log/Xorg.0.log
DS_X_LOG=$LOGDIR/ds.X.log
DS_X_ERR=$LOGDIR/ds.X.err

if [ -z "$DSDIR" ]; then
    echo "No \$DSDIR defined, aborting"
    exit 1
fi

for i in {1..10}; do # 5secs
    [ ! -s $X_LOCK ] && sleep 0.5 && continue
    PID=`cat $X_LOCK`
    kill -s 0 $PID >/dev/null 2>&1
    [ $? -eq 0 ] && break
    sleep 0.1
done

\cp -b -S .bak $XORG_LOG $DS_X_LOG # temporarily bypass an alias
if [ -e $DS_X_LOG ]; then
    [ -e $DS_X_ERR ] && \mv $DS_X_ERR $DS_X_ERR.bak 2>/dev/null
    cat $DS_X_LOG 2>/dev/null | egrep "WW|EE" > $DS_X_ERR
fi

exit 0
