#!/bin/bash
#
# Configure the oracleasm startup scripts.
#

# Force LC_ALL=C
export LC_ALL=C
 
USAGE="[-l <manager>] [-i|-I] [-e|-d] [-u <user>] [-g <group>] [-b|-p] [-s y|n] [[-o <order>] ...] [[-x <exclude>] ...]"

exec 3>/dev/null

# The only argument allowed for listing is -l, to specify an alternate
# $ORACLE_ASMMANAGER to list.
if [ $# = 0 -o \( $# = 2 -a "$1" = "-l" \) ]
then
    list=t
else
    list=
fi
help=
verbose=
version=
usage=
interactive=
oracleasm_enabled=
oracleasm_uid=
oracleasm_gid=
oracleasm_scanboot=
oracleasm_scanorder=
oracleasm_scanexclude=
oracleasm_use_logical_block_size=
while case "$#" in 0) break ;; esac
do
    case "$1" in
    -l|--manager)
        case "$#" in 1) usage=t; break ;; esac
        shift
        ORACLE_ASMMANAGER="$1"
        ;;
    -i|--interactive)
        interactive=t
        ;;
    -I|--interactive-all)
        interactive=T  # Ask advanced questions
        ;;
    -e|--enable)
        oracleasm_enabled=true
        ;;
    -d|--disable)
        oracleasm_enabled=false
        ;;
    -u|--uid|--user)
        case "$#" in 1) usage=t; break ;; esac
        shift
        oracleasm_uid="$1"
        ;;
    -g|--gid|--group)
        case "$#" in 1) usage=t; break ;; esac
        shift
        oracleasm_gid="$1"
        ;;
    -s|--scanboot)
        case "$#" in 1) usage=t; break ;; esac
        shift
        case "$1" in
        [tT]|[tT][rR][uU][eE]|[yY]|[yY][eE][sS])
            oracleasm_scanboot=true
            ;;
        [fF]|[fF][aA][lL][sS][eE]|[nN]|[nN][oO])
            oracleasm_scanboot=false
            ;;
        *) usage=t; break ;;
        esac
        ;;
    -o|--scanorder)
        case "$#" in 1) usage=t; break ;; esac
        shift
        if [ "$oracleasm_scanorder" = "none" ]
        then
            oracleasm_scanorder=
        fi

        case "$1" in
        ""|none)
            oracleasm_scanorder=none
            ;;
        *[^a-zA-Z0-9\ ]*)
            usage=t; break
            ;;
        *)
            oracleasm_scanorder="${oracleasm_scanorder:+$oracleasm_scanorder }$1"
            ;;
        esac
        ;;
    -x|--scanexclude)
        case "$#" in 1) usage=t; break ;; esac
        shift
        if [ "$oracleasm_scanexclude" = "none" ]
        then
            oracleasm_scanexclude=
        fi

        case "$1" in
        ""|none)
            oracleasm_scanexclude=none
            ;;
        *[^a-zA-Z0-9\ ]*)
            usage=t; break
            ;;
        *)
            oracleasm_scanexclude="${oracleasm_scanexclude:+$oracleasm_scanexclude }$1"
            ;;
        esac
        ;;
    -b|--logical-blocks)
	oracleasm_use_logical_block_size=true
	;;
    -p|--physical-blocks)
	oracleasm_use_logical_block_size=false
	;;
    -v|--verbose)
        verbose=t
        exec 3>&2
        ;;
    -V|--version)
        version=t
        ;;
    -h|--help)
        help=t
        ;;
    *)
        usage=t
        ;;
    esac
    shift
done

# Load configuration
. oracleasm-Xshlib

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

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

if [ "$list" = "t" ]
then
    cat <<EOF
