#!/usr/bin/env bash
# wslu - Windows 10 linux Subsystem Utility
# Component of Windows 10 linux Subsystem Utility
# <https://github.com/wslutilities/wslu>
# Copyright (C) 2019 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/>.

# Version
wslu_version=2.0.0

# Speed up script by not using unicode.
LC_ALL=C
LANG=C

# force bash to not showing code manually.
set +x

# checking interopability
#cat /proc/sys/fs/binfmt_misc/WSLInterop | grep enabled >/dev/null || (echo "WSL Interopability is disabled. Please enable it before using WSL."; exit 1)

# variables
## 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=`echo -e '\033[1m'`
reset=`echo -e '\033(B\033[m'`

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

# functions

function help {
	echo -e "`basename "$1"` - Part of wslu, a collection of utilities for Windows 10 Windows Subsystem for Linux
Usage: $2

For more help for `basename "$1"`, visit the following site: https://github.com/wslutilities/wslu/wiki/`basename "$1"`"
}

function double_dash_p {
	echo "$(echo $@ | sed -e 's|\\|\\\\|g')"
}

function interop_prefix {
	if [ -f /etc/wsl.conf ]; then
		tmp=$(awk -F '=' '/root/ {print $2}' /etc/wsl.conf)
		if [ "$tmp" == "" ]; then
			echo "/mnt/"
		else
			echo $tmp
		fi
	else
		echo "/mnt/"
	fi
}

function winps_exec {
	$(interop_prefix)c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NonInteractive -Command $@
}

# when --debug, debug.
if [ "$1" == "--debug" ]; then
	echo "${debug}Debug Mode Enabled."
	shift
	set -x
fi

# basic distro detection
distro="$(cat /etc/os-release | head -n1 | sed -e 's/NAME=\"//g')"
case $distro in
    *WLinux*) distro="wlinux";;
    Ubuntu*) distro="ubuntu";;
    *Debian*) distro="debian";;
    *Kali*) distro="kali";;
    openSUSE*) distro="opensuse";;
    SLES*) distro="sles";;
    Alpine*) distro="alpine";;
    Arch*) distro="archlinux";;
    Scientific*) distro="scilinux";;
    *Fedora*) distro="fedora";;
	*Generic*) [ "fedora" == "$(cat /etc/os-release | grep -e "LIKE=" | sed -e 's/ID_LIKE=//g')" ] && distro="fedora" || distro="unknown";;
    *) distro="unknown";;
esac
version="03"

var_type=1

help_short="wslvar (--sys|--shell) [NAME]\nwslvar (--help|--version|--getsys|--getshell)"

function call_shell {
	winps_exec "(Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders').'$@'" | cat
}

function view_shell {
	winps_exec "Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'"| tail -n +3 | head -n -10
}

function call_sys {
	winps_exec "Write-Output \$Env:$@" | cat
}

function view_sys {
	winps_exec "Get-ChildItem env:" | tail -n +2 | head -n -2
}

function cl_destoryer {
	echo "$@" | tr -d "\r"
}

function caller {
	if [ "$@" != "" ]; then
		case $var_type in
			1) p="$(cl_destoryer $(call_sys $@))";;
			2) p="$(cl_destoryer $(call_shell $@))";;
			*) echo "${error}Invalid variable type. Aborted."; exit 22;;
		esac
		echo $p
	else
		echo "${error} No Input. Aborted."
		exit 21
	fi
}

while [ "$1" != "" ]; do
	case "$1" in
		-s|--sys) var_type=1; shift;;
		-l|--shell) var_type=2; shift;;
		-S|--getsys) view_sys; exit;;
		-L|--getshell) view_shell; exit;;
		-h|--help) help $0 "$help_short"; exit;;
		-v|--version) echo "wslu v$wslu_version; wslvar v$version"; exit;;
		*) caller "$@"; exit;;
	esac
done

exit 21

