#!/usr/bin/env bash
cd ~

gitcred="$HOME/.config/git/credentials"
if [ ! -f "$gitcred" ]; then
	echo "Missing $gitcred"
	exit 1
fi
if ! grep -q 'gitlab.com' $gitcred; then
	echo "Missing gitlab.com in $gitcred"
	exit 1
fi

git clone -n --config=credential.helper=store https://ejrich@gitlab.com/ejri/ejrconfig.git ~/.ejrconfig

if [ ! -d ".ejrconfig" ]; then
	echo "Failed to create .ejrconfig"
	exit 1
fi

cd .ejrconfig

cat <<EOT > .git/hooks/post-checkout
#!/usr/bin/env bash
find . -path ./.git -prune -o -type f -exec chmod a-w {} +
EOT
cat <<EOT > .git/hooks/post-merge
#!/usr/bin/env bash
find . -path ./.git -prune -o -type f -exec chmod a-w {} +
EOT

chmod +x .git/hooks/post-checkout
chmod +x .git/hooks/post-merge
git checkout
