#!/bin/sh

OS=$(uname -s)
PROJ_DIR="$(pwd)"
CHEATNT_DIR="$PROJ_DIR/.cheatnt"
FILEPATH=$1

is_tracked(){
	file=$1
	filepath=$(get_full_filepath $file $PROJ_DIR)
	grep -q "$filepath" "$CHEATNT_DIR/tracked"
	if [[ $(echo $?) -eq 0 ]]; then echo 1; else echo 0; fi
}

contains_file(){
	folder=$1
	file=$2
	if [[ -f "$folder/$file" || "$file" == "$folder"* ]]; then
		echo 1
	else
		echo 0
	fi
}

get_full_filepath(){
	file=$1
	folder=$2
	if [[ file == "/"* ]]; then
		echo $file
	else
		echo $folder/$file
	fi
}

if [[ -e $CHEATNT_DIR ]]; then
	if [[ $OS == "Linux" ]]; then
		echo "linux implementation coming soon..."
	elif [[ $OS == "Darwin" ]]; then
		if [[ -f $FILEPATH ]]; then
			istracked=$(is_tracked $FILEPATH)
			iscontained=$(contains_file $PROJ_DIR $FILEPATH)
			echo istracked=$istracked iscontained=$iscontained
			if [[ $istracked -eq 1 ]]; then
				filepath=$(get_full_filepath $FILEPATH $PROJ_DIR)
				escaped=$(printf '%s' "$filepath" | sed 's/[.[\*^$]/\\&/g')
				# echo $escaped
				sed -i '' -E "s|.+$escaped||g" $CHEATNT_DIR/tracked && echo "untracked $filepath"
			else
				echo "$FILEPATH is not tracked!"
			fi
		else
			echo "$1 : file does not exist!"
			exit 1
		fi
	elif [[ $OS == "Windows" ]]; then
		echo die trash
	else echo "what OS is this?"
	fi
else
	echo "cheatnt wasn't initialized"
fi
