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.
24 use Getopt
::Long
qw(:config no_ignore_case bundling);
28 my $program = basename
($0);
30 my $file = '/etc/hostuuid';
40 'help|h|?' => \
$print_help,
41 'version|v' => \
$print_version,
42 'create|c' => \
$create_uuid,
44 'verify' => \
$verify_uuid,
50 $program: version $version
52 This program is released under GPLv3
53 Written by Richard Hartmann for Debian and Grml
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
78 my $uuid = find_hostuuid
();
80 print STDERR
"$program: Found UUID '$uuid' in '$file'. Aborting!\n" unless $quiet;
83 open (F
, ">", "$file") or do {
84 die "$program: Could not open file: '$file'\: $!\n" unless $quiet;
87 $uuid = create_UUID_as_string
(UUID_V4
);
88 print "$program\: Stored UUID '$uuid' in file '$file'\n" unless $quiet;
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;
103 if (/^([0-9a-z]{8}-([0-9a-z]{4}-){3}[0-9a-z]{12})/i) {
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;
116 print "NOT a valid UUID\n" unless $verify_uuid || $quiet;
121 print_version
() if $print_version;
122 print_help
(0) if $print_help;
123 create_uuid
() if $create_uuid;
125 my $uuid = find_hostuuid
();
127 print "$uuid\n" unless $verify_uuid || $quiet;
130 print STDERR
"$program: Could not find UUID in '$file'\n" unless $verify_uuid || $quiet;