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

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