#!/bin/bash

ALACRITTY_SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
alacritty_req_list_debian=(cmake pkg-config libfreetype6-dev libfontconfig1-dev libxcb-xfixes0-dev libxkbcommon-dev)
alacritty_req_list_arch=(cmake pkg-config libfreetype6-dev libfontconfig1-dev libxcb-xfixes0-dev libxkbcommon-dev)
alacritty_req_list_raspberry=(cmake pkg-config libfreetype6-dev libfontconfig1-dev libxcb-xfixes0-dev libxkbcommon-dev)

alacritty_source="https://github.com/alacritty/alacritty.git"

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 
}

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 
}

alacritty_install_rust()
{
	cd $ALACRITTY_SCRIPT_DIR
	sudo curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
	source $HOME/.cargo/env
}

alacritty_install_core()
{
	cd $ALACRITTY_SCRIPT_DIR

	alacritty_install_rust

	git clone $alacritty_source
	cd alacritty/
	cargo build --release -j$CPU_CORE_COUNT
	sudo cp target/release/alacritty /usr/local/bin
	sudo cp extra/logo/alacritty-term.svg /usr/share/pixmaps/Alacritty.svg	
	cd .. 
	sudo rm -r alacritty/
}

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

outro()
{
	graphics_install_prompt "Alacritty installation complete"
	cd $MAIN_DIR
}


install_alacritty()
{
	intro

	cd $ALACRITTY_SCRIPT_DIR

	mkdir -p $USER_DOTFILE_DIR/alacritty 

	case $OS in
	  debian)
		install_from_list "${alacritty_req_list_debian[@]}"
		;;

	  arch)
		install_from_list "${alacritty_req_list_arch[@]}"
		;;

	  rasberry)
		install_from_list "${alacritty_req_list_raspberry[@]}"
		;;

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

	alacritty_install_core
	cp $DOTFILE_DIR/alacritty/alacritty_$PLATFORM.yml $USER_DOTFILE_DIR/alacritty/alacritty.yml
	alacritty_set_opacity
	alacritty_set_font_size	

	outro
}
