Skip to content
Snippets Groups Projects
rm-module.sh 3.24 KiB
Newer Older
  • Learn to ignore specific revisions
  • Easy Build's avatar
    Easy Build committed
    #!/bin/bash
    # IT4Innovations 2019
    
    
    DIRNAME=$(dirname "$0")
    LIB_DIR="$(dirname "$0")/lib"
    
    Easy Build's avatar
    Easy Build committed
    
    # Include functions
    
    . "$LIB_DIR"/find_dep
    . "$LIB_DIR"/find_module_paths
    . "$LIB_DIR"/recursion
    . "$LIB_DIR"/remove_module
    
    Easy Build's avatar
    Easy Build committed
    
    # Display a usage message.
    usage () {
    
        if [ -n "$@" ]; then
           echo "usage error: $@"
    
    Easy Build's avatar
    Easy Build committed
        fi
        cat <<EOF
    Usage: rm-module [OPTION ...] MODULE
    Remove MODULE with dependency from system
    Options:
    -s	--single	remove only single module
    -o      --once		confirm remove only once
    -f      --force		don't ask, just do it!
    -h      --help		display this usage message and exit
    EOF
    }
    
    # Parse the command line.
    ARGS=$(getopt --options shfo \
           --long single,help,force,once \
           --name "rm-module" -- "$@")
    GETOPT_STATUS=$?
    
    
    if [ "$GETOPT_STATUS" -ne 0 ]; then
    
    Easy Build's avatar
    Easy Build committed
        exit 6
    fi
    
    eval set -- "$ARGS"
    
    while :; do
        case "$1" in
            -s|--single) SINGLE="yes" ;;
            -o|--once) ONCE="yes" ;;
            -f|--force) FORCE="yes" ;;
            -h|--help) SHOWHELP="yes" ;;
            --) shift; break ;;
            *) echo "Error: internar error; getopt permitted \"$1\" unexpectedly"
               exit 6
               ;;
        esac
        shift
    done
    
    if [ "$SHOWHELP" ]; then
        usage
        exit 0
    fi
    
    if [[ -z "$1" ]];then
        usage
        exit 1
    fi
    
    
    MODULE=$(ml show "$1" 2>&1)
    
    Easy Build's avatar
    Easy Build committed
    
    if [ $? == 1 ];then
        echo "Error: can't find module $1"
        exit 1
    fi
    
    MODULE=$(echo "$MODULE" | head -2 | tail -1 | tr -d ':' | rev | cut -d '/' -f 1,2 | rev | sed 's/.lua//')
    
    if [ "$SINGLE" ]; then
    
        DEP=$(find_dep "$MODULE")
    
    Easy Build's avatar
    Easy Build committed
        if [[ -z $DEP ]]; then
            echo -e "\r[\e[32mINDEPENDENT\e[0m] $MODULE"
    
            find_module_paths "$MODULE"
    
    Easy Build's avatar
    Easy Build committed
    	if [[ -z "$FORCE" ]];then
                echo -ne "\nRemove module $MODULE? [yes/NO]: "
                read CONTINUE
                if [ "$CONTINUE" != "yes" ]; then
                    exit 0
                fi
            fi
    
            remove_module "$MODULE"
    
    Easy Build's avatar
    Easy Build committed
    	exit 0
        else
            echo -e "\r[\e[31m DEPENDENT \e[0m] $MODULE"
    	exit 1
        fi
    fi
    
    echo -n "Building module tree..."
    
    recursion "$MODULE"
    
    Easy Build's avatar
    Easy Build committed
    echo "done"
    
    REMOVELIST=""
    
    
    for MODULE in "$OUT"; do
    
    Easy Build's avatar
    Easy Build committed
        echo -n "[           ] $MODULE"
    
        DEP=$(find_dep "$MODULE")			#find all modules dependenton this one
        for MOD in "$REMOVELIST"; do
    	DEP=$(echo "$DEP" | grep -v "$MOD")	#erase modules which is on remove list
    
    Easy Build's avatar
    Easy Build committed
        done
        if [[ -z $DEP ]]; then
    	echo -e "\r[\e[32mINDEPENDENT\e[0m] $MODULE"
    	REMOVELIST+="$MODULE "			#if module is independent add it to remove list
        else
    	echo -e "\r[\e[31m DEPENDENT \e[0m] $MODULE"
        fi
    done
    
    if [[ -z $REMOVELIST ]]; then
        echo -e "\nNothing to remove, exiting..."
        exit 0
    fi
    
    echo -e "\nRemove list:"
    
    for MODULE in "$REMOVELIST"; do
        find_module_paths "$MODULE"
    
    Easy Build's avatar
    Easy Build committed
    done
    
    if [ "$FORCE" ]; then
    
        for MODULE in "$REMOVELIST"; do
            remove_module "$MODULE"
    
    Easy Build's avatar
    Easy Build committed
        done
        exit 0
    fi
    
    if [ "$ONCE" ]; then
        echo -e "\n!!!\e[31m WARNING \e[0m!!! Modules will be permanently deleted! !!!\e[31m WARNING \e[0m!!!\n"
        echo -n "Remove modules? [yes/NO]: "
        read CONTINUE
        if [ "$CONTINUE" != "yes" ]; then
            exit 0
        fi
    fi
    
    
    for MODULE in "$REMOVELIST"; do
    
    Easy Build's avatar
    Easy Build committed
        if [[ -z "$ONCE" ]]; then
            echo -ne "\nRemove module $MODULE? [yes/NO]: "
            read CONTINUE
            if [ "$CONTINUE" != "yes" ]; then
                continue
            fi
        fi
    
        remove_module "$MODULE"
    
    Easy Build's avatar
    Easy Build committed
    done
    
    exit 0