#!/bin/bash

#used for cli command "set system aaa tacacs-plus server-ip"
#used for cli command "delete system aaa tacacs-plus server-ip"
#input $1:the server ip address
#no output

input=`echo "$*" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`

grep "server=" /etc/pam.d/common-account > /dev/null 
while [ $? -eq 0 ]
do
    sed -i "s/\(.*\)server=\(.*\)secret=\(.*\)/\1secret=\3/g" /etc/pam.d/common-account
    grep "server=" /etc/pam.d/common-account > /dev/null
done
if [ "$input" = "" ]; then
    sed -i "s/\(.*\)secret=\(.*\)/\1secret=\2/g" /etc/pam.d/common-account
else
    sed -i "s/\(.*\)secret=\(.*\)/\1$input secret=\2/g" /etc/pam.d/common-account
fi

grep "server=" /etc/pam.d/common-auth > /dev/null 
while [ $? -eq 0 ]
do
    sed -i "s/\(.*\)server=\(.*\)secret=\(.*\)/\1secret=\3/g" /etc/pam.d/common-auth
    grep "server=" /etc/pam.d/common-auth > /dev/null
done
if [ "$input" = "" ]; then
    sed -i "s/\(.*\)secret=\(.*\)/\1secret=\2/g" /etc/pam.d/common-auth
else
    sed -i "s/\(.*\)secret=\(.*\)/\1$input secret=\2/g" /etc/pam.d/common-auth
fi

grep "server=" /etc/pam.d/common-session > /dev/null 
while [ $? -eq 0 ]
do
    sed -i "s/\(.*\)server=\(.*\)secret=\(.*\)/\1secret=\3/g" /etc/pam.d/common-session
    grep "server=" /etc/pam.d/common-session > /dev/null
done
if [ "$input" = "" ]; then
    sed -i "s/\(.*\)secret=\(.*\)/\1secret=\2/g" /etc/pam.d/common-session
else
    sed -i "s/\(.*\)secret=\(.*\)/\1$input secret=\2/g" /etc/pam.d/common-session
fi
exit 0