ORACLEASM_ENABLED=${ORACLEASM_ENABLED:-false}
ORACLEASM_UID=${ORACLEASM_UID}
ORACLEASM_GID=${ORACLEASM_GID}
ORACLEASM_SCANBOOT=${ORACLEASM_SCANBOOT:-false}
ORACLEASM_SCANORDER="${ORACLEASM_SCANORDER}"
ORACLEASM_SCANEXCLUDE="${ORACLEASM_SCANEXCLUDE}"
ORACLEASM_USE_LOGICAL_BLOCK_SIZE="${ORACLEASM_USE_LOGICAL_BLOCK_SIZE:-false}"
EOF
    exit 0
fi

[ -n "$oracleasm_enabled" ] && ORACLEASM_ENABLED="$oracleasm_enabled"
[ -n "$oracleasm_uid" ] && ORACLEASM_UID="$oracleasm_uid"
[ -n "$oracleasm_gid" ] && ORACLEASM_GID="$oracleasm_gid"
[ -n "$oracleasm_scanboot" ] && ORACLEASM_SCANBOOT="$oracleasm_scanboot"
[ -n "$oracleasm_use_logical_block_size" ] && ORACLEASM_USE_LOGICAL_BLOCK_SIZE="$oracleasm_use_logical_block_size"

if [ -n "$oracleasm_scanorder" ]
then
    [ "$oracleasm_scanorder" = "none" ] && oracleasm_scanorder=
    ORACLEASM_SCANORDER="$oracleasm_scanorder"
fi

if [ -n "$oracleasm_scanexclude" ]
then
    [ "$oracleasm_scanexclude" = "none" ] && oracleasm_scanexclude=
    ORACLEASM_SCANEXCLUDE="$oracleasm_scanexclude"
fi


#
# write_sysconfig()
#
# Writes the system configuration out
#
write_sysconfig()
{
    echo -n "Writing Oracle ASM library driver configuration: "
    cat >"${ORACLEASM_CONFIG}" 2>&3 <<EOF
#
# This is a configuration file for automatic loading of the Oracle
# Automatic Storage Management library kernel driver.  It is generated
# By running /etc/init.d/oracleasm configure.  Please use that method
# to modify this file
#

# ORACLEASM_ENABLED: 'true' means to load the driver on boot.
ORACLEASM_ENABLED=${ORACLEASM_ENABLED:-false}

# ORACLEASM_UID: Default user owning the $ORACLE_ASMMANAGER mount point.
ORACLEASM_UID=${ORACLEASM_UID}

# ORACLEASM_GID: Default group owning the $ORACLE_ASMMANAGER mount point.
ORACLEASM_GID=${ORACLEASM_GID}

# ORACLEASM_SCANBOOT: 'true' means scan for ASM disks on boot.
ORACLEASM_SCANBOOT=${ORACLEASM_SCANBOOT}

# ORACLEASM_SCANORDER: Matching patterns to order disk scanning
ORACLEASM_SCANORDER="${ORACLEASM_SCANORDER}"

# ORACLEASM_SCANEXCLUDE: Matching patterns to exclude disks from scan
ORACLEASM_SCANEXCLUDE="${ORACLEASM_SCANEXCLUDE}"

# ORACLEASM_USE_LOGICAL_BLOCK_SIZE: 'true' means use the logical block size
# reported by the underlying disk instead of the physical. The default
# is 'false'
ORACLEASM_USE_LOGICAL_BLOCK_SIZE=${ORACLEASM_USE_LOGICAL_BLOCK_SIZE}

EOF

    if [ $? != 0 ]
    then
        die "Unable to write the driver configuration"
    fi

    echo "done"

    return
}


