Advertisement
yo9gjx

gettfloppyimage

Nov 12th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 15.25 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # generate filename without directory:
  4. FILENAME=$(basename $0)
  5. CAT=$(which cat)
  6.  
  7. print_help()
  8. {
  9. ##########################################################################
  10. $CAT <<EOF
  11.  
  12.   $0
  13.  
  14.   author:      Karl Voit, shellscript@Karl-Voit.at
  15.  
  16.   date:        v0.1 2002-05-20
  17.                v0.2 2003-12-12 (docu)
  18.  
  19.   copyright:   GPL
  20.  
  21.   usage:       $FILENAME
  22.                ... this will read in one diskette image
  23.                $FILENAME 5
  24.                ... this will read in five diskettes in a row (same name)
  25.  
  26.   description: This script generates an image of a floppy disk using dd
  27.  
  28.                Please email me suggestions for improvements and errors!
  29.                I am still learning how to write good shell-scripts *g*
  30.  
  31. EOF
  32. ##########################################################################
  33. }
  34.  
  35. ## 2do:
  36. ##      * loop for set of disks
  37. ##      *
  38.  
  39.  
  40.  
  41. ########################################################
  42. ########################################################
  43. ########################################################
  44. ##                                                    ##
  45. ##                  PERSONALISATION                   ##
  46. ##                                                    ##
  47. ########################################################
  48. ########################################################
  49. ########################################################
  50.  
  51.  
  52. # filenames to use for ...
  53.  
  54. DEFAULT_SIGNAL="/usr/share/sounds/users/SOUND/SHORTS/BLOOP.WAV"
  55.  
  56.  
  57. # change to "done", if you changed all options above to _your_ needs!
  58.  
  59. CONFIGURED_="NOTdone"
  60.  
  61.  
  62. ########################################################
  63.  
  64.  
  65.  
  66.  
  67. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  68. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  69. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  70. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  71. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  72. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  73. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  74. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  75.  
  76. # --- normally you DON'T have to change anything below this line! --- #
  77.  
  78. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  79. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  80. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  81. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  82. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  83. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  84. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  85. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
  86.  
  87.  
  88.  
  89.  
  90. ########################################################
  91. ########################################################
  92. ########################################################
  93. ##                                                    ##
  94. ##               hardcoded configuration              ##
  95. ##                                                    ##
  96. ########################################################
  97. ########################################################
  98. ########################################################
  99.  
  100.  
  101. ## generates a timestamp like "2002_Jan_31_-_09h43m03s"
  102. #TIMESTAMP1=`/bin/date +%Y_%b_%d_-_%Hh%Mm%Ss`
  103.  
  104. ## generates a syslog-like timestamp like "Mar 16 09:19:50"
  105. ## lisa qmail:
  106. #TIMESTAMP2=`/bin/date +"%b %d %H:%M:%S"`
  107.  
  108. #LOGFILE_BIN=/???path/"$TIMESTAMP".log
  109.  
  110.  
  111. BIG_DISKETTE_SIZE="1474560"
  112. SMALL_DISKETTE_SIZE="737280"
  113.  
  114.  
  115. # change to "on" if you want to read the debug-messages (can be much) to trace
  116. # down problems:
  117. export DEBUG="off"
  118.  
  119.  
  120. ########################################################
  121. ########################################################
  122. ########################################################
  123. ##                                                    ##
  124. ##                   F U N C T I O N S                ##
  125. ##                                                    ##
  126. ########################################################
  127. ########################################################
  128. ########################################################
  129.  
  130. ## ---------------------------------------------------------
  131.  
  132. myexit()
  133. {
  134.     doreport debug "function myexit($1) called"
  135.  
  136.     [ "$1" -lt 1 ] && echo "$FILENAME done."
  137.     [ "$1" -gt 0 ] && echo "$FILENAME aborted with errorcode $1."
  138.  
  139. #optionally    [ "$1" -gt 0 ] && do_sound_error
  140.  
  141.     exit $1
  142. }
  143.  
  144.  
  145. ## ---------------------------------------------------------
  146.  
  147.  
  148. ## check, if some files needed are not found
  149. testiffound()
  150. {
  151.     doreport debug "function testiffound($1) called"
  152.  
  153.   if [ -z "$2" ]; then
  154.     doreport debug "The tool \"$1\" is missing because \"$2\" is empty."
  155.     doreport notify "The tool \"$1\" could not be located (missing?)"
  156.     export SOMETHINGMISSING="yes"
  157.   fi
  158. }
  159.  
  160.  
  161. ## ---------------------------------------------------------
  162.  
  163.  
  164. doreport_internal_writestring()
  165. {
  166. ## !! for use withing function "doreport" only !!
  167. ## prints out all strings on stdout
  168.  
  169.         echo
  170.         echo "===$1============================================"
  171.         echo "                                                 $FILENAME"
  172.  
  173.  
  174. ## FIXXME:
  175. ## 2DO: loop instead of this quick-hack!!!
  176.  
  177.         if [ ! -z "$2" ]; then
  178.         echo "$2"
  179.         fi
  180.         if [ ! -z "$3" ]; then
  181.         echo "$3"
  182.         fi
  183.         if [ ! -z "$4" ]; then
  184.         echo "$4"
  185.         fi
  186.         if [ ! -z "$5" ]; then
  187.         echo "$5"
  188.         fi
  189.         if [ ! -z "$6" ]; then
  190.         echo "$6"
  191.         fi
  192.         if [ ! -z "$7" ]; then
  193.         echo "$7"
  194.         fi
  195.         if [ ! -z "$8" ]; then
  196.         echo "$8"
  197.         fi
  198.  
  199.         echo
  200.         echo "===============================================================";
  201.         echo
  202.  
  203. }
  204.  
  205.  
  206.  
  207. ## ---------------------------------------------------------
  208.  
  209.  
  210. doreport()
  211. {
  212. ## reports the parameter to the stdout
  213.  
  214. ## usage: (shortnote|notify|error|debug) string1 [string2] [string3] [...] [string7]
  215.  
  216. ## NEEDS: doreport_internal_writestring
  217.  
  218.  
  219.     case "$1" in
  220.  
  221.     "shortnote")
  222.     echo "$FILENAME: $2 $3 $4 $5 $6 $7";;
  223.  
  224.     "notify")
  225.     doreport_internal_writestring " notification ==" "$2" "$3" "$4" "$5" "$6" "$7";;
  226.  
  227.     "error")
  228.         doreport_internal_writestring " ERROR =========" "$2" "$3" "$4" "$5" "$6" "$7";;
  229.  
  230.     "debug")
  231.         ## debugs the script
  232.         if [[ "$DEBUG" = "on" ]]; then echo "$TIMESTAMP $FILENAME: DEBUG: $2 $3 $4 $5 $6 $7"; fi;
  233.         echo "do nothing" >/dev/null;;
  234.  
  235.     *)
  236.         doreport_internal_writestring " INTERNAL ERROR " "An error occured, while calling function \"doreport\":" "The parameter that was given ($1) has no target/handle." "Aborting.";
  237.         myexit 1;;
  238.  
  239.     esac
  240. }
  241.  
  242.  
  243.  
  244.  
  245. ## ---------------------------------------------------------
  246.  
  247. ask()
  248. {
  249. ## asks something (parameter 1) the user
  250.  
  251.   echo
  252.   echo "=== question =================================================="
  253.   echo "                                                 $FILENAME"
  254.   echo "$1"
  255.   echo
  256.   echo "==============================================================="
  257.   echo
  258.  
  259. ## example: ##  ask "Do you want this?"
  260. ## example: ##  read ANSWER
  261. ## example: ##  echo  
  262. ## example: ##  case "$ANSWER" in
  263. ## example: ##  
  264. ## example: ##  y)
  265. ## example: ##    CDCPY_docover ;;
  266. ## example: ##  *)
  267. ## example: ##    echo "  OK, maybe you're right." ;
  268. ## example: ##    echo "  We don't need these fancy stuff anyway ..." ;
  269. ## example: ##    echo ;;
  270. ## example: ##
  271. ## example: ##  esac
  272.  
  273. }
  274.  
  275.  
  276. ## ---------------------------------------------------------
  277.  
  278. ## plays the soundfile using "play"
  279. play_sound()
  280. {
  281.     ## wether "play" is avaliable is checked before
  282.     if [[ ! -z "$1" ]] ; then $PLAY "$1" >/dev/null; fi
  283. }
  284.  
  285.  
  286. ## ---------------------------------------------------------
  287.  
  288.  
  289. precondition_asserts()
  290. {
  291. ## * test for things that are needed
  292. ## * check, if caller = root-user
  293. ## * check, if everything was configured by the user
  294. ## * check, if a parameter is given:
  295.  
  296.       export SOMETHINGMISSING="no"
  297.  
  298.       DD=$(which dd)
  299.       testiffound dd $DD
  300.      
  301.       PLAY=$(which play)
  302.       testiffound play $PLAY
  303.      
  304.       LS=$(which ls)
  305.       testiffound ls $LS
  306.      
  307.       EXPR=$(which expr)
  308.       testiffound expr $EXPR
  309.      
  310. ##  MV=$(which mv)
  311. ##  testiffound cdrdao $MV
  312. ## 
  313. ##  CAT=$(which cat)
  314. ##  testiffound cat $CAT
  315.    
  316.     GREP=$(which grep)
  317.     testiffound grep $GREP
  318.    
  319. ##  HEAD=`which head`
  320. ##  testiffound head $HEAD
  321.    
  322.     AWK=`which awk`
  323.     testiffound awk $AWK
  324.    
  325. ##  SED=`which sed`
  326. ##  testiffound sed $SED
  327. ## 
  328. ##  FIND=`which find`
  329. ##  testiffound find $FIND
  330. ## 
  331. ##  DATE=`which date`
  332. ##  testiffound date $DATE
  333. ## 
  334. ##  CHOWN=`which chown`
  335. ##  testiffound chown $CHOWN
  336.  
  337.  
  338.     ## check, if any tool was NOT found
  339.     doreport debug "CHECK: any tool missing?"
  340.     if [ "$SOMETHINGMISSING" = "yes" ]; then
  341.       print_help
  342.       doreport error "One or more tool(s), that are needed are missing! (See output above!)" "Please make sure, that you those tools are installed properly and try again." "aborting."
  343.       myexit 2
  344.     fi
  345.  
  346.  
  347.  
  348.     ## check, if everything was configured by the user
  349.     if [ ! "$CONFIGURED_" = "done" ]; then
  350.       print_help
  351.       doreport "error" "Please make sure, that you checked/modified all options" "to meet your requirements!" "(see section "PERSONALISATION" in file $0)"
  352.       myexit 3
  353.     fi
  354.    
  355.    
  356.    
  357.     ## check, if a parameter is given:
  358.     if [ -z "$1" ]; then
  359.      #print_help
  360.      doreport "debug" "\$1 == \"$1\""
  361.      doreport "notify" "no parameter 1 for the image-file found!" "Using standard-filename \"floppy.img\""
  362.      IMAGE_FILE="floppy"
  363.  
  364. ##     ask "Do you want to read in with the (s)tandard-filename or (e)xit?"
  365. ##     read ANSWER
  366. ##     echo  
  367. ##     case "$ANSWER" in
  368. ##     
  369. ##     s|S|y|Y)
  370. ##       IMAGE_FILE="floppy" ;;
  371. ##     *)
  372. ##       myexit 1 ;;
  373. ##   
  374. ##     esac
  375.      
  376.     else
  377.      IMAGE_FILE="$1"
  378.     fi
  379.    
  380.     ## check, if a parameter is given:
  381.     if [ -n "$2" ]; then
  382.     doreport debug "Second parameter \"$2\" found."
  383.     doreport "shortnote" "I recognised a second parameter \"$2\" which defines the amount of images."
  384.     IMAGE_COUNT="$2"
  385.     else
  386.     IMAGE_COUNT=""
  387.     fi
  388.  
  389.     doreport debug "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< all checks OK!"
  390.     export PRECONDITIONS_CHECKED="yes"
  391.  
  392.  
  393. }
  394.  
  395.  
  396. ## ---------------------------------------------------------
  397.  
  398.  
  399. check_filesize()
  400. {
  401.  
  402.     CHK_FILESIZE_RESULT="not OK"
  403.  
  404.     CHK_FILESIZE=`$LS -la|$GREP "$1"|$AWK '{ print $5 }'`
  405.  
  406.     doreport "debug" "determined file-size is \"$CHK_FILESIZE\""
  407.  
  408.     if [ "$CHK_FILESIZE" == "$BIG_DISKETTE_SIZE" ]
  409.     then
  410.     doreport "debug" "filesize of image seems to be OK for 1.44MB."
  411.     CHK_FILESIZE_RESULT="OK"
  412.     else
  413.     doreport "debug" "The filesize of the image differs from standard 1.44MB-diskette!"
  414.     fi
  415.  
  416.     if [ "$CHK_FILESIZE" == "$SMALL_DISKETTE_SIZE" ]
  417.     then
  418.     doreport "debug" "filesize of image seems to be OK for 720kB."
  419.     CHK_FILESIZE_RESULT="OK"
  420.     else
  421.     doreport "debug" "The filesize of the image differs from standard 720kB-diskette!"
  422.     fi
  423.  
  424.  
  425. #    if [ "$CHK_FILESIZE_RESULT" == "OK" -a $CHK_EXITSTATUS -eq 0 ]
  426.     if [ "$CHK_FILESIZE_RESULT" == "OK" ]
  427.     then
  428.     doreport "shortnote" "(The creation of the disk image was succesfully.)"
  429.     else
  430.     doreport "error" "There was an error reading the diskette!" "Filesize doesn't fit 720kB nor 1.44MB-Diskette."
  431.     fi
  432.  
  433.  
  434. }
  435.  
  436.  
  437. ## ---------------------------------------------------------
  438.  
  439. postcondition_asserts()
  440. {
  441.  
  442. ## * deletes temporary files
  443. ## * ...
  444.  
  445. ##  if [ -z "???filename" ]; then
  446. ##    ???
  447. ##  fi
  448.  
  449. echo "donothing" >/dev/null
  450.  
  451. }
  452.  
  453.  
  454.  
  455. ########################################################
  456. ########################################################
  457. ########################################################
  458. ##                                                    ##
  459. ##                S C R I P T                         ##
  460. ##                                                    ##
  461. ########################################################
  462. ########################################################
  463. ########################################################
  464.  
  465. ## test for important stuff:
  466. precondition_asserts "$1" "$2" "$3" "$4" "$4" "$5" "$6" "$7" "$8" "$9"
  467.  
  468.  
  469.  
  470. ## parse parameter 1
  471. case "$1" in
  472.  
  473.     "help"|"--help"|"?"|"/?")
  474.     doreport debug "Parameter help|--help|?|/? detected"
  475.     print_help;
  476.     myexit 0;;
  477.  
  478. ##    "kill"|"stop")
  479. ##    doreport debug "Parameter kill|stop detected"
  480. ##    blablabla;;
  481. ##
  482. ##    *)
  483. ##    doreport debug "Unknown parameter.";
  484. ##    doreport error "The parameter you typed in ($1) could not be recognised!" "It will ignored and the script halted.";
  485. ##    print_help;
  486. ##    myexit 1;;
  487.  
  488. esac
  489.  
  490.  
  491.  
  492. if [ -z "$IMAGE_COUNT" ]
  493. then
  494.     ###################################
  495.     ## do a simple copy of ONE image ##
  496.     ###################################
  497.  
  498.     doreport "notify" "Everything seems to be OK, starting to read out the floppy ..." "May take long - best-case approx. 50 seconds."
  499.  
  500.     $DD if="/dev/fd0" of="$IMAGE_FILE.img" count=1 bs=1440k >/dev/null
  501.  
  502.     CHK_EXITSTATUS=$?
  503.  
  504.     echo " "
  505.  
  506.     if [ $CHK_EXITSTATUS -eq 0 ]
  507.     then
  508.     doreport "debug" "Exit-status of dd was OK"
  509.     else
  510.         doreport "debug" "dd had exit status != 0"
  511.         doreport "error" "An error occured during reading the floppy disk."
  512.     fi
  513.  
  514.     check_filesize "$IMAGE_FILE.img"
  515.  
  516.     play_sound $DEFAULT_SIGNAL
  517.  
  518. else
  519.  
  520.     #################################################
  521.     ## make a set of images from 1 to $IMAGE_COUNT ##
  522.     #################################################
  523.  
  524.     counter=1
  525.  
  526.     while [ $counter -le $IMAGE_COUNT ]
  527.     do
  528.  
  529.     echo " "
  530.     doreport "shortnote" "[diskette $counter] Please insert diskette number $counter (of $IMAGE_COUNT) and press ENTER"
  531.     read DUMMYVARIABLE
  532.  
  533.     doreport "shortnote" "[diskette $counter] (starting to read out the floppy - usually takes long: best-case 50 seconds)"
  534.  
  535.     ## append leading zero for all interations < 9:
  536.     if [ $counter -le 9 ]
  537.     then
  538.         CURRENT_FILENAME="$IMAGE_FILE"_0"$counter".img
  539.     else
  540.         CURRENT_FILENAME="$IMAGE_FILE""$counter".img
  541.     fi
  542.  
  543.     doreport "debug" "doing interation for file $IMAGE_FILE number $counter"
  544.  
  545.     ## do the actual thing (wow! at last we can really _do_ something ;-)
  546.     $DD if="/dev/fd0" of="$CURRENT_FILENAME" count=1 bs=1440k >/dev/null
  547.  
  548.     if [ $? -eq 0 ]
  549.     then
  550.         doreport "shortnote" "[diskette $counter] (The readout of the disk image number $counter was succesfully.)"
  551.     else
  552.         doreport "debug" "dd had exit status != 0"
  553.         doreport "error" "[diskette $counter] An error occured during reading the floppy disk number $counter."
  554.     fi
  555.  
  556.     check_filesize "$CURRENT_FILENAME"
  557.  
  558.     ## increment counter
  559.     counter=`$EXPR $counter + 1`
  560.  
  561.     play_sound $DEFAULT_SIGNAL
  562.  
  563.     done
  564.  
  565. fi
  566.  
  567.  
  568.  
  569. ########################################################
  570.  
  571.  
  572. ## test for important stuff:
  573. postcondition_asserts
  574.  
  575. myexit 0
  576.  
  577.  
  578. ########################################################
  579. ##                      E N D                         ##
  580. ########################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement