Skip to content

Instantly share code, notes, and snippets.

@vroman

vroman/floppycopy.sh

Last active Jan 11, 2019
Embed
What would you like to do?
#!/bin/bash
# A shabby CLI script to create floppy images which only runs on macOS
#
# Made for my own use, so don't expect it will fit yours. Its purposse
# is copy 1.44 MB floppy images from an external USB floppy disk into
# an image for later backup.
#
# Copyright (C) 2018-2019 Víctor Román Archidona
# https://www.victorroman.es
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org>
main() {
local floppy=$(get_floppy)
if [ -z "${floppy}" ]; then
echo "No floppies available. Exiting"
exit 1
fi
local floppy_bs=$(get_floppy_bs $floppy)
local floppy_count=$(get_floppy_count $floppy)
echo -e "Floppy source: $floppy - Sector size: $floppy_bs - Total blocks: $floppy_count - Total size: $(($floppy_bs*$floppy_count))\n"
while [ true ] ; do
read -p "Where should I store the diskette image? (path and filename)?: " savefile
[ "${savefile}" == "" ] && continue
#local message="An image of $floppy will be saved into $savefile. Insert your diskette and"
#local message+="press any key to continue.\n"
#echo -e $message
#read -n 1 -s -r
echo "An image of $floppy will be saved into $savefile."
# Locks untel there is a diskette
echo -n "Checking if diskette is onto the floppy, waiting otherwise..."
floppy_read_wait $floppy
echo " OK"
floppy_label=$(get_floppy_label $floppy)
echo -e "Floppy label is: $floppy_label\n\n"
echo -n "Floppy MD5 is being calculated. Please wait."
floppy_md5=$(calculate_md5 $floppy)
echo "Floppy MD5 is $floppy_md5"
echo "Copying file..."
sudo dd if=$floppy of=$savefile bs=$floppy_bs count=$floppy_count
chown $UID:$(id -g $UID) $savefile
file_md5=$(calculate_md5 $savefile)
echo "File MD5 is: $file_md5"
if [ "" != "" ]; then
echo "WARNING: MD5 between floppy and image differs. Image may not be accurate"
else
echo "MD5 matches between floppy and image. Image is consistent."
fi
echo "Process ended. Diskette is being ejected."
sudo diskutil eject $floppy
read -p "Would you like to create another floppy image? (Y/N) " another
another=$(echo $another | tr 'A-Z' 'a-z')
if [ "${another}" == "y" ]; then
continue
fi
if [ "${another}" == "n" ]; then
exit 0
fi
done
}
get_floppy() {
local candidates=$(sudo diskutil list | grep "(external, physical)" | awk {'print $1'})
local floppies=()
for candidate in $candidates; do
size=$(sudo diskutil info $candidate | grep "Disk Size:" | awk {'print $3$4'})
if [ "${size}" == "1.5MB" ]; then
floppies+=($candidate)
fi
done
floppies=$(echo $floppies | sed s/'\/dev\/disk'/'\/dev\/rdisk'/g)
echo $floppies
}
get_floppy_bs() {
sudo diskutil info $1 | grep "Disk Size:" | awk {'print $9'} | awk -F- {'print $1'}
}
get_floppy_count() {
sudo diskutil info $1 | grep "Disk Size:" | awk {'print $8'}
}
get_floppy_label() {
sudo diskutil info $1 | grep "Volume Name" | awk '{ print substr($0, index($0,$3)) }'
}
floppy_read_wait() {
local output=""
while [ -z "${output}" ]; do
output=$(sudo diskutil info $1 | grep "Volume Total Space")
sleep 1
done
}
calculate_md5() {
sudo md5 $1 | awk {'print $4'}
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.