ly/Makefile: Sort variables
[sunny256-utils.git] / mkrepo
blob9716206d759c2fe41970f0684ba25a9407b3ac31
1 #!/usr/bin/env perl
3 #=======================================================================
4 # mkrepo
5 # File ID: b7ee76c4-10c2-11e0-8682-00023faf1383
7 # [Description]
9 # Character set: UTF-8
10 # ©opyleft 2010– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'create-remote' => 0,
24 'directory' => "",
25 'exthd-remote' => 0,
26 'gitlab-remote' => 0,
27 'help' => 0,
28 'quiet' => 0,
29 'rsyncnet-remote' => 0,
30 'verbose' => 0,
31 'version' => 0,
35 our $progname = $0;
36 $progname =~ s/^.*\/(.*?)$/$1/;
37 our $VERSION = "0.2.0";
39 Getopt::Long::Configure('bundling');
40 GetOptions(
42 'create-remote|c' => \$Opt{'create-remote'},
43 'debug' => \$Opt{'debug'},
44 'directory|d=s' => \$Opt{'directory'},
45 'exthd-remote|e' => \$Opt{'exthd-remote'},
46 'gitlab-remote|g' => \$Opt{'gitlab-remote'},
47 'help|h' => \$Opt{'help'},
48 'quiet|q+' => \$Opt{'quiet'},
49 'rsyncnet-remote|r' => \$Opt{'rsyncnet-remote'},
50 'verbose|v+' => \$Opt{'verbose'},
51 'version' => \$Opt{'version'},
53 ) || die("$progname: Option error. Use -h for help.\n");
55 $Opt{'verbose'} -= $Opt{'quiet'};
56 $Opt{'help'} && usage(0);
57 if ($Opt{'version'}) {
58 print_version();
59 exit(0);
62 exit(main());
64 sub main {
65 # {{{
66 my $Retval = 0;
68 $Opt{'directory'} =~ /[^a-z0-9\-_\.\/]/ &&
69 die("$progname: $Opt{'directory'}: Invalid characters in --directory argument\n");
71 my $url = 'sunny@sunbase.org';
72 my $rsyncnet_url = $ENV{RN};
73 my $dir = $Opt{'directory'} ? "/home/sunny/repos/Git-$Opt{'directory'}" : "/home/sunny/repos/Git";
74 my $exthd_dir = '/media/exthd/alt/repos/Git';
76 defined($ARGV[0]) || die("$progname: No repo name specified, see --help\n");
77 my $repo = $ARGV[0];
78 $repo =~ s/(.*)\.git$/$1/i;
79 my $rg = "$dir/$repo.git";
80 $repo =~ /[^a-z0-9\-_\.\/]/ && die("$progname: $repo: Name contains invalid characters\n");
81 chomp(my $hname = `hostname`); # FIXME: Should probably use $HNAME
82 if ($Opt{'create-remote'}) {
83 mysystem("ssh", $url, "mkdir $rg && git init --bare $rg");
84 mysystem("git", "remote", "add", "sunbase", "$url:$rg");
85 if (-d "$dir/.") {
86 msg(1, "'$dir' exists, check for local repo...");
87 if (!-d "$rg/.") {
88 msg(1, "'$rg' doesn't exist, create local repo");
89 mysystem("git", "init", "--bare", $rg);
90 } else {
91 msg(1, "$rg: Repo already exists, that's good");
93 mysystem("git", "remote", "add", $hname, "sunny\@localhost:$rg");
94 chomp(my $origdir = `pwd`);
95 if (chdir($rg)) {
96 mysystem("git", "remote", "add", "sunbase", "$url:$rg");
97 chdir($origdir) ||
98 warn("$progname: $origdir: Cannot chdir to original directory: $!\n");
99 } else {
100 warn("$progname: $rg: chdir error: $!\n");
102 } else {
103 msg(1, "'$dir' doesn't exist, skipping check for local repo");
106 if ($Opt{'gitlab-remote'}) {
107 mysystem("git", "remote", "add", "gitlab", "git\@gitlab.com:sunny256/$repo.git");
109 if ($Opt{'exthd-remote'}) {
110 -d "$exthd_dir/$repo.git/." || mysystem("git", "init", "--bare", "$exthd_dir/$repo.git");
111 mysystem("git", "remote", "add", "exthd", "sunny\@localhost:$exthd_dir/$repo.git");
113 if ($Opt{'rsyncnet-remote'}) {
114 my $repodir = sprintf("repos/Git%s/$repo.git",
115 length($Opt{'directory'}) ? "-$Opt{'directory'}" : "",
117 mysystem("git", "remote", "add", "rsync-net", "$rsyncnet_url:$repodir");
118 mysystem("ssh", $rsyncnet_url, "git", "init", "--bare", "$repodir");
121 return $Retval;
122 # }}}
123 } # main()
125 sub mysystem {
126 # {{{
127 my @cmd = @_;
128 msg(0, "Executing '" . join(" ", @cmd) . "'");
129 system(@cmd);
130 return;
131 # }}}
132 } # mysystem()
134 sub print_version {
135 # Print program version {{{
136 print("$progname $VERSION\n");
137 return;
138 # }}}
139 } # print_version()
141 sub usage {
142 # Send the help message to stdout {{{
143 my $Retval = shift;
145 if ($Opt{'verbose'}) {
146 print("\n");
147 print_version();
149 print(<<"END");
151 Usage: $progname [options] repo_name
153 Options:
155 -c, --create-remote
156 Set up the sunbase remote
157 -d X, --directory X
158 Create a repo inside Git-X/ instead of Git/
159 -e, --exthd-remote
160 Create and set up the exthd remote
161 -g, --gitlab-remote
162 Define the gitlab remote
163 -h, --help
164 Show this help.
165 -q, --quiet
166 Be more quiet. Can be repeated to increase silence.
167 -r, --rsyncnet-remote
168 Create the rsync-net remote and initialize it.
169 -v, --verbose
170 Increase level of verbosity. Can be repeated.
171 --version
172 Print version information.
175 exit($Retval);
176 # }}}
177 } # usage()
179 sub msg {
180 # Print a status message to stderr based on verbosity level {{{
181 my ($verbose_level, $Txt) = @_;
183 if ($Opt{'verbose'} >= $verbose_level) {
184 print(STDERR "$progname: $Txt\n");
186 return;
187 # }}}
188 } # msg()
190 __END__
192 # This program is free software; you can redistribute it and/or modify
193 # it under the terms of the GNU General Public License as published by
194 # the Free Software Foundation; either version 2 of the License, or (at
195 # your option) any later version.
197 # This program is distributed in the hope that it will be useful, but
198 # WITHOUT ANY WARRANTY; without even the implied warranty of
199 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
200 # See the GNU General Public License for more details.
202 # You should have received a copy of the GNU General Public License
203 # along with this program.
204 # If not, see L<http://www.gnu.org/licenses/>.
206 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :