source ./scripts/include/vars.sh
source ./scripts/include/sudo.sh
source ./scripts/include/utils.sh

cd $ROOT_DIR

if [ ! -d $RELEASES_DIR ]; then
  echo "No releases directory found. Exiting..."
  exit 1
fi

# If directory u-boot doesn't exist, clone base git repo
if [ ! -d $UBOOT_GIT_DIR ]; then
  # mkdir $UBOOT_GIT_DIR
  # cd $UBOOT_GIT_DIR
  # git init
  git clone git@github.com:u-boot/u-boot.git $UBOOT_GIT_DIR
  cd $UBOOT_GIT_DIR
  git checkout tags/v1.3.4
  git checkout -B tomtom
fi

for release_dir in $(ls $RELEASES_DIR); do
  release_dir="$RELEASES_DIR/$release_dir"
  echo "got release_dir: $release_dir"

  cd $release_dir

  get_platform_dir $release_dir
  echo "got platform_dir: $platform_dir"

  get_uboot_dir $platform_dir
  echo "got uboot_dir: $uboot_dir"

  # later releases don't include the entire uboot so this breaks 
  # sudo rm -rf $UBOOT_GIT_DIR/*
  cd $UBOOT_GIT_DIR

  echo "copying everything from $uboot_dir to $UBOOT_GIT_DIR ..."
  sudo cp -r $uboot_dir/* $UBOOT_GIT_DIR

  git add .
  git commit -m "u-boot: update version $release_dir"
  
  cd $ROOT_DIR
done
