<commit_before>#!/bin/bash
###############################################################################
#
# Installs brewmaster and sets it up to run automatically at login
# FOR THE CURRENT USER.
# 
#     Usage: ./install.sh 
#
#  Homepage: http://bjschafer.github.io/brewmaster
#
#    Author: <NAME> <<EMAIL>> (bjs)
#
# Changelog:
# 
###############################################################################
OPTIND=1

SYS=$(uname -s)

if [ $SYS == "Darwin" ]; then
	brewmaster_home="/usr/local/brewmaster"
elif [ $SYS == "Linux" ]; then
	brewmaster_home="$HOME/.linuxbrew/brewmaster"
fi

function install_homebrew()
{
	if hash brew 2>/dev/null; then
		return 0
	else
		if [ $SYS == "Darwin" ]; then
			ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # note this prompts the user
		elif [ $SYS == "Linux" ]; then
			if hash ruby 2>/dev/null && hash gcc 2>/dev/null; then
				ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
			else
				echo "Please install ruby and gcc using your distribution's package manager and try again."
				exit 4
			fi
		else
			echo "Brewmaster is not supported on your System since Homebrew isn't supported!"
			exit 2
		fi
	fi
}

function install_xcodeTools()
{
	if hash gcc 2>/dev/null; then
		return 0
	else
		if [ $SYS == "Darwin" ]; then
			xcode-select --install
		else
			echo "Missing developer tools. Try a sudo apt-get install build-essential"
			exit 4
		fi
	fi
}

function find_local_config()
{
	config_type="local"

		read -e -p "Please enter the path to the .yaml file: [$brewmaster_home/config.yaml]" config_location

	if [ $config_location == "" ]; then
		config_location="$brewmaster_home/config.yaml"
	fi

	if [ ! -e $config_location ]; then
		echo 
		read -p "File not found. Please ensure the config file is there and you have permissions, then press enter to try again."
		if [ ! -e $config_location ]; then
			echo "File not found. Exiting."
			exit 1
		fi
	fi

}

# Guess we gotta create ANOTHER (?) scheduled task to keep it up to date?? Or just use the same one somehow?
# Assumes that the config file is stored in config.yaml inside the repo.
function find_git_config()
{
	config_type="git"
	read -e -p "Please enter the git repository location: " git_repo

	# Let's just let git handle verification, should generally be safe (?)
	git clone $git_repo $config_location
	repo_name=$(basename $git_repo .git)
	config_location="$brewmaster_home/$repo_name/config.yaml"

	sed -i -e "s,UPDATE_CALL,cd $brewmaster_home/$repo_name && git pull,g" com.bjschafer.brewmaster.sync.plist
}

function find_http_config()
{
	config_type="http"
	read -e -p "Please enter the URL in the format http[s]://example.com/config.yaml: " url_repo

	echo $config_location | grep -E -q 'http[s]?:\/\/([A-Za-z0-9]+\.)+[A-Za-z]+[\/\w\.\?=]*'
	if [ $? -ne 0 ]; then # not a valid url
		echo "Invalid URL. Try again."
		find_http_config
	fi

	# if they want a URL, let's store it locally where we please
	config_location="$brewmaster_home/config.yaml"

	curl -output $config_location $url_repo
	if [ $? -ne 0]; then # unable to access
		echo "Try again?"
		find_http_config
	fi

	sed -i -e "s,UPDATE_CALL,curl -silent -output $config_location $url_repo,g" com.bjschafer.brewmaster.sync.plist
}

function get_config()
{
	if [ $1 ]; then
		source $1
	else
		# Guess we have to do this the hard way.
		echo "Where is your config stored?"
		select lgh in "Locally" "Git" "HTTP"; do
			case $lgh in
				Locally ) find_local_config; break;;
				Git ) find_git_config; break;;
				HTTP ) find_http_config; break;;
				* ) echo "Invalid."; exit 1; break;; # should do this better
			esac
		done
	fi
}

