Added --quiet, --verify and <STDIN> handling
[hostuuid-debian.git] / hostuuid
blobf25d579ea60cda3fa86dfc614991f1bc73b3973a
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.
20 use strict;
21 use warnings;
23 use UUID::Tiny;
24 use Getopt::Long qw(:config no_ignore_case bundling);
25 use File::Basename;
27 my $version = '1.2';
28 my $program = basename($0);
30 my $file = '/etc/hostuuid';
31 my $print_version;
32 my $print_help;
33 my $create_uuid = 0;
34 my $quiet;
35 my $verify_uuid;
38 GetOptions (
39 'file|f=s' => \$file,
40 'help|h|?' => \$print_help,
41 'version|v' => \$print_version,
42 'create|c' => \$create_uuid,
43 'quiet|q' => \$quiet,
44 'verify' => \$verify_uuid,
45 ) or print_help(255);
47 sub print_version() {
48 unless ($quiet) {
49 print <<VERSION;
50 $program: version $version
52 This program is released under GPLv3
53 Written by Richard Hartmann for Debian and Grml
54 VERSION
56 exit 0;
59 sub print_help {
60 my ($exit_code) = @_;
61 unless ($quiet) {
62 print <<HELP;
63 Help for $program:
65 -h, -?, --help : print this help
66 -v, --version : print version
67 -f, --file : file to read from/write to. Defaults to /etc/hostuuid
68 -c, --create : create & save UUID
69 -q, --quiet : Don't print anything
70 --verify : verify if there is a valid UUID in file
71 Does not print, only exits with zero/non-zero return code
72 HELP
74 exit $exit_code;
77 sub create_uuid() {
78 my $uuid = find_hostuuid();
79 if ($uuid) {
80 print STDERR "$program: Found UUID '$uuid' in '$file'. Aborting!\n" unless $quiet;
81 exit 101;
82 } else {
83 open (F, ">", "$file") or do {
84 die "$program: Could not open file: '$file'\: $!\n" unless $quiet;
85 exit 2;
87 $uuid = create_UUID_as_string(UUID_V4);
88 print "$program\: Stored UUID '$uuid' in file '$file'\n" unless $quiet;
89 print F "$uuid\n";
90 close (F);
91 exit 0;
95 sub find_hostuuid() {
96 # if ($create_uuid) {return 0 unless -e $file;}
97 return 0 unless ((-e $file && $create_uuid) || !$create_uuid);
98 open (F, "<", "$file") or do {
99 die "$program: Could not open file: '$file'\: $!\n" unless $verify_uuid || $quiet;
100 exit 2;
102 while (<F>) {
103 if (/^([0-9a-z]{8}-([0-9a-z]{4}-){3}[0-9a-z]{12})/i) {
104 close (F);
105 return $1;
110 sub main() {
111 unless (-t STDIN) {
112 if (<STDIN> =~ /^([0-9a-z]{8}-([0-9a-z]{4}-){3}[0-9a-z]{12})/i) {
113 print "$1 is a valid UUID\n" unless $verify_uuid || $quiet;
114 exit;
115 } else {
116 print "NOT a valid UUID\n" unless $verify_uuid || $quiet;
117 exit 105;
120 chomp $file;
121 print_version() if $print_version;
122 print_help(0) if $print_help;
123 create_uuid() if $create_uuid;
125 my $uuid = find_hostuuid();
126 if ($uuid) {
127 print "$uuid\n" unless $verify_uuid || $quiet;
128 exit 0;
129 } else {
130 print STDERR "$program: Could not find UUID in '$file'\n" unless $verify_uuid || $quiet;
131 exit 100;
135 main();