Skip to content

Instantly share code, notes, and snippets.

@Lantizia
Last active July 27, 2019 11:46
Show Gist options
  • Save Lantizia/2d292dda74765ad588c232dc9de14071 to your computer and use it in GitHub Desktop.
Save Lantizia/2d292dda74765ad588c232dc9de14071 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# INSTALLATION...
#
# echo -e 'SUBSYSTEM=="block", ENV{ID_VENDOR}=="SWDrv", ENV{UDISKS_IGNORE}="1"' | sudo tee /etc/udev/rules.d/99-swdrv.rules
# sudo udevadm control -R
# until cdemu device-id 1; do cdemu add-device; done
# sudo wget http://gist.github.com/Lantizia/2d292dda74765ad588c232dc9de14071/raw/swdrv -O /usr/local/bin/swdrv
# sudo chmod +x /usr/local/bin/swdrv
#
exec 3>&1 >/dev/null 2>&1
log() { echo "$1" >&3; if [[ -n $2 ]]; then exit $2; fi; }
if [ "$1" == "insert" ]; then
# Check an image has been specified
[ -z "$2" ] && log "Error: No image specified." 1
# Check image file exists
[ -f "$2" ] || log "Error: Image file does not exist." 1
# Check there is a CDEmu Device #00 and if not add one
c=0; until cdemu device-id 0; do cdemu add-device; ((c++)); sleep 0.25; [ $c = 20 ] && log "Error: CDEmu Device #00 can not be added." 1; done
# Check CDEmu Device #00 is marked as for SWDrv use
cdemu device-id 0|grep "\['SWDrv"||cdemu device-id 0 "SWDrv" "Virt Optical Drv" "1.0" "SWDrv"
# Find the block location of CDEmu Device #00
SWDRVDEV=$(cdemu device-mapping|grep ^0|awk '{print $2}')
# If using Wine set this as DOS block device d:
[ -n "$WINEPREFIX" ] && ln -nfs $SWDRVDEV $WINEPREFIX/dosdevices/d::
# Unload CDEmu Device #00
c=0; until cdemu status|grep '^0 *False'; do cdemu unload 0; ((c++)); sleep 0.25; [ $c = 20 ] && log "Error: CDEmu Device #00 will not unload." 1; done
# Load CDEmu Device #00
c=0; until cdemu status|grep '^0 *True'; do cdemu load 0 "$2"; ((c++)); sleep 0.25; [ $c = 20 ] && log "Error: CDEmu Device #00 will not load." 1; done
# Mount CDEmu Device #00
c=0; until udisksctl info -b $SWDRVDEV|grep 'MountPoints: */'; do udisksctl mount -b $SWDRVDEV; ((c++)); sleep 0.25; [ $c = 20 ] && log "Error: CDEmu Device #00 will not mount." 1; done
# Find the mount location of CDEmu Device #00
SWDRVMNT=$(udisksctl info -b $SWDRVDEV|sed -n 's/^[ ]*MountPoints:[ ]*\(.*\)$/\1/p')
# If using Wine set this as DOS mount device d:
[ -n "$WINEPREFIX" ] && ln -nfs $SWDRVMNT $WINEPREFIX/dosdevices/d:
# Give out the mount location
log "$SWDRVMNT" 0
elif [ "$1" == "eject" ]; then
# Check CDEmu Device #00 actually exists
cdemu device-id 0 || log "Error: CDEmu Device #00 does not exist." 1
# Find the block location of CDEmu Device #00
SWDRVDEV=$(cdemu device-mapping|grep ^0|awk '{print $2}')
# If CDEmu Device #00 is loaded then unmount it if mounted
if [ cdemu status|grep '^0 *True' ]; then c=0; until udisksctl info -b $SWDRVDEV|grep 'MountPoints: *$'; do udisksctl unmount -b $SWDRVDEV; ((c++)); sleep 0.25; [ $c = 20 ] && log "Error: CDEmu Device #00 will not unmount." 1; done; fi
# Unload CDEmu Device #00
c=0; until cdemu status|grep '^0 *False'; do cdemu unload 0; ((c++)); sleep 0.25; [ $c = 20 ] && log "Error: CDEmu Device #00 will not unload." 1; done
else
log "Error: Specify insert or eject." 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment