#! /usr/bin/wishx

# This program was written by Jerome ALET - <alet@unice.fr> to learn
# the beautiful Tcl/Tk language. You can redistribute it under the terms
# of the GNU General Public Licence of the Free Software Foundation.
# You can read the GNU GPL version 2.0 in the file COPYING which must
# be distributed with this program. 

# Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

#
# display an error message in a dialog box
proc error_message { message } {
        tk_dialog .mywindow "Xeject: Error" $message "" 0 "OK"
}

#
# if we find that /dev/cdrom or /mnt/cdrom is mounted or not
# we activate the mount or eject button and we disable the other one
proc test_cdrom_present {} {
        #        
        # no match found
        set found 0
        #
        # open the mounted filesystems file /etc/mtab
        set fileid [open /etc/mtab]
        #
        # we create a search context
        set fctid [scancontext create]
        #
        # in this search context, each time the word "cdrom" 
        # appears we increment found by one
        scanmatch $fctid cdrom { incr found }
        #
        # we begin the scan
        scanfile $fctid $fileid
        #
        # now we can delete the search context and close the file
        scancontext delete $fctid
        close $fileid
        #
        # if found then we activate the mount button
        # else we activate the eject button
        if {$found != 0} {
                activate_mount_button
        } else {
                activate_eject_button
        }        
}

#
# nothing to say
proc activate_eject_button {} {
        .eject configure -text "CDROM Dismounted"
        .eject configure -state disabled
        .mount configure -text "Mount the CDROM"
        .mount configure -state normal
}

#
# nothing to say
proc activate_mount_button {} {
        .eject configure -text "Eject the CDROM"
        .eject configure -state normal
        .mount configure -text "CDROM Mounted"
        .mount configure -state disabled
}

#
# we unmount the mounted cdrom, then we eject it
proc eject_cdrom {} {
        #
        # we don't want tcl/tk error messages
        catch {
                #
                # we unmount /mnt/cdrom
                exec /bin/umount /mnt/cdrom >& /dev/null
        } error_umount
        if { $error_umount != "" } {
                #
                # an error occured
                error_message "Error while Unmounting CDROM from /mnt/cdrom"
        }
        catch {
                #
                # we eject the default cdrom /dev/cdrom
                exec /usr/bin/eject >& /dev/null
        } error_eject
        if { $error_eject != "" } {
                #
                # an error occured
                error_message "Error while Ejecting CDROM /dev/cdrom"
        }
        #
        # now we test if the unmount operation was successfull
        test_cdrom_present
}

#
# we mount the default cdrom /dev/cdrom to the default directory /mnt/cdrom
proc mount_cdrom {} {
        #
        # we don't want tcl/tk error messages
        catch {
                #
                # we mount the default cdrom /dev/cdrom
                # in the /mnt/cdrom directory
                exec /bin/mount -t iso9660 /dev/cdrom /mnt/cdrom >& /dev/null
        } error_mount
        if { $error_mount != "" } {
                #
                # an error occured
                error_message "Error while Mounting CDROM /dev/cdrom on /mnt/cdrom"
        }
        #
        # now we test if the mount operation was successfull
        test_cdrom_present
}

# 
# we create the two buttons
button .eject
# we bind the click action to the eject_cdrom procedure
.eject configure -command eject_cdrom
.eject configure -disabledforeground red

#
# just read above
button .mount
.mount configure -command mount_cdrom
.mount configure -disabledforeground red

#
# we must test the initial state
test_cdrom_present

#
# we display the buttons and wait
pack .eject .mount
