#!/bin/bash
# 
# Return a disk to the operating system
#


# Force LC_ALL=C
export LC_ALL=C
 
USAGE="[-l <manager>] [-v] <label>|<device>"

exec 3>/dev/null

help=
verbose=
version=
usage=
while case "$#" in 0) break ;; esac
do
    case "$1" in
    -l|--manager)
        case "$#" in 1) usage=t; break ;; esac
        shift
        ORACLE_ASMMANAGER="$1"
        ;;
    -v|--verbose)
        verbose=t
        exec 3>&2
        ;;
    -V|--version)
        version=t
        ;;
    -h|--help)
        help=t
        ;;
    -*)
        usage=t
        ;;
    *)
        break
        ;;
    esac
    shift
done


# Load configuration
. oracleasm-Xshlib

if [ "$help" = "t" -o "$usage" = "t" ]
then
    usage
fi

if [ "$version" = "t" ]
then
    version
fi


#
# Delete a disk by name
#
delete_disk()
{
    if [ "$#" != "1" -o -z "$1" ]
    then
        die "delete_disk(): Requires an argument"
    fi

    DISKNAME="$(upper_disk "$1")"
    if [ -z "$DISKNAME" ]
    then
        return 1
    fi

    disk_path="$(asm_disk_path "${ORACLE_ASMMANAGER}" "${DISKNAME}")"
    if [ ! -e "$disk_path" ]
    then
        die "Disk \"$DISKNAME\" does not exist or is not instantiated" >&2
    fi

    #
    # Nested if suckage, but there's a reason.  We want to validate
    # ALL of these checks before we squash the disk header.  If any of
    # the checks fails, we will still remove the name, but not
    # squash the header.  It isn't ours to squash.
    #
    INFO="$(oracleasm-read-label "${disk_path}" 2>&3)"
    if [ -z "$INFO" ]
    then
        echo "Disk \"$DISKNAME\" defines an unmarked device" >&2
    else
        LABEL="$(echo "$INFO" | cut -f1 -d:)"
        if [ -z "$LABEL" ]
        then
            echo "Disk \"$DISKNAME\" defines a device with no label" >&2
        else
            if [ "$LABEL" != "$DISKNAME" ]
            then
                echo "Disk \"$DISKNAME\" is labeled for disk \"$LABEL\"" >&2
            else
                echo -n "Clearing disk header: "
                oracleasm-write-label -d "${disk_path}" 2>&3
                if [ $? != 0 ]
                then
                    echo "failed"
                    die "Unable to clear disk \"${DISKNAME}\""
                fi

                echo "done"
            fi
        fi
    fi

    # No matter what happened above, we can drop the disk
    echo -n "Dropping disk: "
    oracleasm-clean-disk -l "$ORACLE_ASMMANAGER" "${DISKNAME}" 2>&3
    if [ $? != 0 ]
    then
        echo "failed"
        die "Unable to delete disk \"${DISKNAME}\""
    fi

    echo "done"
    return 0
}

#
# Delete by device name
#
delete_device()
{
    if [ "$#" != "1" -o -z "$1" ]
    then
        die "delete_device(): Requires an argument"
    fi

    DEV="$1"

    INFO="$(oracleasm-read-label "${DEV}" 2>&3)"
    if [ $? != 0 ]
    then
        echo "Unable to access device \"${DEV}\"" >&2
        return 1
    fi

    if [ -z "$INFO" ]
    then
        echo "Device \"$DEV\" is not marked as an ASM disk" >&2
        return 1
    fi

    LABEL="$(echo "$INFO" | cut -f1 -d:)"
    if [ -z "$LABEL" ]
    then
        echo "Device \"$DEV\" defines a device with no label" >&2
        return 1
    fi

    RC=0
    # If instantiated, go through the name
    if oracleasm-querydisk -l "${ORACLE_ASMMANAGER}" "${LABEL}" 1>&3 2>&3
    then
        delete_disk "${LABEL}"
    else
        echo -n "Clearing disk header: "
        oracleasm-write-label -d "${DEV}" 2>&3
        if [ "$RC" != 0 ]
        then
            echo "failed"
            die "Unable to clear disk \"${DEV}\""
        fi
        echo "done"
    fi

    return 0
}


if [ $# != 1 -o -z "$1" ]
then
    usage
fi
TARGET="$1"
shift

case "$TARGET" in
*/*)
    delete_device "$TARGET"
    ;;
*)
    delete_disk "$TARGET"
    ;;
esac

exit 0
