#!/bin/bash

# This file is part of L-SMASH project.
# http://github.com/silverfilain/x264_L-SMASH

# Copyright (C) 2010 L-SMASH project
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# Aug. 26, 2010 Takashi Hirata
#     Initial release for unofficial repository of L-SMASH.
# Sep. 20, 2010 Takashi Hirata
#     Change cloning of official repo to optional.
#     Add help.
#     Remove pushurl settings.

if [ x"$1" = x"-h" -o x"$1" = x"--help" ]; then
	echo
	echo 'Usage: ./x264_L-SMASH_clone.sh [options]'
	echo
	echo 'Options:'
	echo '    -h'
	echo '    --help              Show this help.'
	echo "    --enable-official   Integrate L-SMASH's official repo in output directory."
	exit 1
fi

#### semi-const settings ####

workdir=x264_L-SMASH
plain_repo=git://git.videolan.org/x264.git
kovy_repo=git://github.com/Kovensky/x264-audio.git
official=git://repo.or.cz/L-SMASH.git
unofficial=git://github.com/silverfilain/x264_L-SMASH.git
plain_remote=porigin
kovy_remote=korigin

collaborator=

use_official='no'

#### parsing options ####
for opt do
	optdata="${opt#*=}"
	case "$opt" in
		--enable-official)
			use_official='yes'
			;;
		*)
			echo "Unknown option : $opt"
			;;
	esac
done

#### utilities ####
fatal(){
	echo -n "ERROR: "
	echo $1
	exit 1
}

#### cloning ####

echo "INFO: ==== CLONING FROM $unofficial"
git clone $unofficial $workdir || fatal "failed to clone UNOFFICIAL repo."
cd $workdir

git remote add $plain_remote $plain_repo || fatal "failed to clone x264 repo."
git remote add $kovy_remote $kovy_repo || fatal "failed to clone x264-audio repo."
git fetch $plain_remote || fatal "failed to 'fetch $plain_remote'."
git fetch $kovy_remote || fatal "failed to 'fetch $kovy_remote'."
#git config remote.$plain_remote.pushurl $unofficial
#git config remote.$kovy_remote.pushurl $unofficial

git branch plain $plain_remote/master || fatal "failed to 'branch'."
git branch kaudio $kovy_remote/audio_new || fatal "failed to 'branch'."
#git branch laudio origin/laudio || fatal "failed to 'branch'."
#git branch lsmash origin/lsmash || fatal "failed to 'branch'."

#### setup "output" directory to be able to share with repo.or.cz
if [ x"$use_official" = x"yes" ]; then
	pushd output
	rm -f *
	echo "INFO: ==== CLONING FROM $official"
	git clone $official . || fatal "failed to clone repo.or.cz."
	popd
	git reset --hard
fi

echo "INFO: end."
