#!/usr/bin/env bash
# wslu - Windows 10 linux Subsystem Utility
# Component of Windows 10 linux Subsystem Utility
# <https://github.com/patrick330602/wslu>
# Copyright (C) 2018 Patrick Wu
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# global config
wslu_version=1.9

## color
black=`echo -e '\e[30m'`
red=`echo -e '\e[31m'`
green=`echo -e '\e[32m'`
brown=`echo -e '\e[33m'`
blue=`echo -e '\e[34m'`
purple=`echo -e '\e[35m'`
cyan=`echo -e '\e[36m'`
yellow=`echo -e '\e[1;33m'`
white=`echo -e '\e[1;37m'`
dark_gray=`echo -e '\e[1;30m'`
light_red=`echo -e '\e[1;31m'`
light_green=`echo -e '\e[1;32m'`
light_blue=`echo -e '\e[1;34m'`
light_purple=`echo -e '\e[1;35m'`
light_cyan=`echo -e '\e[1;36m'`
light_gray=`echo -e '\e[37m'`
orange=`echo -e '\e[38;5;202m'`
light_orange=`echo -e '\e[38;5;214m'`
bold=`tput bold`
reset=`tput sgr0`

## indicator
info="${green}[info]${reset}"
error="${red}[error]${reset}"
warn="${orange}[warn]${reset}"
debug="${cyan}[debug]${reset}"

## basic distro detection
distro="$(cat /etc/os-release | head -n1 | sed -e 's/NAME=\"//g')"
if [[ "$distro" == *WLinux* ]]; then
	distro=wlinux
elif [[ "$distro" == Ubuntu* ]]; then
	distro="ubuntu"
elif [[ "$distro" = *Debian* ]]; then
	distro="debian"
elif [[ "$distro" == *Kali* ]]; then
	distro="kali"
elif [[ "$distro" == openSUSE* ]]; then
	distro="opensuse"
elif [[ "$distro" == SLES* ]]; then
	distro="sles"
fi


function help
{
	echo -e "`basename "$1"` - Component of Windows 10 Linux Subsystem Utility
Usage: $2"
}

#no_longer_use_but_might_use_it_in_the_future="For more help for `basename "$1"`, visit the following site: https://github.com/patrick330602/wslu/wiki/`basename "$1"`"

version="25"

help_short="wslsys (-h|-v|-S|-U|-b|-B|-fB|-R|-K|-P) -s"
branch=`/mnt/c/Windows/System32/reg.exe query "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v "BuildBranch" 2>&1 | sed -n 3p | sed -e "s/BuildBranch//" | sed -e 's/^[[:space:]]*//' | awk '{$1=""; sub("  ", " "); print}' | sed -e 's|\r||g'`
build=`/mnt/c/Windows/System32/reg.exe query "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v "CurrentBuild" 2>&1 | egrep -o '([0-9]{5})'`
full_build=`/mnt/c/Windows/System32/reg.exe query "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v "BuildLabEx" 2>&1 | sed -n 3p | sed -e "s/BuildLabEx//" | sed -e 's/^[[:space:]]*//' | awk '{$1=""; sub("  ", " "); print}' | sed -e 's|\r||g'`
installdate=`/mnt/c/Windows/System32/reg.exe query "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v "InstallDate" | egrep -o "(0x|0X)[a-fA-F0-9]+" | xargs printf "%d\n" | bc | xargs -I '{}' date --date="@{}"`
release="$(cat /etc/os-release | grep "PRETTY_NAME=" | sed -e 's/PRETTY_NAME=//g' -e 's/"//g')"
kernel="$(</proc/sys/kernel/ostype) $(</proc/sys/kernel/osrelease)"

uptime=$(</proc/uptime)
uptime=${uptime//.*}
days=$((${uptime}/86400))
hours=$((${uptime}/3600%24))
minutes=$((${uptime}/60%60))
uptime="${days}d ${hours}h ${minutes}m"

case "$distro" in
	'ubuntu'|'kali'|'debian'|'wlinux')
		packages="$((packages+=$(dpkg --get-selections | grep -cv deinstall$)))";;
	'opensuse'|'sles')
		packages="$(rpm -qa | wc -l)"
		;;
esac

function printer
{
	if [[ $2 != "-s" ]]; then
		echo $1
	else
		echo $1 | sed 's/^.*: //'
	fi
}

case $1 in
        -h|--help) help $0 "$help_short"; exit;;
	    -v|--version) echo "wslsys v$wslu_version.$version"; exit;;
		-I|--sys-installdate) printer "Release Install Date: $installdate" $2;exit;;
		-b|--branch) printer "Branch: $branch" $2;exit;;
		-B|--build) printer "Build: $build" $2;exit;;
		-fB|--full-build) printer "Full Build: $full_build" $2;exit;;
		-U|--uptime) printer "Uptime: $uptime" $2;exit;;
		-R|--release) printer "Linux Release: $release" $2;exit;;
		-K|--kernel) printer "Linux Kernel: $kernel" $2;exit;;
		-P|--package) printer "Packages Count: $packages" $2;exit;;
		*) echo -e "Release Install Date: $installdate\nBranch: $branch\nBuild: $build\nFull Build: $full_build\nUptime: $uptime\nLinux Release: $release\nLinux Kernel: $kernel\nPackages Count: $packages";exit;;
esac
