#!/bin/bash

ALACRITTY_SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# Define the demendencies for each os that i actively use
alacritty_dependencies_debian=(cmake pkg-config libfreetype6-dev libfontconfig1-dev libxcb-xfixes0-dev libxkbcommon-dev)
alacritty_dependencies_arch=(cmake pkg-config)
alacritty_dependencies_raspberry=(cmake pkg-config libfreetype6-dev libfontconfig1-dev libxcb-xfixes0-dev libxkbcommon-dev)

#Alacritty Source file location, in this case Github
alacritty_source="https://github.com/alacritty/alacritty.git"

#Set the font size for Alacritty, this will be changed on the config file directly 
alacritty_set_font_size()
{	
	cd $USER_DOTFILE_DIR/alacritty/
	graphics_input_prompt "What size font would you like (6 to 34): " 
	read alafontSize 
	sed -i -e  "s/size: ../size: $alafontSize/g" alacritty.yml 
}

#Set the opacity for Alacritty, this will be changed on the config file directly 
alacritty_set_opacity()
{	
	cd $USER_DOTFILE_DIR/alacritty/
	graphics_input_prompt "What opacity would you like (0.1 max opacitx  to 1->no opacity ): " 
	read alaopacity  
	sed -i -e  "s/opacity: .../opacity: $alaopacity/g" alacritty.yml 
}

# Runst must be installed to compile the source code
alacritty_install_rust()
{
	cd $ALACRITTY_SCRIPT_DIR
	sudo curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
	source $HOME/.cargo/env
}

# The most important part The core of Allacritty will be installed (compiled wiht rust) 
# and executable will be put in /bin so that Launcher can index it (plus logo) 
alacritty_install_core()
{
	cd $ALACRITTY_SCRIPT_DIR

	#calling function to install rust
	alacritty_install_rust
	
	#clone the git repo. 
	git clone $alacritty_source
	# goint into the repo
	cd alacritty/
	echo "Build Flags : --release -j$CPU_CORE_COUNT"
	# Building alacritty wiht rust an the right amount of core for the compilation 
	cargo build --release -j$CPU_CORE_COUNT
	
	# Maving executable into /bin	
	sudo cp target/release/alacritty /usr/local/bin

	# Making sure that the logo is transfared as wel 
	sudo cp extra/logo/alacritty-term.svg /usr/share/pixmaps/Alacritty.svg	
	cd .. 
	sudo rm -r alacritty/
}

install_alacritty()
{
	graphics_install_prompt "Allacritty Will be installed"
	graphics_install_prompt "Source: $alacritty_source"

	#Goind the this scrit's dir
	cd $ALACRITTY_SCRIPT_DIR

	#Creatinf the configuration file path
	mkdir -p $USER_DOTFILE_DIR/alacritty 

	#installing dependencies from standart repositories	
	case $OS in
	  debian)
		install_from_repo "${alacritty_dependencies_debian[@]}"
		;;

	  arch)
		install_from_repo "${alacritty_dependencies_arch[@]}"
		;;

	  rasberry)
		install_from_repo "${alacritty_dependencies_raspberry[@]}"
		;;

	  *)
		echo "Invalid os is selectred"
		exit_abord
		;;
	esac

	#Calling function to compile Allacritty wiht rust
	alacritty_install_core

	#Copying the configuration file to the configuration of the user
	cp $DOTFILE_DIR/alacritty/alacritty_$PLATFORM.yml $USER_DOTFILE_DIR/alacritty/alacritty.yml

	#Calling funtion to set the opacity
	alacritty_set_opacity

	#Calling function to set the fint size
	alacritty_set_font_size	
	
	graphics_install_prompt "Alacritty installation complete"

	# Going back the the main dir where the installation script is
	cd $MAIN_DIR
}
