#!/bin/bash
#ECS IPv6 - rhel

#****************************global*****************************
tool_name="ipv6-setup"


#***************************end_value***************************
end_value=0


#*****************************error*****************************
ERROR_NON_INTERFACES=100 #interfaces do not exist
ERROR_INVAILD_OPTION=101 #invalid option
ERROR_DEV_NO_EXIST=102   #specified device does not exist


function modify_conf() {
    local conf_file=$1
    local key=$2
    local value=$3

    if [ ! "$(cat "$conf_file" | grep -i "^${key}[[:space:]]*=")" ]; then
        echo "$key=$value" >> "$conf_file"
    else
        sed -i "s/^${key}[[:space:]]*=.*/$key=$value/" "$conf_file"
    fi
}

function check_interface(){
    local interface=$1
    local interface_dir="/sys/class/net/$interface"

    if [ -d $interface_dir ];then
        echo 1
    else 
        echo 0
    fi
}

function get_interfaces(){
    local interfaces=""
    local net_dir="/sys/class/net/"

    if [ ! -d $net_dir ];then
        echo ""
        return
    fi
    interfaces=$(ls $net_dir | grep '^eth[0-9]\+$')
    echo "$interfaces"
}

function network_restart () {
    echo "network restart"
    if which systemctl > /dev/null 2>&1; then
        systemctl restart NetworkManager
    else
        service network restart
    fi
}

function ipv6_dhcp(){
    local inf=$1

    echo "Set up dynamic ipv6 for $inf"
    local ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-$inf"
    local network_file="/etc/sysconfig/network"
    if [ -f "$ifcfg_file" ];then
        modify_conf "$ifcfg_file" "IPV6INIT" "yes"
        modify_conf "$ifcfg_file" "DHCPV6C" "yes"
    else
        echo "$ifcfg_file does not exist" 
    fi
    if [ -f "$network_file" ];then
        modify_conf "$network_file" "NETWORKING_IPV6" "yes"
    else
        echo "$ifcfg_file does not exist"
    fi
    network_restart
}

function config_ipv6() {
    local interfaces=""
    interfaces=$(get_interfaces)
    if [ -z "$interfaces" ];then
        echo "[error] interfaces do not exist"
        end_value=$ERROR_NON_INTERFACES
        exit $end_value
    fi
    for inf in $interfaces;do
        if [ $(check_interface $inf) -eq 1 ];then
            ipv6_dhcp "$inf"
        fi
    done
}

function show_usage(){
    cat <<EOF
    Usage: $tool_name [OPTION...]
    Examples:
    $tool_name --help                            # show usage
    $tool_name --dev [dev]                       # specified device to config ipv6
        e.g. $tool_name --dev eth0
    $tool_name                                   # auto config all device ipv6
EOF

}

function get_opt(){
    if [ "$#" == 0 ];then
        echo "Set up dynamic ipv6 for all devs"
        config_ipv6
    else
        local option=$1
        case $option in
            --help)
                show_usage;;
            --dev)
                dev_arg=$2
                if [ -z $dev_arg ];then
                    echo "[error] dev parameter is empty"
                    exit $ERROR_INVAILD_OPTION
                fi
                if [ $(check_interface $dev_arg) -eq 1 ];then
                    ipv6_dhcp $dev_arg
                else
                   echo "[error] $dev does not exist"
                   exit $ERROR_DEV_NO_EXIST
                fi;;
            *)
                echo "Invalid option !"
                show_usage
                exit $ERROR_INVAILD_OPTION;;
        esac
    fi

}

get_opt "$@"
