cat <<EOT > /usr/local/bin/sclic
#!/bin/bash
printf '%.s-' {1..78};echo;
echo '- Mobile User License Tool v1.0            - Copyright Heiko Ankenbrand 2020 -';
printf '%.s-' {1..78};echo;
SHOW_SUM="1";

mSITIONAL=()
while [[ \$# -gt 0 ]]
do
key="\$1"
case \$key in
    -g|--gateways)
       shift # past argument
       echo;echo 'Gateways from topology:';echo;
       mgmt_cli -r true show gateways-and-servers details-level full --format json | \$CPDIR/jq/jq -r '.objects[] | select(.type | contains("Member","simple-gateway")) | ."ipv4-address",."name"  ' |xargs -n 2 | grep -v 0.0.0. | awk '{print \$1 "\t\t" \$2}' ;
       echo;printf '%.s-' {1..78};echo;
       SHOW_SUM="0";
    ;;

    -m|--management)
       shift # past argument
       echo;echo "Licenses on management server:";echo;
       cplic print;
       printf '%.s-' {1..78};echo;
       SHOW_SUM="0";
    ;;


    -r|--remote)
       REMOTEIP="\$2"
       shift # past argument
       shift # past value
       $CPDIR/bin/cprid_util -server \$REMOTEIP -verbose rexec -rcmd bash -c 'cplic print' > /tmp/cplic_print_lic.txt ;
       if [ ! -s /tmp/cplic_print_lic.txt ]
       then
       echo;echo 'This IP is not from a gateway. Use the option "sclic -g" to show all gateways.';echo;
       else
       echo; echo -n "Licenses on gateway "; echo -n \$REMOTEIP;echo ":";echo;
       more /tmp/cplic_print_lic.txt
       echo;
       fi
       printf '%.s-' {1..78};echo;
       SHOW_SUM="0";
    ;;

    -s|--summary)
       REMOTEIP="\$2"
       shift # past argument
       shift # past value       
       $CPDIR/bin/cprid_util -server \$REMOTEIP -verbose rexec -rcmd bash -c 'fw tab -t userc_users -s' |grep localhost > /tmp/sclic.txt ;
       if [ ! -s /tmp/sclic.txt ]
       then
       echo;echo 'This IP is not from a gateway. Use the option "sclic -g" to show all gateways.';
       else
       $CPDIR/bin/cprid_util -server \$REMOTEIP -verbose rexec -rcmd bash -c 'fw tab -t sslt_om_ip_params -s' |grep localhost > /tmp/snxlic.txt ;
       $CPDIR/bin/cprid_util -server \$REMOTEIP -verbose rexec -rcmd bash -c 'fw tab -t cvpn_session -s' |grep localhost > /tmp/moblic.txt ;
       echo;
       echo; echo -n "User counters on gateway "; echo -n \$REMOTEIP;echo ":";echo;
       echo -n '  Endpoint Security VPN (SecureClient) now on the gateway    : ';
       more /tmp/sclic.txt | awk {'print \$4'};
       echo -n '  Endpoint Security VPN users on the gateway (peak)          : ';
       more /tmp/sclic.txt | awk {'print \$5'};
       fi
       if [ ! -s /tmp/snxlic.txt ]
       then
       echo -n '';
       else
       echo -n '  SNX (SSL VPN not MOB) users now on the gateway             : ';
       more /tmp/snxlic.txt | awk {'print \$4'};
       echo -n '  SNX (SSLVPN not MOB) users on the gateway (peak)           : ';
       more /tmp/snxlic.txt | awk {'print \$5'};
       fi
       if [ ! -s /tmp/moblic.txt ]
       then
       echo -n '';
       else
       echo -n '  Mobile Access Portal users now on the gateway              : ';
       more /tmp/moblic.txt | awk {'print \$4'};
       echo -n '  Mobile Access Portal users on the gateway (peak)           : ';
       more /tmp/moblic.txt | awk {'print \$5'};
       fi
       echo;
       printf '%.s-' {1..78};echo;
       SHOW_SUM="1";
    ;;


    *)    # unknown option
    #echo "Unbekannt"
    POSITIONAL+=("\$1") # save it in an array for later
    shift # past argument
    SHOW_SUM="1";
    ;;

esac
done


if [ \$SHOW_SUM == "1" ] 
then
    echo;echo  'Remote Access Licenses on SMS:';echo;
    echo -n '  Endpoint Security VPN (SecureClient)  (CPVP-VSC-5-NGX-XXX) : ';
    cplic print |grep never | grep -o -E 'CPVP-VSC-5-NGX\+.*' |sed 's/CPVP-VSC-5-NGX+//g' |awk '{ total = total + \$1 } END { print total }';
    echo -n '  Endpoint Security VPN (SecureClient)  (CPVP-VSC-XXX-NGX)   : ';
    cplic print |grep never |grep -v 'CPVP-VSC-5-NGX\+' |grep -o -E 'CPVP-VSC-.*NGX' | sed 's/CPVP-VSC-//g' | sed 's/-NGX//g' | awk '{ total = total+ \$1 } END { print total }'
    echo -n '  SNX (not MOB)                         (CPVP-SNX-XXX-NGX)   : ';
    cplic print |grep never | grep SNX | sed 's/.*\sCPVP\-SNX\-//' | sed 's/\-NGX.*//' |awk '{ total2 = total2 + \$1 } END { print total2 }'
    echo -n '  Mobile Access Blade                   (CPSB-SSLVPN-XXX)    : ';
    cplic print |grep never | grep SSLVPN | sed 's/.*\sCPSB\-SSLVPN\-//' | awk '{ total3 = total3 + \$1 } END { print total3 }'
    # printf '%.s-' {1..78};echo;
    echo;echo 'User Center keys: ';echo;
    echo '  Endpoint Security VPN (SecureClient): ';
    cplic print |grep never | grep 'CPVP-VSC-' | grep -o -E 'SWB CK-............'| awk '{print \$2}' |sed 's/CK-//g' | awk '{print "    " \$1}'; 
    echo '  SNX: '; 
    cplic print |grep never | grep 'CPVP-SNX-' | sed 's/.*\sCK-//' | awk '{print "    " \$1}'; 
    echo '  MOB: ';
    cplic print |grep never | grep 'CPSB-SSLVPN-' | sed 's/.*\sCK-//' | awk '{print "    " \$1}'; 
    echo;printf '%.s-' {1..78};echo;
fi

EOT
chmod 770 /usr/local/bin/sclic


