#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only

#  (kgit-init), (initialize a meta-based kernel git tree)

#  Copyright (c) 2008-2011 Wind River Systems, Inc.

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.

#  This program 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 General Public License for more details.

#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# For consistent behaviour with "grep -w"
LC_ALL=C
export LC_ALL

path=`dirname $0`
. $path/kgit

usage()
{
cat <<EOF

 kgit-init [-v] [-c] <src> [<base_branch>] <tgt_repo>
 
  Initialize an existing or new repository

   -c: clean the tgt_repo after the clone completes. This will
       perform garbage collection and remove all existing 
       branches (under the assumption they'll be recreated)

   <src>        : The source repository.
   <base branch>: optional. indicates a base branch point in the
                  src repository to use as the base for the
                  dest repository
   <tgt_rep>    : repository to initialize. Can exist or not
                  exist before the command is run.

   -h: help
   -v: verbose

EOF
}

# command line processing
while [ $# -gt 0 ]; do
    case "$1" in
	-o|--o)
		dest="$2"
		shift
		;;
	-v|--v)
		verbose=t
		;;
	-c|--c)
		clean=t
		;;
	-h|--h) 
	        usage
                exit;
                ;;
	*)
	        break
		;;
    esac
    shift
done

if [ ! $# -gt 0 ]; then
    usage
    exit
fi

source kgit

# assign the default set of arguments
#   source tree, branch point and destination
src=$1
initial_branch_point=$2
dest=$3

content_branch=master

# now fixup based on zero'd values
if [ -z "$initial_branch_point" ]; then
    # no source, no branch, just a dest. We'll be
    # reanimating this repo
    dest=$src
    src=
else
    if [ -z "$dest" ]; then
	# no branch was passed, this is just <src> <dest>
	dest=$initial_branch_point
	initial_branch_point=
    fi
fi

if [ -n "$dest" ] && [ -z "$src" ]; then
    if [ ! -d "$dest" ]; then
	echo "ERROR. If no src is passed, $dest must exist"
	exit 1
    fi
fi  

if [ -z "$src" ] && [ ! -d "$src" ]; then
    echo "ERROR. $src is not a valid src repository"
    exit 1
fi

if [ -z "$message" ]; then
    message="base: add .gitignore and exclusions"
fi

# figure out what type of type destination repository we are creating
new_repo=
existing_repo=
if [ -d "$dest/.git" ]; then
    existing_repo=t
else
    # this means we are starting a repository from scratch
    new_repo=t
fi

if [ -n "$new_repo" ]; then
    if [ -n "$verbose" ]; then
	echo "[INFO] git clone --shared $src $dest"
    else
	clone_opt=-q
    fi

    mkdir -p $dest

    clone_opt="$clone_opt --shared"

    # this means that we don't have to worry about converting
    # remote branches into local tracking ones
    git clone --bare $clone_opt $src $dest/.git
    r=$?
    if [ $r -ne 0 ]; then
	echo [ERROR] git clone of \"$src\" failed
	exit $r
    fi

    # check to see if the src is a bare repository and if
    # it is, do an optimized startup
    if [ -d $src/.git ]; then
	cp -a $src/.git/config $dest/.git
	cd $dest
    else
	cd $dest
	git config core.bare false
	# layering on 1k+ of patches makes auto gc inevitable/annoying
	git config gc.auto 0
	git checkout -q -f $checkout_opts master
	if [ $? != 0 ]; then
	    echo "[ERROR] git checkout -q -f master failed"
	    exit -1
	fi
    fi

    if [ -n "$clean" ]; then
	clean_tgt_repo
    fi

    git checkout -q -f $checkout_opts master
    if [ $? != 0 ]; then
	echo "[ERROR] git checkout -qf master failed"
	exit -1
    fi
    git reset --hard $initial_branch_point &> /dev/null
    if [ $? -ne 0 ]; then
	echo "[ERROR] branch point $initial_branch_point is not valid"
	exit 1
    fi

    meta_dir=$(kgit --meta)

    if [ ! -d "$meta_dir/cfg/scratch/obj" ]; then
	mkdir -p $meta_dir/cfg/scratch/obj
    fi
    if [ ! -d "$meta_dir/patches" ]; then
	mkdir -p $meta_dir/patches
    fi

    git checkout -q $content_branch
    if [ $? != 0 ]; then
	echo "[ERROR] git checkout -q content_branch failed"
	exit -1
    fi
fi
