# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/git.eclass,v 1.15 2009/02/19 17:07:28 scarabeus Exp $

## --------------------------------------------------------------------------- #
# subversion.eclass author: Akinori Hattori <hattya@gentoo.org>
# modified for git by Donnie Berkholz <spyderous@gentoo.org>
# improved by Fernando J. Pereda <ferdy@gentoo.org>
# you should currently poke with updates Tomas Chvatal <scarabeus@gentoo.org>
#
# The git eclass is written to fetch the software sources from
# git repositories like the subversion eclass.
#
#
# Description:
#   It is necessary to define the EGIT_REPO_URI variable at least.
#
#  15-03-2009 - I. Besoiu <givemesugarr@gmail.com>
#  The eclass has been modified into a single bash file for git fetch/clone of 
#  the git libdrm and xf86-video-radeonhd repositories for other linux environments with bash.
#  Adjusting acordingly the variables in the script can support a wider range
#  of git repositories and packages. 
#  For a better understanding on eclasses see the www.gentoo.org sites.
#  This script comes without any warranty. If it breaks stuff on your system i'm not to be 
#  held responsible.
#  The full text of the GNU General Public License v2 can be found here:
#  http://www.gnu.org/licenses/gpl-2.0.html
#
## --------------------------------------------------------------------------- #

#!/bin/bash

##
#
EGIT="git"

## -- EGIT_STORE_DIR:  git sources store directory
#
EGIT_STORE_DIR="/usr/src/git-src"

## -- EGIT_FETCH_CMD:  git clone command
#
EGIT_FETCH_CMD="git clone --bare --depth 1"
# --bare --depth 1"

## -- EGIT_UPDATE_CMD:  git fetch command
# -f -u
EGIT_UPDATE_CMD="git fetch -f -u"

## -- EGIT_DIFFSTAT_CMD: Command to get diffstat output
#
EGIT_DIFFSTAT_CMD="git diff --stat"

## -- EGIT_CHECKOUT_CMD: Command to switch branches
#
EGIT_CHECKOUT_CMD="git checkout -b"

# Set this variable to a non-empty value to disable the automatic updating of
# an GIT source tree. This is intended to be set outside the git source
# tree by users.
EGIT_OFFLINE=""


## -- EGIT_OPTIONS:
#
# the options passed to clone and fetch
#
EGIT_OPTIONS=""

## - EGIT_REPACK:
#
# git eclass will repack objects to save disk space. However this can take a
# long time with VERY big repositories. If this is your case set:
# EGIT_REPACK=false
#
EGIT_REPACK="false"

## - EGIT_PRUNE:
#
# git eclass can prune the local clone. This is useful if upstream rewinds and
# rebases branches too often. If you don't want this to happen, set:
# EGIT_PRUNE=false
#
EGIT_PRUNE="false"

## -- EGIT_TREE:
#
# git eclass can checkout any tree.
# Defaults to EGIT_BRANCH.
#
EGIT_TREE=${EGIT_BRANCH}

## -- EGIT_REPO_URI:  repository uri
#
# e.g. http://foo, git://bar
#
# supported protocols:
#   http://
#   https://
#   git://
#   git+ssh://
#   rsync://
#   ssh://
#
# cloning 3 repositories 
EGIT_REPO_URI_1="git://anongit.freedesktop.org/git/mesa/drm"
EGIT_REPO_URI_2="git://anongit.freedesktop.org/git/xorg/driver/xf86-video-radeonhd"
EGIT_REPO_URI_3="git://anongit.freedesktop.org/mesa/mesa"
EGIT_REPO_URI_4="git://anongit.freedesktop.org/git/xorg/proto/dri2proto"


EGIT_BRANCH_1="r6xx-r7xx-support"
EGIT_BRANCH_2="r6xx-r7xx-support"
EGIT_BRANCH_3="master"

## --Taking the working and actual dirs so that at the end of the script the user gets to the same
#    directory from which the script has been launched
WORKING_DIR=${1}
ACTUAL_DIR=$(pwd)

## -- die () function
## it exits with error when some errors occur during the script execution
## ---------------------------------------------#

die() {
	echo ${1}
	exit;
}

## -- git_fetch() function
## it clones/updates the git repository
## ------------------------------------------------- #

