:%s/^ /\t/
[hostuuid.git] / hostuuid
blobeb4c359c2d43b5c245f2062063ba3bf4ed40558e
1 #!/usr/bin/perl
3 #This program is free software: you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation, either version 3 of the License, or
6 #(at your option) any later version.
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 #GNU General Public License for more details.
13 #You should have received a copy of the GNU General Public License
14 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Written by Richard Hartmann in 2009, feel free to email
17 # richih dot mailinglist -at- gmail dot com with questions.
19 =head1 NAME
21 hostuuid - program to create, display, check /etc/hostuuid
23 =cut
25 use strict;
26 use warnings;
28 use UUID::Tiny;
29 use Getopt::Long qw(:config no_ignore_case bundling);
30 use File::Basename;
32 my $VERSION;
33 $VERSION = '1.2';
34 my $program = basename($0);
36 my $file = '/etc/hostuuid';
37 my $force;
38 my $print_version;
39 my $print_help;
40 my $input;
41 my $create_uuid = 0;
42 my $quiet;
43 my $no_stdin=0;
45 =head1 SYNOPSIS
47 B<hostuuid> [-f I<file>]
49 B<hostuuid> -c [--force] [-f I<file>]
51 echo 4f1cc27c-6c7b-4419-9db6-5b2f6a600b37 | B<hostuuid>
53 =head1 DESCRIPTION
55 B<hostuuid> creates, displays and verifies the contents of /etc/hostuuid or any other file.
56 It will also check anything on STDIN for being a UUID.
58 =head1 OPTIONS
60 =over 4
62 =item B<-f|--file> I<filename>
64 The I<filename> B<hostuuid> will read from/write to.
66 =item B<--force>
68 Force creation of new UUID.
70 =item B<-c|--create>
72 Create a new UUID.
74 =item B<-q|--quiet>
76 Don't print anything.
77 B<hostuuid> will still exit with proper return codes for you to script with.
79 =item B<--input>
81 Save this UUID to I<filename>.
82 Defaults to /etc/hostuuid
84 =item B<--nostdin>
86 Do not listen on STDIN.
87 Needed to work around a bug in debconf.
89 =item B<-h|-?|--help>
91 Print help.
93 =item B<--version>
95 Print version information.
97 =back
99 =head1 FILES
101 /etc/hostuuid
103 =head1 REQUIRES
105 Perl 5.8, UUID::Tiny
107 =head1 SEE ALSO
109 perl(1)
111 =head1 AUTHOR
113 Richard Hartmann <richih.mailinglist@gmail.com>
115 =cut
117 GetOptions (
118 'file|f=s' => \$file,
119 'force' => \$force,
120 'create|c' => \$create_uuid,
121 'input=s' => \$input,
122 'quiet|q' => \$quiet,
123 'nostdin' => \$no_stdin,
124 'help|h|?' => \$print_help,
125 'version' => \$print_version,
126 ) or print_help(255);
128 sub print_version() {
129 unless ($quiet) {
130 print <<VERSION;
131 $program: version $VERSION
133 This program is released under GPLv3
134 Written by Richard Hartmann for Debian and Grml
135 VERSION
137 exit 0;
140 sub print_help {
141 my ($exit_code) = @_;
142 unless ($quiet) {
143 print <<HELP;
144 Help for $program:
146 -f, --file : file to read from/write to. Defaults to /etc/hostuuid
147 --force : force creation of new UUID
148 -c, --create : create & save UUID
149 -q, --quiet : Don't print anything
150 --input : Save UUID to file. Defaults to /etc/hostuuid
151 --nostdin : Do not listen on STDIN
152 -h, -?, --help : print this help
153 --version : print version
154 HELP
156 exit $exit_code;
159 sub create_uuid() {
160 my $uuid = find_hostuuid();
161 if ($uuid && !$force) {
162 print STDERR "$program: Found UUID '$uuid' in '$file'. Aborting!\n" unless $quiet;
163 exit 101;
164 } else {
165 save_uuid(create_UUID_as_string(UUID_V4));
169 sub find_hostuuid() {
170 return 0 unless ((-e $file && $create_uuid) || !$create_uuid);
171 open (F, "<", "$file") or do {
172 die "$program: Could not open file: '$file'\: $!\n" unless $quiet;
173 exit 2;
175 my $uuid = check_uuid(<F>);
176 close (F);
177 return $uuid;
180 sub check_uuid {
181 my ($string) = @_;
182 return $1 if ($string =~ /^([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$/i);
183 return undef;
186 sub save_uuid {
187 my ($uuid) = @_;
188 unless (check_uuid($uuid)) {
189 print "$uuid is NOT a valid UUID\n" unless $quiet;
190 exit 3;
192 open (F, ">", "$file") or do {
193 die "$program: Could not open file: '$file'\: $!\n" unless $quiet;
194 exit 5;
196 print "$program\: Stored UUID '$uuid' in '$file'\n" unless $quiet;
197 print F "$uuid\n";
198 close (F);
199 exit 0;
202 sub main() {
203 unless ($no_stdin || -t STDIN) { ## we need to test for $no_stdin cause debconf makes us hang, otherwise
204 my $uuid = check_uuid(<STDIN>);
205 if ($uuid) {
206 print "$uuid is a valid UUID\n" unless $quiet;
207 exit 0;
208 } else {
209 print "NOT a valid UUID\n" unless $quiet;
210 exit 105;
213 print_version() if $print_version;
214 print_help(0) if $print_help;
215 save_uuid($input) if $input;
216 create_uuid() if $create_uuid;
218 my $uuid = find_hostuuid();
219 if ($uuid) {
220 print "$uuid\n" unless $quiet;
221 exit 0;
222 } else {
223 print STDERR "$program: Could not find valid UUID in '$file'\n" unless $quiet;
224 exit 100;
228 main();