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

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
1.1.1.6 ! root       47:        # with extension removed and spaces converted to underscores
1.1.1.5   root       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
1.1.1.6 ! root       52:                STFILE=${BASENAME}.st
1.1.1.5   root       53:        else
                     54:                # basename can reliably give last path component
1.1.1.6 ! root       55:                STFILE=$(basename "$BASENAME").st
1.1.1.5   root       56:        fi
1.1.1.6 ! root       57:        STFILE=$(echo $STFILE | tr ' ' '_')
1.1       root       58: fi
                     59: if [ -f "$STFILE" ]; then
                     60:        echo "ERROR: ST file '$STFILE' already exists, remove it first. Aborting..."
                     61:        exit 1
                     62: fi
                     63: 
1.1.1.2   root       64: step=0
1.1.1.5   root       65: TEMPDIR=""
1.1       root       66: 
1.1.1.2   root       67: # script exit/error handling
                     68: exit_cleanup ()
                     69: {
                     70:        if [ $? -eq 0 ]; then
                     71:                echo "$step) Cleaning up temporary files..."
                     72:        else
                     73:                echo
                     74:                echo "ERROR, cleaning up..."
                     75:        fi
1.1.1.5   root       76:        if [ \! -z "$TEMPDIR" ]; then
                     77:                echo "rm -rv $TEMPDIR"
                     78:                rm -rv $TEMPDIR
                     79:        fi
1.1.1.2   root       80:        echo "Done."
                     81: }
                     82: 
1.1.1.5   root       83: 
                     84: if [ -d "$ZIPFILE" ]; then
                     85:        echo "Converting '$ZIPFILE/' -> '$STFILE'"
                     86:        ZIPDIR="$ZIPFILE"
                     87: else
                     88:        if [ -z "$(which unzip)" ]; then
                     89:                echo "ERROR: 'unzip' tool missing."
                     90:                exit 2
                     91:        fi
1.1.1.6 ! root       92:        TEMPDIR=$(mktemp -d) || exit 2
1.1.1.5   root       93:        echo "Converting '$ZIPFILE' -> '$TEMPDIR/' -> '$STFILE'"
                     94: 
                     95:        trap exit_cleanup EXIT
                     96: 
                     97:        echo
                     98:        step=$(($step+1))
                     99:        echo "$step) Unzipping..."
                    100:        echo "unzip $ZIPFILE -d $TEMPDIR"
                    101:        unzip "$ZIPFILE" -d "$TEMPDIR" || exit 2
                    102: 
                    103:        # .zip files created with STZip sometimes have wrong access rights...
                    104:        echo "chmod -R u+rw $TEMPDIR/*"
                    105:        chmod -R u+rw $TEMPDIR/*
                    106:        ZIPDIR=$TEMPDIR
                    107: fi
1.1       root      108: 
1.1.1.3   root      109: echo
                    110: step=$(($step+1))
                    111: echo "$step) Checking/skipping intermediate directories..."
                    112: while true; do
1.1.1.6 ! root      113:        count=$(ls "$ZIPDIR"|wc -l)
1.1.1.3   root      114:        if [ $count -ne 1 ]; then
                    115:                if [ $count -eq 0 ]; then
                    116:                        echo "ERROR: zip content is empty!"
                    117:                        exit 1
                    118:                fi
                    119:                # more than one dir/file
                    120:                break
                    121:        fi
1.1.1.6 ! root      122:        dir=$(ls "$ZIPDIR")
1.1.1.3   root      123:        if [ \! -d "$ZIPDIR/$dir" ]; then
                    124:                # not dir
                    125:                break
                    126:        fi
                    127:        if [ -z "$(echo $dir|grep -v -i '^auto$')" ]; then
                    128:                # don't skip AUTO dir
                    129:                break
                    130:        fi
                    131:        echo "- $dir"
1.1.1.6 ! root      132:        ZIPDIR="$ZIPDIR/$dir"
1.1.1.3   root      133: done
1.1       root      134: 
                    135: # size of reserved sectors, FATs & root dir + zip content size
1.1.1.6 ! root      136: size=$((24 + $(du -ks "$ZIPDIR"|awk '{print $1}')))
1.1       root      137: 
                    138: # find a suitable disk size supported by mformat and Atari ST
                    139: disksize=0
                    140: for i in 360 400 720 800 1440 2880; do
                    141:        if [ $i -gt $size ]; then
                    142:                disksize=$i
                    143:                break
                    144:        fi
                    145: done
                    146: 
                    147: if [ $disksize -gt 0 ]; then
                    148:        echo
                    149:        step=$(($step+1))
                    150:        echo "$step) Creating $disksize KB disk image..."
1.1.1.2   root      151:        echo "dd if=/dev/zero of=$STFILE bs=1024 count=$disksize"
                    152:        dd if=/dev/zero of="$STFILE" bs=1024 count=$disksize
1.1.1.6 ! root      153: 
1.1       root      154:        echo
                    155:        step=$(($step+1))
                    156:        echo "$step) Formating disk image..."
                    157:        case $disksize in
1.1.1.2   root      158:                360) format="-t 80 -h 1 -n 9" ;;
                    159:                400) format="-t 80 -h 1 -n 10" ;;
                    160:                800) format="-t 80 -h 2 -n 10" ;;
                    161:                *)   format="-f $disksize" ;;
1.1       root      162:        esac
1.1.1.2   root      163:        echo "mformat -a $format -i $STFILE ::"
                    164:        mformat -a $format -i "$STFILE" ::
1.1.1.6 ! root      165: 
1.1       root      166:        echo
                    167:        step=$(($step+1))
                    168:        echo "$step) Copying data to disk image..."
1.1.1.3   root      169:        echo "MTOOLS_NO_VFAT=1 mcopy -i $STFILE -spmv $ZIPDIR/* ::"
1.1.1.6 ! root      170:        MTOOLS_NO_VFAT=1 mcopy -i "$STFILE" -spmv "$ZIPDIR"/* ::
1.1       root      171: else
                    172:        echo "ERROR: zip contents don't fit to a floppy image ($size > 2880 KB)."
                    173: fi
                    174: 
                    175: echo
                    176: step=$(($step+1))
1.1.1.2   root      177: # 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.