msg() {
    ALL_OFF="\e[1;0m"
    BOLD="\e[1;1m"
    GREEN="${BOLD}\e[1;32m"
	local mesg=$1; shift
	printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}" "$@" >&2
}

add_users() {
    _l="/etc/login.defs"
    _p="/etc/passwd"
 
    ## get mini UID limit ##
    l=$(grep "^UID_MIN" $_l)
 
    ## get max UID limit ##
    l1=$(grep "^UID_MAX" $_l)
 
    ## use awk to print if UID >= $MIN and UID <= $MAX and shell is not /sbin/nologin   ##
    users=$(awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max  && $7 != "/sbin/nologin" ) print $0 }' "$_p" | cut -d: -f1)
    for i in $users; do
        msg "Adding user '$i' to user-group 'sambashare'"
        usermod -a -G sambashare $i
    done
}

add_group() {
    getent group "sambashare" &>/dev/null || groupadd -r sambashare
    add_users
    chgrp sambashare var/lib/samba/usershare
}

post_upgrade() {
  add_group
  echo ""
  msg "Please add new users to 'sambashare' group."
  echo ""
}

post_install() {
  msg "Attempting to enable Samba services..."
  systemctl is-active smb >/dev/null || systemctl enable smb
  systemctl is-active nmb >/dev/null || systemctl enable nmb
  add_group
  echo ""
  msg "Samba services should now be enabled."
  msg "Please add new users to 'sambashare' group."
  msg "Reboot your system, so changes will take effect.."
  echo ""
}

post_remove() {
  msg "Attempting to disable Samba services..."
  ! systemctl is-enabled smb >/dev/null || systemctl disable smb
  ! systemctl is-enabled nmb >/dev/null || systemctl disable nmb
  ! getent group sambashare || groupdel sambashare
  echo ""
  msg "Samba services should now be disabled. Reboot your system."
  echo ""
}
