#!/bin/bash
# Runs indexing from scratch on all supported projects
# Needed when full re-indexing is needed:
# - to fix an indexing bug
# - after database changes

#  This file is part of Elixir, a source code cross-referencer.
#
#  Copyright (C) 2019--2020 Michael Opdenacker and contributors
#
#  Elixir is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Elixir is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Elixir.  If not, see <http://www.gnu.org/licenses/>.

index() {
    project=$1
    master=$2
    remote=$3
    remote2=$4
    echo "$project: first indexing pass..."

    export LXR_DATA_DIR=$ELIXIR_ROOT/$project/data
    export LXR_REPO_DIR=$ELIXIR_ROOT/$project/repo

    mkdir -p $LXR_DATA_DIR
    cd `dirname $LXR_REPO_DIR`
    git clone --bare $master `filename $LXR_REPO_DIR`

    if [ "$remote" != "" ]
    then
        cd $LXR_REPO_DIR
        git remote add other $remote
        git fetch --tags other
    fi

    if [ "$remote2" != "" ]
    then
        cd $LXR_REPO_DIR
        git remote add other2 $remote2
        git fetch --tags other2
    fi

    cd $ELIXIR_INSTALL
    ./update.py $ELIXIR_THREADS

    # The above can take so much time on the first run that it's worth running a new update

    echo "$project: updating the repository after the first indexing pass..."
    cd $LXR_REPO_DIR
    git fetch --all --tags

    echo "$project: 2nd indexing pass..."
    cd $ELIXIR_INSTALL
    ./update.py $ELIXIR_THREADS
}

if [ "$ELIXIR_ROOT" = "" ]
then
    echo "Error: ELIXIR_ROOT environment variable not set"
    echo "It's where Elixir data are stored"
    exit 1
fi

if [ "$ELIXIR_INSTALL" = "" ]
then
    echo "Error: ELIXIR_INSTALL environment variable not set"
    echo "It's where Elixir code is stored"
    exit 1
fi

index amazon-freertos https://github.com/aws/amazon-freertos.git &
index arm-trusted-firmware https://github.com/ARM-software/arm-trusted-firmware &
index barebox https://git.pengutronix.de/git/barebox &
index busybox git://git.busybox.net/busybox &
index coreboot https://review.coreboot.org/coreboot.git &
index dpdk git://dpdk.org/dpdk &
index glibc git://sourceware.org/git/glibc.git &
index linux git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git https://github.com/bootlin/linux-history.git &
index llvm https://github.com/llvm/llvm-project.git &
index mesa https://gitlab.freedesktop.org/mesa/mesa.git &
index musl git://git.musl-libc.org/musl &
index ofono https://git.kernel.org/pub/scm/network/ofono/ofono.git &
index qemu https://git.qemu.org/git/qemu.git &
index u-boot https://gitlab.denx.de/u-boot/u-boot.git &
index uclibc-ng git://uclibc-ng.org/git/uclibc-ng &
index zephyr https://github.com/zephyrproject-rtos/zephyr &
index toybox https://github.com/landley/toybox.git &
index grub https://git.savannah.gnu.org/git/grub.git &
