#!/bin/bash
# Authors: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw>
# License: GPL
# Description: Get the netmask of network card interface

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions


ethx=$1
export LC_ALL=C

Usage() {
  echo "Get the netmask of network card interface" 
  echo "Usage:"
  echo "$(basename $0) INTERFACE"
  echo "Ex: $(basename $0) eth0"
}

[ -z "$ethx" ] && Usage && exit 1
if ! LC_ALL=C ip link show dev $ethx >/dev/null 2>&1; then
   exit 1
fi
_cidr="$(LC_ALL=C ip addr show dev $ethx 2>/dev/null | awk '/inet / {print $2}' | cut -d/ -f2 | head -n1)"
if [ -n "$_cidr" ]; then
  _val=$(( 0xffffffff ^ ((1 << (32 - _cidr)) - 1) ))
  echo "$(( (_val >> 24) & 255 )).$(( (_val >> 16) & 255 )).$(( (_val >> 8) & 255 )).$(( _val & 255 ))"
else
  exit 1
fi
exit 0