function install_brewmaster()
{
	sed -i -e "s,CONFIG_LOCATION,$config_location,g" com.bjschafer.brewmaster.plist
	
	mkdir -p "$brewmaster_home/bin"
	cp bin/brewmaster.sh "$brewmaster_home/bin"
	cp bin/get_yaml.rb "$brewmaster_home/bin"

	if [ $SYS == "Darwin" ]; then
		cp com.bjschafer.brewmaster.plist $HOME/Library/LaunchAgents
		launchctl load $HOME/Library/LaunchAgents/com.bjschafer.brewmaster.plist

		if [ $config_type == "http" ] || [ $config_type == "git" ]; then
			cp com.bjschafer.brewmaster.sync.plist $HOME/Library/LaunchAgents
			launchctl load $HOME/Library/LaunchAgents/com.bjschafer.brewmaster.sync.plist
		fi
	else
		if [ $config_type == "http" ] || [ $config_type == "git" ]; then<commit_msg>Fixes for install.sh<commit_after>#!/bin/bash
###############################################################################
#
# Installs brewmaster and sets it up to run automatically at login
# FOR THE CURRENT USER.
# 
#     Usage: ./install.sh 
#
#  Homepage: http://bjschafer.github.io/brewmaster
#
#    Author: <NAME> <<EMAIL>> (bjs)
#
# Changelog:
# 
###############################################################################
OPTIND=1

SYS=$(uname -s)

if [ "$SYS" == "Darwin" ]; then
	brewmaster_home="/usr/local/brewmaster"
elif [ "$SYS" == "Linux" ]; then
	brewmaster_home="$HOME/.linuxbrew/brewmaster"
fi

function install_homebrew()
{
	if hash brew 2>/dev/null; then
		return 0
	else
		if [ "$SYS" == "Darwin" ]; then
			ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # note this prompts the user
		elif [ "$SYS" == "Linux" ]; then
			if hash ruby 2>/dev/null && hash gcc 2>/dev/null; then
				ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
			else
				echo "Please install ruby and gcc using your distribution's package manager and try again."
				exit 4
			fi
		else
			echo "Brewmaster is not supported on your System since Homebrew isn't supported!"
			exit 2
		fi
	fi
}

function install_xcodeTools()
{
	if hash gcc 2>/dev/null; then
		return 0
	else
		if [ "$SYS" == "Darwin" ]; then
			xcode-select --install
		else
			echo "Missing developer tools. Try a sudo apt-get install build-essential"
			exit 4
		fi
	fi
}

function find_local_config()
{
	config_type="local"

		read -e -p "Please enter the path to the .yaml file: [$brewmaster_home/config.yaml]" config_location

	if [ "$config_location" == "" ]; then
		config_location="$brewmaster_home/config.yaml"
	fi

	if [ ! -e "$config_location" ]; then
		echo 
		read -p "File not found. Please ensure the config file is there and you have permissions, then press enter to try again."
		if [ ! -e "$config_location" ]; then
			echo "File not found. Exiting."
			exit 1
		fi
	fi

}

# Guess we gotta create ANOTHER (?) scheduled task to keep it up to date?? Or just use the same one somehow?
# Assumes that the config file is stored in config.yaml inside the repo.
function find_git_config()
{
	config_type="git"
	read -e -p "Please enter the git repository location: " git_repo

	# Let's just let git handle verification, should generally be safe (?)
	git clone $git_repo $config_location
	repo_name=$(basename $git_repo .git)
	config_location="$brewmaster_home/$repo_name/config.yaml"

	sed -i -e "s,UPDATE_CALL,cd $brewmaster_home/$repo_name && git pull,g" com.bjschafer.brewmaster.sync.plist
}

function find_http_config()
{
	config_type="http"
	read -e -p "Please enter the URL in the format http[s]://example.com/config.yaml: " url_repo

	echo $config_location | grep -E -q 'http[s]?:\/\/([A-Za-z0-9]+\.)+[A-Za-z]+[\/\w\.\?=]*'
	if [ "$?" -ne 0 ]; then # not a valid url
		echo "Invalid URL. Try again."
		find_http_config
	fi

	# if they want a URL, let's store it locally where we please
	config_location="$brewmaster_home/config.yaml"

	curl -output $config_location $url_repo
	if [ "$?" -ne 0]; then # unable to access
		echo "Try again?"
		find_http_config
	fi

	sed -i -e "s,UPDATE_CALL,curl -silent -output $config_location $url_repo,g" com.bjschafer.brewmaster.sync.plist
}

function get_config()
{
	if [ "$1" ]; then
		source $1
	else
		# Guess we have to do this the hard way.
		echo "Where is your config stored?"
		select lgh in "Locally" "Git" "HTTP"; do
			case $lgh in
				Locally ) find_local_config; break;;
				Git ) find_git_config; break;;
				HTTP ) find_http_config; break;;
				* ) echo "Invalid."; exit 1; break;; # should do this better
			esac
		done
	fi
}

function install_brewmaster()
{
	sed -i -e "s,CONFIG_LOCATION,$config_location,g" com.bjschafer.brewmaster.plist
	
	mkdir -p "$brewmaster_home/bin"
	cp bin/brewmaster.sh "$brewmaster_home/bin"
	cp bin/get_yaml.rb "$brewmaster_home/bin"

	if [ "$SYS" == "Darwin" ]; then
		cp com.bjschafer.brewmaster.plist $HOME/Library/LaunchAgents
		launchctl load $HOME/Library/LaunchAgents/com.bjschafer.brewmaster.plist

		if [ "$config_type" == "http" ] || [ $config_type == "git" ]; then
			cp com.bjschafer.brewmaster.sync.plist $HOME/Library/LaunchAgents/com.bjschafer.brewmaster.sync.plist
			launchctl load $HOME/Library/LaunchAgents/com.bjschafer.brewmaster.sync.plist
		fi
	else
		if [ "$config_type" == "http" ] || [ $config_type == "git" ]; then
{'fraction_non_alphanumeric': 0.16092469980922455, 'fraction_numerical': 0.004039950622825721, 'mean_word_length': 3.8408473655621944, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '163400', 'n_tokens_mistral': 3334, 'n_tokens_neox': 2997, 'n_words': 907}