#!/bin/bash
set -euxo pipefail

git submodule update --remote

omit='grep -v "sindresorhus/awesome\|bradoyler/awesome-all\|emijrp/awesome-awesome\|erichs/awesome-awesome\|oyvindrobertsen/awesome-awesome\|fleveque/awesome-awesomes\|bayandin/awesome-awesomeness\|jnv/lists\|coopermaa/awesome-awesome\|awesome-android-ui"'
latest=$(find ./awesome-awesomeness -iname "*README*" -exec bash -c 'cat {} | grep -Eo "(http|https)://github.com/[a-zA-Z0-9./?=_%:-]*"	| grep awesome ' \; | $omit | sort -u)

added=$(git config --file .gitmodules --get-regexp '\.url$' | grep github | awk '{print $2}' | $omit | sort -u)

to_add=$(comm -23 <(echo "$added") <(echo "$latest"))

for url in $to_add
do
  name=$(echo $url | awk -F '/' '{print $5}')
	git submodule add $url lists/$name
done
