#!/bin/bash

# Author: An-Cheng Huang <ancheng@vyatta.com>
# Date: 2007
# Description: config notify script

# **** License ****
# Copyright (c) 2019, AT&T Intellectual Property. All rights reserved.
#
# Copyright (c) 2014,2016 by Brocade Communications Systems, Inc.
# All rights reserved.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007-2013 Vyatta, Inc.
# All Rights Reserved.
#
# SPDX-License-Identifier: GPL-2.0-only
# **** End License ****

[ -r /etc/default/vyatta ] && source /etc/default/vyatta

cur_tty=$(ps -eo pid,tty= | grep "$2" | head -n1 | awk '{ print $2 }')
cur_user=$(getent passwd "$1" | cut -d: -f1)
ulist=( $(ps -a -o args,tty,ruser n \
                     | grep "^newgrp $VYATTA_CFG_GROUP_NAME" | cut -c 29-) )

for (( i = 0; i < ${#ulist[@]}; i += 2 )); do
  utty=${ulist[i]}
  if [ "$utty" == "$cur_tty" ]; then
    continue
  fi
  user=$(grep "[^:]\+:[^:]*:${ulist[i+1]}:" /etc/passwd | cut -d ':' -f 1)
  sudo write "$user" "$utty" <<EOF
Active configuration has been changed by user '$cur_user' on '$cur_tty'.
Please make sure you do not have conflicting changes. You can also discard
the current changes by issuing 'exit discard'.
EOF
done
