#!/bin/bash 
# set -vex
PATH=$PATH:/usr/local/lib/inx
. inx_essentials
maybe_switch

white ; clear
fig-col
figlet Cdrom Access

cdrom_cleanup ()
{
    rm -r $cdromtemp
}

not_an_option ()
{
    bold ; yellow
    echo -e "\n  That doesn't look like an option... Please try again."
    sleep 3
    cdrom_cleanup
    exec cd-mount
}

cdrom_access ()
{
    # Might have been mounted elsewhere
    CDROM_MOUNTPOINT=$(grep /dev/scd"$CDROM" /etc/mtab | awk '{print $2}')
    cd  $CDROM_MOUNTPOINT
    unbold ; clear ; fig-col
    echo -e "\n\n  Choose your access method for ${CDROM_MOUNTPOINT}.\n"
    head-col
    echo "  1:  Midnight Commander."
    echo "  2:  Vifm               "
    echo "  3:  MOC Music Player   "
    echo "  4:  Command Line       "
    echo
    fig-col
    echo "  Any other key to return to the cdrom access menu."
    unbold
    read -s -n 1 CDACCESS
    
    case $CDACCESS in
        1)
	mc 
	cd 
	cdrom_access
	;;
	2)
	vifm
	cd 
	cdrom_access
	;;
	3)
	mocp $CDROM_MOUNTPOINT
        pkill mocp # Apparently we have to obliterate mocp or it hangs on to its working directory...
	cd 
	cdrom_access
	;;
	4)
        white ; clear ; bold ; yellow
        echo -e "\nThis is a subshell at ${CDROM_MOUNTPOINT}. Exit when you are finished."
        echo -e "The prompt and text are in bold to remind you...\n"
        white ; bold
        /bin/bash
	unbold
        cd 
        cdrom_access
	;;
	*)
	cd
        exec cd-mount
	;;
    esac	
}

cdrom_mount_check ()
{
    # Is it in mtab?
    if grep /dev/scd"$CDROM" /etc/mtab > /dev/null 2>&1 ; then
        CDMTAB=yes
    else
        CDMTAB=no
    fi	
}

# Function to do cd/dvd mounts as root. 
maybe_mount_cdrom ()
{   
    cdrom_mount_check
    # Mount points?
    sudo mkdir -p /media/cdrom"$CDROM"

    if [ "$CDMTAB" = "yes" ] ; then
        # OK, we don't need to mount this
	:
    else
        head-col
        echo -e "\n  Mounting /media/cdrom"$CDROM", please wait..."
	sleep 3
        printf "  "
        sudo mount /dev/scd"$CDROM" /media/cdrom"$CDROM" > $cdromtemp/error 2>&1
	cdrom_mount_check
        if [ "$CDMTAB" = "no" ] ; then
            info-col
	    echo -e "\nYour device failed to mount. 'mount' says:\n"
	    cat $cdromtemp/error
	    sleep 6
	    cdrom_cleanup
	    exec cd-mount
	fi    
    fi
    
    cdrom_access
}

cdrom_umount_check ()
{
    # See if it's actually mounted...
    cdrom_mount_check
    # Various mind-numbing possibilities...
    if [ "$CDMTAB" = "no" ] ; then
        case "$ACTION" in
	    u|U)
            echo -e "\n  The device is not mounted, according to /etc/mtab."
	    sleep 3
	    exec cd-mount
	    ;;
	    e|E)
	    echo -e "\n  Ejecting..."
	    sleep 2
            if [ $CDROMCOUNT -gt 1 ] ; then
	        eject /dev/scd"$DEVNUM"
            else
	        eject /dev/scd0
	    fi
	    ;;
	esac
	
    elif [ "$CDMTAB" = "yes" ] ; then
        case "$ACTION" in
	    u|U)
	    echo -e "\n  Unmounting..."
	    sleep 2
	    if [ $CDROMCOUNT -gt 1 ] ; then
	        sudo umount /dev/scd"$DEVNUM"
	    else
	        sudo umount /dev/scd0
	    fi	
	    ;;
	    e|E)
            if [ $CDROMCOUNT -gt 1 ] ; then
	        sudo eject /dev/scd"$DEVNUM"
	    else
	        sudo eject /dev/scd0
	    fi
	    ;;
	esac    
    fi
}    

# Grab the cdrom/dvdroms and put them somewhere

    cdromtemp=$(mktemp -dt cdromtemp.XXXXXXXXXX)
    ls -1 /dev/scd[0-9] > $cdromtemp/cdroms # One on each line.

# See if we caught anything...
    if [ ! -s "$cdromtemp/cdroms" ] ; then
        bold ; yellow
        echo -e "\n  You don't seem to have any cdrom or dvdrom devices."
	sleep 3
	cdrom_cleanup
	exec cd-mount
    fi

# There are probably only one or two, but you never know...
    head-col
    echo -e "  Type this number:\tTo access:\t Mounted on:\n"

    CDROMCOUNT=0
    info-col
    for cdrom in $(cat "$cdromtemp/cdroms")
      do
        printf "\t  ${CDROMCOUNT}\t\t$cdrom"
        CDROMMOUNT=$(grep $cdrom /etc/mtab | awk '{print $2}')
      if [ $CDROMMOUNT ] ; then
          printf "\t$CDROMMOUNT\n"
      else
          printf "\n"
      fi    
        CDROMCOUNT=$(( $CDROMCOUNT + 1 ))
      done

    echo
    head-col
    if [ $CDROMCOUNT -gt 1 ] ; then
        echo -e "\n  Type the number for your device to access it.\n"
    else
        echo -e "\n  Press <enter> to access your device.\n"
    fi

    fig-col
    echo "  d: Other File Systems          "
    echo "  e: Eject a device.             "
    echo "  m: Return to the main INX menu."
    echo "  u: Unmount a device.           "
    echo "  X: Give me a command prompt.   "

    unbold
    f-ttynum
    read -s -n 1 CDROM
    if [ "$CDROM" = "" ] ; then CDROM=0 ; fi
    case  $CDROM in
        [0-"$CDROMCOUNT"]) # Anyone with more than 10 cdrom devices doesn't need INX ;)
        maybe_mount_cdrom
        ;;
	d|D)
	exec drivinx
	;;
        x|X)
        cdrom_cleanup
        clear
        f-prompt
        ;;
        e|E|u|U)
        ACTION=$CDROM
        # See if there's only one...
	if [ $CDROMCOUNT -gt 1 ] ; then
	    echo -e "\n  Type the number for the device that you wish to eject or unmount."
	    read -s -n 1 DEVNUM
	    CDROM=$DEVNUM  
	        case $DEVNUM in
		    [0-"$CDROMCOUNT"])
		    cdrom_umount_check
		    cdrom_cleanup
		    exec cd-mount
		    ;;
		    *)
                    not_an_option
		    ;;
		esac
	else
	    CDROM=0
	    cdrom_umount_check
	    cdrom_cleanup
	    exec cd-mount
	fi    
	;;
	m|M)
	cdrom_cleanup
	exec menu
	;;
        *)
        not_an_option
        ;;
    esac    