git_fetch() {

	local EGIT_CLONE_DIR
	
	EGIT_TREE=${EGIT_BRANCH}
	# EGIT_REPO_URI is empty.
	[[ -z ${EGIT_REPO_URI} ]] && die "${EGIT}: EGIT_REPO_URI is empty."

	# check for the protocol or pull from a local repo.
	if [[ -z ${EGIT_REPO_URI%%:*} ]] ; then
		case ${EGIT_REPO_URI%%:*} in
			git*|http|https|rsync|ssh)
				;;
			*)
				die "${EGIT}: fetch from "${EGIT_REPO_URI%:*}" is not yet implemented."
				;;
		esac
	fi

	if [[ ! -d ${EGIT_STORE_DIR} ]] ; then
		echo "${FUNCNAME}: initial clone. creating git directory"
		mkdir -p "${EGIT_STORE_DIR}" \
			|| die "${EGIT}: can't mkdir ${EGIT_STORE_DIR}."
	fi

	cd -P "${EGIT_STORE_DIR}" || die "${EGIT}: can't chdir to ${EGIT_STORE_DIR}"
	EGIT_STORE_DIR=${PWD}

	[[ -z ${EGIT_REPO_URI##*/} ]] && EGIT_REPO_URI="${EGIT_REPO_URI%/}"
	EGIT_CLONE_DIR="${EGIT_PROJECT}"
	#echo ${EGIT_PROJECT}

	GIT_DIR="${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}"

	if [[ ! -d ${EGIT_CLONE_DIR} ]] ; then
		# first clone
		echo "git clone start -->"
		echo "   repository: ${EGIT_REPO_URI}"
		
		${EGIT_FETCH_CMD} ${EGIT_OPTIONS} "${EGIT_REPO_URI}" ${GIT_DIR} \
			|| die "${EGIT}: can't fetch from ${EGIT_REPO_URI}."
		
		cd ${GIT_DIR}
		# We use --bare cloning, so git doesn't do this for us.
		git config remote.origin.url "${EGIT_REPO_URI}"

	elif [[ -n ${EGIT_OFFLINE} ]] ; then
		local oldsha1=$(git rev-parse ${EGIT_BRANCH})
		echo "git update offline mode -->"
		echo "   repository: ${EGIT_REPO_URI}"
		echo "   commit: ${oldsha1}"
	else
		# Git urls might change, so unconditionally set it here
		cd ${GIT_DIR}
		git config remote.origin.url "${EGIT_REPO_URI}"

		#cd ${GIT_DIR}
		# fetch updates
		echo "git update start -->"
		echo "   repository: ${EGIT_REPO_URI}"

		local oldsha1=$(git rev-parse ${EGIT_BRANCH})
		
		${EGIT_UPDATE_CMD} ${EGIT_OPTIONS} origin ${EGIT_BRANCH}:${EGIT_BRANCH} \
			|| die "${EGIT}: can't update from ${EGIT_REPO_URI}."

		# piping through cat is needed to avoid a stupid Git feature
		${EGIT_DIFFSTAT_CMD} ${oldsha1}..${EGIT_BRANCH} | cat
	fi

	echo "   local clone: ${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}"

	if ${EGIT_REPACK} || ${EGIT_PRUNE} ; then
		ebegin "Garbage collecting the repository"
		git gc $(${EGIT_PRUNE} && echo '--prune')
		eend $?
	fi

	#cd /
	# export to the ${WORKDIR}
	mkdir -p "${WORKDIR}"
	#cd /
	echo ${EGIT_TREE}
	git archive --format=tar ${EGIT_TREE} | ( cd "${WORKDIR}" ; tar xf - )

	echo ">>> Unpacked to ${WORKDIR}"

}

## --script usage in case of typing --usage --help or not typing any option for the script
#
if [[ ${1} == '--usage' ]] || [[ -z ${1} ]] || [[ ${1} == '--help' ]]; then
  echo 'radeon-update.sh --usage' 
  echo 'To use the script you must specify the working directory in which to put the git repository and the src package.'
  exit
else  

## -- first git repository (mesa)
# 
  EGIT_REPO_URI=${EGIT_REPO_URI_3}
  EGIT_BRANCH=${EGIT_BRANCH_3}
  PN="mesa"
  EGIT_PROJECT=${PN/-git}
  WORKDIR="${WORKING_DIR}/mesa"

## -- cleaning old src directory, cloning/updating the drm repository
# 
  echo '***********************************************'
  echo '************ updating mesa ********************'
  echo '***********************************************'
  rm ${WORKDIR} -fR
  git_fetch || die "git_fetch() failed"
  echo 
  echo `pwd` 

## -- second git repository (libdrm)
# 
  EGIT_REPO_URI=${EGIT_REPO_URI_1}
  EGIT_BRANCH=${EGIT_BRANCH_1}
  PN="drm"
  EGIT_PROJECT=${PN/-git}
  WORKDIR="${WORKING_DIR}/drm"

## -- cleaning old src directory, cloning/updating the drm repository
# 
  echo '***********************************************'
  echo '************ updating libdrm ******************'
  echo '***********************************************'
  rm ${WORKDIR} -fR
  git_fetch || die "git_fetch() failed"
  echo 
  echo `pwd` 

## -- third repository xf86-video-radeonhd
#
  EGIT_REPO_URI=${EGIT_REPO_URI_2}
  EGIT_BRANCH=${EGIT_BRANCH_2}
  PN="xf86-video-radeonhd"
  EGIT_PROJECT=${PN/-git}
  WORKDIR="${WORKING_DIR}/xf86-video-radeonhd"

## -- cleaning old src directory, cloning/updating the drm repository
# 
  echo '***********************************************'
  echo '******** updating xf86-video-radeonhd *********'
  echo '***********************************************'
  rm ${WORKDIR} -fR
  git_fetch || die "git_fetch() failed"
  echo 
  echo `pwd` 


## --fourth repo dri2proto
#
  EGIT_REPO_URI=${EGIT_REPO_URI_4}
  EGIT_BRANCH=${EGIT_BRANCH_3}
  PN="dri2proto"
  EGIT_PROJECT=${PN/-git}
  WORKDIR="${WORKING_DIR}/dri2proto"

## -- cleaning old src directory, cloning/updating the drm repository
# 
  echo '***********************************************'
  echo '************ updating dri2proto ***************'
  echo '***********************************************'
  rm ${WORKDIR} -fR
  git_fetch || die "git_fetch() failed"
  echo 
  echo `pwd` 

## -- building dri2proto
#
  echo '***********************************************'
  echo '************ building dri2proto ***************'
  echo '***********************************************'
  cd ${WORKING_DIR}/dri2proto
  
## -- we're using the workdir/build prefix so that the package won't install the 
#  files in the official directory and the script doesn't need the root priviledges
#  until we copy the files in the right directories
#
  ./autogen.sh --prefix="${WORKING_DIR}/dri2proto/build" || die "autogen on drm failed"
  make || die "error making dri2proto"
  make install || die "error installing dri2proto"
  #sudo cp build/* -av /

## -- building libdrm and kernel modules
#
  echo '***********************************************'
  echo '************ building libdrm ******************'
  echo '***********************************************'
  cd ${WORKING_DIR}/drm

## -- we're using the workdir/build prefix so that the package won't install the 
#  files in the official directory and the script doesn't need the root priviledges
#  until we copy the files in the right directories
#
  ./autogen.sh --prefix="${WORKING_DIR}/drm/build" || die "autogen on drm failed"
  make || die "error making drm"
  make install || die "error installing libdrm"
  #sudo cp build/* -av /
  echo '***********************************************'
  echo '************ building modules *****************'
  echo '***********************************************'
  cd linux-core
  make || die "error making kernel modules"
  
  echo '***********************************************'
  echo '************ installing modules ***************'
  echo '***********************************************'
  #sudo mv /lib/modules/`uname -r`/kernel/drivers/gpu/drm/drm.ko /lib/modules/`uname -r`/kernel/drivers/gpu/drm/drm.koback \
  #	|| die "error installing kernel modules"
  #sudo cp drm.ko /lib/modules/`uname -r`/kernel/drivers/gpu/drm/ || die "error installing kernel modules"
  #sudo mv /lib/modules/`uname -r`/kernel/drivers/gpu/drm/radeon/radeon.ko /lib/modules/`uname -r`/kernel/drivers/gpu/drm/radeon/radeon.koback \
  #	|| die "error installing kernel modules"
  #sudo cp radeon.ko /lib/modules/`uname -r`/kernel/drivers/gpu/drm/radeon/ || die "error installing kernel modules"

## -- cleaning old src directory, cloning/updating the xf86-video-radeonhd repository
# 
  echo '***********************************************'
  echo '************ updating radeonhd ****************'
  echo '***********************************************'
  rm ${WORKDIR} -fR
  git_fetch || die "git_fetch() failed"
  echo
  echo `pwd` 

## -- building mesa
#
  echo '***********************************************'
  echo '************ building mesa ********************'
  echo '***********************************************'
  cd ${WORKING_DIR}/mesa

## -- we're using the workdir/build prefix so that the package won't install the 
#  files in the official directory and the script doesn't need the root priviledges
#  until we copy the files in the right directories
#
  ./autogen.sh --prefix="${WORKING_DIR}/mesa/build" || die "autogen on drm failed"
  # ./configure --enable-xcb
  make || die "error making mesa"
  make install || die "error installing mesa"
  #sudo cp build/* -av /


## -- building xf86-video-radeonhd
#
  echo '***********************************************'
  echo '************ building radeonhd ****************'
  echo '***********************************************'
  cd ${WORKING_DIR}/xf86-video-radeonhd

## -- we're using the workdir/build prefix so that the package won't install the 
#  files in the official directory and the script doesn't need the root priviledges
#  until we copy the files in the right directories
#
./autogen.sh --prefix="${WORKING_DIR}/xf86-video-radeonhd/build" || die "autogen on xf86-video-radeon failed"
  make || die "error making xf86-video-radeon driver"
  make install || die "error installing xf86-video-radeonhd"
  #sudo cp build/* / -av
  cd ${ACTUAL_DIR}

  #depmod -a `uname -r`
  #modprobe -r radeon
  #modprobe -r drm
  #modprobe drm
  #modprobe radeon
fi

