#!/bin/bash
# LGSM fn_details_distro function
# Author: Daniel Gibbs
# Website: http://gameservermanagers.com
# Version: 270515

# Description: Variables providing useful info on the Operating System such as disk and performace info.
# Used for fn_details, fn_debug and fn_email.

## Distro infomation
# Returns architecture, kernel and distro/os.
arch=$(uname -m)
kernel=$(uname -r)
if [ -n "$(command -v lsb_release)" ]; then
	os=$(lsb_release -s -d)
elif [ -f /etc/debian_version ]; then
	os="Debian $(cat /etc/debian_version)"
elif [ -f /etc/redhat-release ]; then
	os=$(cat /etc/redhat-release)
else
	os="$(uname -s) $(uname -r)"
fi

# Glibc version number
# e.g: 1.17
glibcv=$(ldd --version |grep ldd|awk '{print $NF}')

# tmux version
# e.g: tmux 1.6
if [ -z "$(command -v tmux)" ]; then
	tmuxv="\e[0;31mNOT INSTALLED!\e[0m"
elif [ "$(tmux -V|sed "s/tmux //"|sed -n '1 p'|tr -cd [:digit:])" -lt "16" ]; then
	tmuxv="$(tmux -V) (>= 1.6 required for console log)"
else
	tmuxv=$(tmux -V)
fi

## Performance

# Average server load
load=$(uptime|awk -F 'load average: ' '{ print $2 }')

# Memory

# Older versions of free do not support -h option.
if [ "$(free -h > /dev/null 2>&1; echo $?)" -ne "0" ]; then
	option="-m"
else
	option="-h"
fi
physmemtotal=$(free ${option} | awk '/Mem:/ {print $2}')
physmemused=$(free ${option} | awk '/Mem:/ {print $3}')
physmemfree=$(free ${option} | awk '/Mem:/ {print $4}')
swaptotal=$(free ${option} | awk '/Swap:/ {print $2}')
swapused=$(free ${option} | awk '/Swap:/ {print $3}')
swapfree=$(free ${option} | awk '/Swap:/ {print $4}')

# Uptime
uptime=$(</proc/uptime)
uptime=${uptime/[. ]*/}
minutes=$(( uptime/60%60 ))
hours=$(( uptime/60/60%24 ))
days=$(( uptime/60/60/24 ))

# Disk usage
# available space on the partition.
filesystem=$(df -hP ${rootdir} | grep -v "Filesystem" | awk '{print $1}')
totalspace=$(df -hP ${rootdir} | grep -v "Filesystem" | awk '{print $2}')
usedspace=$(df -hP ${rootdir} | grep -v "Filesystem" | awk '{print $3}')
availspace=$(df -hP ${rootdir} | grep -v "Filesystem" | awk '{print $4}')

# used space in serverfiles dir.
serverfilesdu=$(du -sh ${serverfiles} | awk '{print $1}')
if [ -z ${serverfilesdu} ]; then
	serverfilesdu="0M"
fi

# Backup info
if [ -d "${backupdir}" ]; then
	# used space in backups dir.
	backupdirdu=$(du -sh ${backupdir} | awk '{print $1}')
	if [ -z ${backupdirdu} ]; then
		backupdirdu="0M"
	fi
	# number of backups.
	backupcount=$(find "${backupdir}"/*.tar.gz | wc -l)
	# most recent backup.
	lastbackup=$(ls -t "${backupdir}"/*.tar.gz | head -1)
	# date of most recent backup.
	lastbackupdate=$(date -r ${lastbackup})
	# size of most recent backup.
	lastbackupsize=$(du -h "${lastbackup}" | awk '{print $1}')

fi
