#!/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="28"

cname=""
iconpath="`wslupath -d -H`\\wslu\\wsl.ico"
is_icon=0
is_gui=0
customname=""
customenv=""

help_short="wslusc (-e|-n|-i|-g|-h|-v) ...COMMAND..."

while [ "$1" != "" ]; do
	case "$1" in
		-i|--icon)shift;is_icon=1;iconpath=$1;shift;;
		-n|--name)shift;customname=$1;shift;;
		-e|--env)shift;customenv=$1;shift;;
		-g|--gui)is_gui=1;shift;;
		-h|--help) help $0 "$help_short"; exit;;
		-v|--version) echo "wslusc v$wslu_version.$version"; exit;;
		*) cname=$@;break;;
	esac
done
if [[ "$cname" != "" ]]; then
	tpath=`wslupath -d -T`
	dpath=`wslupath -D`
	script_location="`wslupath -H`/wslu"
	localfile_path="/usr/share/wslu"
	script_location_win="`wslupath -d -H`\\wslu"
	
	raw_command=( $(basename "$cname") )
	new_cname="${raw[1]}"
	if [[ "$customname" != "" ]]; then
		new_cname=$customname
	else
		new_cname=$cname
	fi
	
	# Check default icon location
	if [[ ! -f $script_location/wsl.ico ]]; then
		echo "${info} Default wslusc icon \"wsl.ico\" not found in Windows directory. Copying right now..."
		[[ -d $script_location ]] || mkdir $script_location
		if [[ $localfile_path/wsl.ico ]]; then
			cp $localfile_path/wsl.ico $script_location
			echo "${info} Default wslusc icon \"wsl.ico\" copied. Located at $script_location."
		else
			echo "${error} runHidden.vbs not found. Failed to copy."
			exit 30
		fi
	fi
	# Check presence of runHidden.vbs 
	if [[ ! -f $script_location/runHidden.vbs ]]; then
		echo "${info} runHidden.vbs not found in Windows directory. Copying right now..."
		[[ -d $script_location ]] || mkdir $script_location
		if [[ -f $localfile_path/runHidden.vbs ]]; then
			cp $localfile_path/runHidden.vbs $script_location
			echo "${info} runHidden.vbs copied. Located at $script_location."
		else
			echo "${error} runHidden.vbs not found. Failed to copy."
			exit 30
		fi
	fi

	if [[ "$is_icon" == "1" ]]; then
		icon_filename="$(basename $iconpath)"
		ext="${iconpath##*.}"

		echo "${info} You choose to use custom icon: $iconpath. Processing..."
		cp $iconpath $script_location
		
		if [[ "$ext" != "ico" ]]; then
			if [[ "$ext" == "svg" ]] || [[ "$ext" == "png" ]]; then
				echo "${info} Converting $ext icon to ico..."
				convert "$script_location/$icon_filename" -resize 256X256 "$script_location/${icon_filename%.$ext}.ico"
				icon_filename="${icon_filename%.$ext}.ico"
			else
				echo "${error} wslusc only support creating shortcut using .png/.svg/.ico icon. Aborted."
				exit 22
			fi
		fi
		iconpath="$script_location_win\\$icon_filename"
	fi
	
	if [[ "$customenv" != "" ]]; then
		echo "${info} the following custom variable/command will be applied: $customenv"
	fi

	if [[ "$is_gui" == "1" ]]; then
		/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NonInteractive -Command "Import-Module 'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules\\Microsoft.PowerShell.Utility\\Microsoft.PowerShell.Utility.psd1';\$s=(New-Object -COM WScript.Shell).CreateShortcut('$tpath\\$new_cname.lnk');\$s.TargetPath='C:\\Windows\\System32\\wscript.exe';\$s.Arguments='$script_location_win\\runHidden.vbs bash.exe -c \"cd ~; export DISPLAY=:0; $customenv $cname\"';\$s.IconLocation='$iconpath';\$s.Save();"
	else
		/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NonInteractive -Command "Import-Module 'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules\\Microsoft.PowerShell.Utility\\Microsoft.PowerShell.Utility.psd1';\$s=(New-Object -COM WScript.Shell).CreateShortcut('$tpath\\$new_cname.lnk');\$s.TargetPath='C:\\Windows\\System32\\bash.exe';\$s.Arguments='-c \"cd ~; $customenv $cname\"';\$s.IconLocation='$iconpath';\$s.Save();"
	fi
	tpath="`wslupath -T`/$new_cname.lnk"
	mv "$tpath" "$dpath"
	echo "${info} Create shortcut ${new_cname}.lnk successful"
else
	echo "${error}No input, aborting"
	exit 21
fi
