#!/bin/sh
# Skript which is executed when the powerbutton is pressed
EJECT_CMD="eject /dev/cdrom"
CLOSE_CMD="eject -t /dev/cdrom"

EJTMP="/tmp/~ej"

ACTDATE=`date +%s`
if [ -f $EJTMP ] ; then
   FDATE=`ls -l --time-style=+%s $EJTMP | tr -s ' ' |cut -f6 -d ' '`
   DIFF=$(($ACTDATE - $FDATE))
else
   DIFF=999999
fi
NOCD=`cdctl -g | grep "There is no "`
if [ "$NOCD" = "" ] | [ $DIFF -le 180 ] ; then
   logger -s "CLOSE_CD sent"
   $CLOSE_CMD
   rm $EJTMP
else
   logger -s "EJECT_CD sent"
   umount /mnt/cdrom > /dev/null 2>&1
   umount /mnt/cdfs > /dev/null 2>&1
   $EJECT_CMD
   touch $EJTMP
fi
