#!/bin/sh

if ! [ -d .git ]; then
	echo .git dir not found. exiting....
	exit 1
fi
if [ -f .gear-rules ]; then
	mkdir .gear
	git mv -f .gear-rules .gear/rules
fi
if ! [ -d .gear ]; then
	echo .gear dir not found. exiting....
	exit 1
fi

url=$1
# always use git:// for github
url=${url/http:\/\/github.com\//git://github.com/}
url=${url/https:\/\/github.com\//git://github.com/}
url=${url/ftp:\/\/github.com\//git://github.com/}
url=${url/rsync:\/\/github.com\//git://github.com/}
# always use git:// for gitlab
url=${url/http:\/\/gitlab.com\//git://gitlab.com/}
url=${url/https:\/\/gitlab.com\//git://gitlab.com/}
url=${url/ftp:\/\/gitlab.com\//git://gitlab.com/}
url=${url/rsync:\/\/gitlab.com\//git://gitlab.com/}

if [ -f .gear/upstream/remotes ]; then
	echo ".gear/upstream/remotes already exist; first run"
	echo "rm .gear/upstream/remotes"
	exit 1
fi

mkdir -p .gear/upstream/
cat > .gear/upstream/remotes << EOF
[remote "upstream"]
        fetch = +refs/heads/*:refs/remotes/upstream/*
        url = $url
EOF
git add .gear/upstream/remotes
echo now run gear-remotes-restore

: << __END__

=head1	NAME

gear-remotes-set-from-url - create .gear/upstream/remotes file from the given url

=head1	SYNOPSIS

B<gear-remotes-set-from-url>
I<URL>

=head1	DESCRIPTION

B<gear-remotes-set-from-url> creates I<.gear/upstream/remotes> file from the given remote git repository url.
After I<.gear/upstream/remotes> is created, run B<gear-remotes-restore>.

=head1	ARGUMENTS

I<URL> -- remote git repository url.

=head1	OPTIONS

none.

=head1	AUTHOR

Written by Igor Vlasenko <viy@altlinux.org>.

=head1	COPYING

Copyright (c) 2014-2020 Igor Vlasenko, ALT Linux Team.

This is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

=cut

__END__
