#!/bin/bash

# pokrenuti sa: source provjeri-repozitorij.sh url-repozitorija
# ili najprije: chmod 755 provjeri-repozitorij.sh
# pa onda: ./provjeri-repozitorij.sh url-repozitorija

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

MORA_BITI_MASTER=0

if [ $# -lt 1 ]; then
	echo -e "${RED}Upotreba: ./provjeri-repozitorij.sh url-repozitorija${NC}"
	echo -e "${RED}    npr.: ./provjeri-repozitorij.sh git@github.com:student/os.git${NC}"
	exit 1
fi
URL=$1

if [ $# -gt 1 ]; then
	DIR=$2
else
	DIR=os_test   #ne postaviti ime koji inače koristite!
fi

if [ -d $DIR ]; then
	rm -rf $DIR
fi

dohvati_repozitorij()
{
	if [ ! -d $DIR ]; then
		git clone $URL $DIR &> /dev/null

		if [ $? -ne 0 ]; then
			echo -e "${RED}Neka greška:${NC}"
			rm -rf $DIR
			git clone $URL $DIR
			exit 1
		fi

		if [ $MORA_BITI_MASTER == "1" ]; then
			cd $DIR
			git checkout master &> /dev/null
			if [  $? -ne 0 ] ; then
				echo -e "${RED}NEMA GRANE master! Preimenujte početnu granu u master!${NC}"
				exit 1
			fi
			cd ..
		fi
	fi
}

pogledaj_lab0()
{
	dohvati_repozitorij

	cd $DIR

	SVE=(.gitignore lab1/readme.txt lab2/readme.txt lab3/readme.txt student.txt)

	cnt=0
	for dat in "${SVE[@]}" ; do
		if [ -e $dat ] ; then
			let cnt=cnt+1
		else
			echo -e "${RED}Nema datoteke: $dat${NC}"
		fi
	done
	if [ ! "$cnt" == "${#SVE[@]}" ]; then
		echo -e "${RED}Nije sve napravljeno! Ukupno ima datoteka: $cnt/${#SVE[@]}${NC}"
		exit 1
	else
		echo -e "${GREEN}Sve datoteke na broju!${NC}"
	fi

	x=($(head -1 student.txt))
	echo -e "${YELLOW}Provjerite ispravnost datoteke student.txt:${NC}"
	echo -e "${YELLOW}JMBAG: ${x[0]}${NC}"
	echo -e "${YELLOW}ime: ${x[1]}${NC}"
	echo -e "${YELLOW}prezime: ${x[2]}${NC}"
	echo -e "${YELLOW}e-mail: ${x[3]}${NC}"
	#echo -e "${YELLOW}grupa: ${x[4]}${NC}"

	cd ..
}

echo -e "${GREEN}Provjera repozitorija: $URL (lokalno $DIR)${NC}"

pogledaj_lab0

#makni lokalno dohvaćeni repozitorij
rm -rf $DIR