#
# configure_ask()
#
# Ask configuration questions, setting the shell vars.
#
configure_ask()
{
    cat <<EOF
Configuring the Oracle ASM library driver.

This will configure the on-boot properties of the Oracle ASM library
driver.  The following questions will determine whether the driver is
loaded on boot and what permissions it will have.  The current values
will be shown in brackets ('[]').  Hitting <ENTER> without typing an
answer will keep that current value.  Ctrl-C will abort.

EOF

    while :
    do
        echo -n "Default user to own the driver interface [$ORACLEASM_UID]: "
        read LINE
        case "$LINE" in
        "")
            break
            ;;
        *[^a-zA-Z0-9]*)
            echo "Invalid user: $LINE" >&2
            ;;
        *)
            ORACLEASM_UID=$LINE
            break
            ;;
        esac
    done

    while :
    do
        echo -n "Default group to own the driver interface [$ORACLEASM_GID]: "
        read LINE
        case "$LINE" in
        "")
            break
            ;;
        *[^a-zA-Z0-9]*)
            echo "Invalid group: $LINE" >&2
            ;;
        *)
            ORACLEASM_GID=$LINE
            break
            ;;
        esac
    done

    while :
    do
        if [ "$ORACLEASM_ENABLED" = "true" ]
        then
            CUR=y
        else
            CUR=n
        fi
        echo -n "Start Oracle ASM library driver on boot (y/n) [$CUR]: "
        read LINE
        case "$LINE" in
        "")
            break
            ;;
        y|Y)
            ORACLEASM_ENABLED=true
            break
            ;;
        n|N)
            ORACLEASM_ENABLED=false
            break
            ;;
        *)
            echo "Invalid response: $LINE" >&2
            ;;
        esac
    done

    while :
    do
        if [ -z "$ORACLEASM_SCANBOOT" ]
        then
            ORACLEASM_SCANBOOT=true
        fi
        if [ "${ORACLEASM_SCANBOOT}" = "true" ]
        then
            CUR=y
        else
            CUR=n
        fi
        echo -n "Scan for Oracle ASM disks on boot (y/n) [$CUR]: "
        read LINE
        case "$LINE" in
        "")
            break
            ;;
        y|Y)
            ORACLEASM_SCANBOOT=true
            break
            ;;
        n|N)
            ORACLEASM_SCANBOOT=false
            break
            ;;
        *)
            echo "Invalid response: $LINE" >&2
            ;;
        esac
    done
}

configure_ask_advanced()
{
    cat <<EOF

The next two configuration options take substrings to match device names.
The substring "sd" (without the quotes), for example, matches "sda", "sdb",
etc.  You may enter more than one substring pattern, separated by spaces.
The special string "none" (again, without the quotes) will clear the value.

EOF

    while :
    do
        echo -n "Device order to scan for ASM disks [$ORACLEASM_SCANORDER]: "
        read LINE
        case "$LINE" in
        "")
            break
            ;;
        none)
            ORACLEASM_SCANORDER=
            ;;
        *[^a-zA-Z0-9\ ]*)
            echo "Invalid pattern: $LINE" >&2
            ;;
        *)
            ORACLEASM_SCANORDER="$LINE"
            break
            ;;
        esac
    done

    while :
    do
        echo -n "Devices to exclude from scanning [$ORACLEASM_SCANEXCLUDE]: "
        read LINE
        case "$LINE" in
        "")
            break
            ;;
        none)
            ORACLEASM_SCANORDER=
            ;;
        *[^a-zA-Z0-9\ ]*)
            echo "Invalid pattern: $LINE" >&2
            ;;
        *)
            ORACLEASM_SCANEXCLUDE="$LINE"
            break
            ;;
        esac
    done

    while :
    do
        if [ "$ORACLEASM_USE_LOGICAL_BLOCK_SIZE" = "true" ]
        then
            CUR=y
        else
            CUR=n
        fi
        echo -n "Use device logical block size for ASM (y/n) [$CUR]: "
        read LINE
        case "$LINE" in
        "")
            break
            ;;
        y|Y)
            ORACLEASM_USE_LOGICAL_BLOCK_SIZE=true
            break
            ;;
        n|N)
            ORACLEASM_USE_LOGICAL_BLOCK_SIZE=false
            break
            ;;
        *)
            echo "Invalid response: $LINE" >&2
            ;;
        esac
    done

}



[ -n "$interactive" ] && configure_ask
[ "$interactive" = "T" ] && configure_ask_advanced

write_sysconfig

exit 0
