#!/bin/bash

for file in ~/.bashrc /etc/profile.d/*.sh; do
    [ -r $file ] && . $file
done

echo usage: '<script> <days to save> <Y/N prompt for user if needed>'

noauto=$2

if [ $1 ]
then numdays=$1
else read -p "how many days of changeless sessions should we save? (you can use 0): " numdays
fi

onedayago=$(date -d "$numdays day ago" +%s%3N)

tryuser(){ if ! mgmt_cli login -r true domain $domain --format json | tee id.txt | grep uid
       then echo root login to $domain failed
          { if ! [ $pw ]
            then read -p "enter username to use for deleting sessions: " user </dev/tty
                 read -sp "enter $user pw: " pw </dev/tty
                 fi ; }
          ! mgmt_cli login domain $domain user $user password $pw --format json | tee id.txt | grep uid && echo -e "\n---- PROBLEM ----\ncould not login to $domain, waiting for 5 secs for visibility"; sleep 5 
            fi
}
            
onlytryroot(){ ! mgmt_cli login -r true domain $domain --format json | tee id.txt | grep uid && echo failed to login to domain $domain
}

( echo Global ; mgmt_cli show domains -r true --format json details-level full | jq -r '.objects[] | "\(.name) \(.servers[].name)"') | while read -a domainline
  do domain=${domainline[0]}
     cma=${domainline[1]}
     echo $domain $cma 
   # login to domain
   if [[ "$noauto" == [Yy] ]]
     then tryuser
     else onlytryroot
     fi   
     total=$(mgmt_cli show sessions --format json -s id.txt | jq '.total')
     seq 0 50 $total | while read offset
                         do mgmt_cli show sessions --format json details-level full -s id.txt limit 50 offset $offset |  jq -r '.objects[] | "\(."last-logout-time".posix) \(.uid) \(.changes)"' 
                         done | while read -a sessionline
       do echo ${sessionline[@]}
          lastlogout=${sessionline[0]}
          uid=${sessionline[1]}
          changes=${sessionline[2]}
          [[ $lastlogout =~ [0-9] ]] && [ $changes -lt 1 ] && [ $lastlogout -lt $onedayago ] && echo DISCARDING $lastlogout && mgmt_cli discard -s id.txt uid $uid
          done # sessionline
          mgmt_cli logout -s id.txt #delete self session
     done # domainline

# for MDS level sessions
   # login to domain
   if [[ "$noauto" == [Yy] ]]
     then tryuser
     else onlytryroot
     fi   
     total=$(mgmt_cli show sessions --format json -s id.txt | jq '.total')
     seq 0 50 $total | while read offset
                         do mgmt_cli show sessions --format json details-level full -s id.txt limit 50 offset $offset |  jq -r '.objects[] | "\(."last-logout-time".posix) \(.uid) \(.changes)"' 
                         done | while read -a sessionline
       do echo ${sessionline[@]}
          lastlogout=${sessionline[0]}
          uid=${sessionline[1]}
          changes=${sessionline[2]}
          [[ $lastlogout =~ [0-9] ]] && [ $changes -lt 1 ] && [ $lastlogout -lt $onedayago ] && echo DISCARDING $lastlogout && mgmt_cli discard -s id.txt uid $uid
          done # sessionline
          mgmt_cli logout -s id.txt # delete self session