#!/bin/sh - # # eject -- Eject a CD under FreeBSD. # # Oliver Fromme # # BSD-style copyright and standard disclaimer applies. # Usage() { cat <<-tac >&2 Usage: ${0##*/} [-n] [scsi | ide | ] Default: Look for a SCSI drive first and eject that one. If there is none, the first IDE drive is ejected. Options: -n Only show the command that would be executed. tac exit 1 } GetSCSI() { camcontrol devlist 2>/dev/null \ | awk ' ($NF ~ /,'"$1"'[^0-9]/) { print substr($(NF - 5), 6) ":" $(NF - 3) ":" $(NF - 1); } ' } GetIDE() { for i in 0 1 2 3; do atacontrol info $i 2>/dev/null done \ | awk ' ($2 ~ /^'"$1"'$/) { print $2; } ' } export PATH=/bin:/sbin:/usr/bin:/usr/sbin if [ "$1" = "-n" ]; then SHOWONLY=true shift else SHOWONLY=false fi if [ $# -gt 1 ]; then Usage fi case "$1" in [Ss][Cc][Ss][Ii]|[Ss]) CDDRV=$(GetSCSI 'cd[0-9]+') SCSI=true ;; /dev/cd[0-9]|cd[0-9]) CDDRV=$(GetSCSI "${1##*/}") if [ -n "$CDDRV" ]; then SCSI=true else echo "${0##*/}: No such SCSI drive \"${1##*/}\"." >&2 echo "Maybe you meant \"a${1##*/}\" (IDE)?" >&2 exit 1 fi ;; [Ii][Dd][Ee]|[Ii]|[Aa][Tt][Aa]|[Aa]|[Aa][Tt][Aa][Pp][Ii]) CDDRV=$(GetIDE 'acd[0-9]+') SCSI=false # "atacontrol info" doesn't work as non-root. :-( # In that case use acd0 and hope it's right. if [ -z "$CDDRV" ]; then CDDRV="acd0" fi ;; /dev/acd[0-9]|acd[0-9]) CDDRV="${1##*/}" SCSI=false ;; "") CDDRV=$(GetSCSI 'cd[0-9]+') if [ -n "$CDDRV" ]; then SCSI=true else CDDRV=$(GetIDE 'acd[0-9]+') SCSI=false # "atacontrol info" doesn't work as non-root. :-( # In that case use acd0 and hope it's right. if [ -z "$CDDRV" ]; then CDDRV="acd0" fi fi ;; *) Usage ;; esac if [ -z "$CDDRV" ]; then echo "${0##*/}: No CD-ROM drive found." >&2 exit 1 fi if $SCSI; then if $SHOWONLY; then echo camcontrol eject $CDDRV else exec camcontrol eject $CDDRV fi else if $SHOWONLY; then echo cdcontrol -f $CDDRV eject else exec cdcontrol -f $CDDRV eject fi fi #--