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

# If directory kernel doesn't exist, initialize the git repo
if [ ! -d $KERNEL_GIT_DIR ]; then
  mkdir $KERNEL_GIT_DIR
  cd $KERNEL_GIT_DIR
  git init
fi

# Go to the root directory
cd $ROOT_DIR

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

# Iterate over all directories in the releases directory
for release_dir in $(ls $RELEASES_DIR); do
  release_dir="$RELEASES_DIR/$release_dir"

  echo "got release_dir: $release_dir"
  cd $release_dir

  # TODO: clean this mess up

  get_platform_dir $release_dir

  echo "got platform_dir: $platform_dir"
  cd $platform_dir

  # In the platform directory, there's either a directory android-2.6.32 or a directory kernel which has a subdirectory android-2.6.32, get the path to the android-2.6.32 directory
  android_dir=$(ls | grep android-2.6.32)

  # If there's no such directory, try kernel/android-2.6.32
  if [ -z "$android_dir" ]; then
    android_dir=$(ls | grep kernel)

    if [ -z "$android_dir" ]; then
      echo "No android-2.6.32 directory found. Exiting..."
      exit 1
    fi
    
    android_dir="$android_dir/$(ls | grep android-2.6.32)"
  fi

  android_dir="$platform_dir/$android_dir"
  echo "got android_dir: $android_dir"

  sudo rm -rf $KERNEL_GIT_DIR/*
  cd $KERNEL_GIT_DIR
  
  echo "copying everything from $android_dir to $KERNEL_GIT_DIR ..."
  sudo cp -r $android_dir/* $KERNEL_GIT_DIR

  git add .
  git commit -m "kernel: update version $release_dir"

  cd $ROOT_DIR
done