Annotation of hatari/tools/zip2st.sh, revision 1.1.1.4

1.1       root        1: #!/bin/sh
                      2: # Script for converting .ZIP archives to Atari .ST disk images
                      3: # (for the Hatari emulator).
                      4: 
                      5: # tools are present?
                      6: if [ -z "$(which unzip)" ]; then
                      7:        echo "ERROR: 'unzip' missing."
                      8:        exit 2
                      9: fi
                     10: if [ -z "$(which mformat)" ] || [ -z "$(which mcopy)" ]; then
                     11:        echo "ERROR: 'mformat' or 'mcopy' (from 'mtools' package) missing."
                     12:        exit 2
                     13: fi
                     14: 
1.1.1.4 ! root       15: usage ()
        !            16: {
1.1       root       17:        name=${0##*/}
1.1.1.3   root       18:        echo "Convert a .zip archive file to a .st disk image."
                     19:        echo
                     20:        echo "Single intermediate directories in the zip"
                     21:        echo "file are skipped."
1.1       root       22:        echo
                     23:        echo "Usage:"
                     24:        echo " $name srcname.zip [destname.st]"
                     25:        echo
                     26:        echo "Example:"
                     27:        echo " for zip in *.zip; do $name \$zip; done"
                     28:        echo
1.1.1.4 ! root       29:        if [ \! -z "$1" ]; then
        !            30:                echo "ERROR: $1!"
        !            31:        fi
1.1       root       32:        exit 1
1.1.1.4 ! root       33: }
        !            34: 
        !            35: # one ZIPFILE given?
        !            36: if [ $# -lt 1 ] || [ -z "$1" ] || [ $# -gt 2 ]; then
        !            37:        usage "wrong number of argument(s)"
1.1       root       38: fi
                     39: 
                     40: ZIPFILE=$1
                     41: STFILE=$2
1.1.1.4 ! root       42: 
        !            43: if [ \! -f "$ZIPFILE" ]; then
        !            44:        usage "given zipfile $ZIPFILE is missing"
        !            45: fi
        !            46: 
1.1       root       47: if [ -z "$STFILE" ]; then
                     48:        # if no STFILE path given, save it to current dir (remove path)
                     49:        # and use the ZIPFILE name with the extension removed.
                     50:        # (done with POSIX shell parameter expansion)
                     51:        BASENAME=${ZIPFILE##*/}
                     52:        BASENAME=${BASENAME%.zip}
                     53:        BASENAME=${BASENAME%.ZIP}
                     54:        STFILE=$BASENAME.st
                     55: fi
                     56: if [ -f "$STFILE" ]; then
                     57:        echo "ERROR: ST file '$STFILE' already exists, remove it first. Aborting..."
                     58:        exit 1
                     59: fi
                     60: 
1.1.1.2   root       61: step=0
1.1       root       62: TEMPDIR=`mktemp -d` || exit 2
                     63: echo "Converting" $ZIPFILE "->" $TEMPDIR "->" $STFILE
                     64: 
1.1.1.2   root       65: # script exit/error handling
                     66: exit_cleanup ()
                     67: {
                     68:        if [ $? -eq 0 ]; then
                     69:                echo "$step) Cleaning up temporary files..."
                     70:        else
                     71:                echo
                     72:                echo "ERROR, cleaning up..."
                     73:        fi
                     74:        echo "rm -rv $TEMPDIR"
                     75:        rm -rv $TEMPDIR
                     76:        echo "Done."
                     77: }
                     78: trap exit_cleanup EXIT
                     79: 
1.1       root       80: echo
1.1.1.2   root       81: step=$(($step+1))
1.1       root       82: echo "$step) Unzipping..."
1.1.1.2   root       83: echo "unzip $ZIPFILE -d $TEMPDIR"
                     84: unzip "$ZIPFILE" -d "$TEMPDIR" || exit 2
1.1       root       85: 
                     86: # .zip files created with STZip sometimes have wrong access rights...
1.1.1.2   root       87: echo "chmod -R u+rw $TEMPDIR/*"
1.1       root       88: chmod -R u+rw $TEMPDIR/*
                     89: 
1.1.1.3   root       90: echo
                     91: step=$(($step+1))
                     92: echo "$step) Checking/skipping intermediate directories..."
                     93: ZIPDIR=$TEMPDIR
                     94: while true; do
                     95:        count=$(ls $ZIPDIR|wc -l)
                     96:        if [ $count -ne 1 ]; then
                     97:                if [ $count -eq 0 ]; then
                     98:                        echo "ERROR: zip content is empty!"
                     99:                        exit 1
                    100:                fi
                    101:                # more than one dir/file
                    102:                break
                    103:        fi
                    104:        dir=$(ls $ZIPDIR)
                    105:        if [ \! -d "$ZIPDIR/$dir" ]; then
                    106:                # not dir
                    107:                break
                    108:        fi
                    109:        if [ -z "$(echo $dir|grep -v -i '^auto$')" ]; then
                    110:                # don't skip AUTO dir
                    111:                break
                    112:        fi
                    113:        echo "- $dir"
                    114:        ZIPDIR=$ZIPDIR/$dir
                    115: done
1.1       root      116: 
                    117: # size of reserved sectors, FATs & root dir + zip content size
1.1.1.3   root      118: size=$((24 + $(du -ks $ZIPDIR|awk '{print $1}')))
1.1       root      119: 
                    120: # find a suitable disk size supported by mformat and Atari ST
                    121: disksize=0
                    122: for i in 360 400 720 800 1440 2880; do
                    123:        if [ $i -gt $size ]; then
                    124:                disksize=$i
                    125:                break
                    126:        fi
                    127: done
                    128: 
                    129: if [ $disksize -gt 0 ]; then
                    130:        echo
                    131:        step=$(($step+1))
                    132:        echo "$step) Creating $disksize KB disk image..."
1.1.1.2   root      133:        echo "dd if=/dev/zero of=$STFILE bs=1024 count=$disksize"
                    134:        dd if=/dev/zero of="$STFILE" bs=1024 count=$disksize
1.1       root      135:        
                    136:        echo
                    137:        step=$(($step+1))
                    138:        echo "$step) Formating disk image..."
                    139:        case $disksize in
1.1.1.2   root      140:                360) format="-t 80 -h 1 -n 9" ;;
                    141:                400) format="-t 80 -h 1 -n 10" ;;
                    142:                800) format="-t 80 -h 2 -n 10" ;;
                    143:                *)   format="-f $disksize" ;;
1.1       root      144:        esac
1.1.1.2   root      145:        echo "mformat -a $format -i $STFILE ::"
                    146:        mformat -a $format -i "$STFILE" ::
1.1       root      147:        
                    148:        echo
                    149:        step=$(($step+1))
                    150:        echo "$step) Copying data to disk image..."
1.1.1.3   root      151:        echo "MTOOLS_NO_VFAT=1 mcopy -i $STFILE -spmv $ZIPDIR/* ::"
                    152:        MTOOLS_NO_VFAT=1 mcopy -i "$STFILE" -spmv $ZIPDIR/* ::
1.1       root      153: else
                    154:        echo "ERROR: zip contents don't fit to a floppy image ($size > 2880 KB)."
                    155: fi
                    156: 
                    157: echo
                    158: step=$(($step+1))
1.1.1.2   root      159: # do cleanup in exit handler

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.