remakesd

Annotated makesd-partition-summary

17:8724710f8bd7
2019-05-27 Paul Boddie Removed superfluous IFS storage and retrieval.
paul@5 1
#!/bin/sh
paul@5 2
paul@5 3
# Emit a partition summary for a device employing parameters such as the start,
paul@5 4
# size, and type.
paul@15 5
#
paul@15 6
# Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk>
paul@15 7
#
paul@15 8
# This program is free software; you can redistribute it and/or modify it under
paul@15 9
# the terms of the GNU General Public License as published by the Free Software
paul@15 10
# Foundation; either version 3 of the License, or (at your option) any later
paul@15 11
# version.
paul@15 12
#
paul@15 13
# This program is distributed in the hope that it will be useful, but WITHOUT
paul@15 14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@15 15
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@15 16
# details.
paul@15 17
#
paul@15 18
# You should have received a copy of the GNU General Public License along with
paul@15 19
# this program.  If not, see <http://www.gnu.org/licenses/>.
paul@5 20
paul@5 21
PROGNAME=`basename "$0"`
paul@5 22
THISDIR=`dirname "$0"`
paul@5 23
paul@5 24
paul@5 25
paul@5 26
# Emit any partition details as a complete record.
paul@5 27
paul@5 28
emit_partition()
paul@5 29
{
paul@5 30
    echo "${START:--}\t${SIZE:--}\t${TYPE:--}"
paul@5 31
}
paul@5 32
paul@5 33
# Reset the current partition details.
paul@5 34
paul@5 35
reset_partition()
paul@5 36
{
paul@5 37
    START=
paul@5 38
    SIZE=
paul@5 39
    TYPE=
paul@5 40
}
paul@5 41
paul@5 42
# Emit the current partition details and proceed to the next partition.
paul@5 43
paul@5 44
next_partition()
paul@5 45
{
paul@5 46
    if [ "$START" ] || [ "$SIZE" ] || [ "$TYPE" ] ; then
paul@5 47
        emit_partition
paul@5 48
        reset_partition
paul@5 49
    fi
paul@5 50
}
paul@5 51
paul@5 52
paul@5 53
paul@5 54
# Emit the help message if requested.
paul@5 55
paul@5 56
if [ "$1" = '--help' ] ; then
paul@5 57
    cat 1>&2 <<EOF
paul@5 58
Usage: $PROGNAME ( -f <type> | -p <start> | -s <size> )...
paul@5 59
paul@5 60
Produce partition descriptions, indicating partition type, start position and
paul@5 61
size for each partition. Each new occurrence of an active option starts a new
paul@5 62
partition description.
paul@5 63
paul@5 64
Each line of the produced description is tab-separated with '-' indicating an
paul@5 65
empty field.
paul@5 66
EOF
paul@5 67
    exit 0
paul@5 68
fi
paul@5 69
paul@5 70
# Process the arguments, building a partition description.
paul@5 71
paul@5 72
reset_partition
paul@5 73
paul@5 74
while [ "$1" ] ; do
paul@5 75
    case "$1" in
paul@5 76
        -f )
paul@5 77
            if [ "$TYPE" ] ; then next_partition ; fi
paul@5 78
            TYPE="$2"
paul@5 79
            shift 2
paul@5 80
            ;;
paul@5 81
        -p )
paul@5 82
            if [ "$START" ] ; then next_partition ; fi
paul@5 83
            START="$2"
paul@5 84
            shift 2
paul@5 85
            ;;
paul@5 86
        -s )
paul@5 87
            if [ "$SIZE" ] ; then next_partition ; fi
paul@5 88
            SIZE="$2"
paul@5 89
            shift 2
paul@5 90
            ;;
paul@5 91
        * )
paul@5 92
            shift 1
paul@5 93
            ;;
paul@5 94
    esac
paul@5 95
done
paul@5 96
paul@5 97
# Terminate any unfinished partition.
paul@5 98
paul@5 99
next_partition