mktar: Use `wc` instead of `du` in summary message
[sunny256-utils.git] / ga-pwd
blob2398bac4b3b8acc53163f7c8e2c0a678aedcdee3
1 #!/usr/bin/env perl
3 #=======================================================================
4 # ga-pwd
5 # File ID: b213e514-0d5f-11e6-9cac-fefdb24f8e10
7 # Print name of current directory with user name and/or tilde
9 # Character set: UTF-8
10 # ©opyleft 2016– Ø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;
18 use Cwd 'abs_path';
20 local $| = 1;
22 our %Opt = (
24 'help' => 0,
25 'hostname' => 0,
26 'quiet' => 0,
27 'username' => 0,
28 'verbose' => 0,
29 'version' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.2.0';
37 Getopt::Long::Configure('bundling');
38 GetOptions(
40 'help|h' => \$Opt{'help'},
41 'hostname|H' => \$Opt{'hostname'},
42 'quiet|q+' => \$Opt{'quiet'},
43 'username|u' => \$Opt{'username'},
44 'verbose|v+' => \$Opt{'verbose'},
45 'version' => \$Opt{'version'},
47 ) || die("$progname: Option error. Use -h for help.\n");
49 $Opt{'verbose'} -= $Opt{'quiet'};
50 $Opt{'help'} && usage(0);
51 if ($Opt{'version'}) {
52 print_version();
53 exit(0);
56 exit(main());
58 sub main {
59 # {{{
60 my $Retval = 0;
61 my $abs_path = abs_path('.');
62 my $home_path = abs_path($ENV{'HOME'});
63 my $outpath = $abs_path;
65 if ($Opt{'username'}) {
66 print(getpwuid($<) . '@');
67 $outpath =~ s/^$home_path/~/;
69 chomp(my $hostname = `hostname`);
70 if ($abs_path =~ /^\/media\//) {
71 $Opt{'hostname'} && print($hostname . ':');
72 printf("%s\n", $abs_path);
73 return $Retval;
75 print("$hostname:$outpath\n");
77 return $Retval;
78 # }}}
79 } # main()
81 sub print_version {
82 # Print program version {{{
83 print("$progname $VERSION\n");
84 return;
85 # }}}
86 } # print_version()
88 sub usage {
89 # Send the help message to stdout {{{
90 my $Retval = shift;
92 if ($Opt{'verbose'}) {
93 print("\n");
94 print_version();
96 print(<<"END");
98 Print path of current directory, using the format "host:pwd". Use the
99 "physical" directory location, ignore parent symlinks.
101 If the current directory is below \$HOME and -u/--username is specified,
102 replace the first part with '~'.
104 If the current directory is below /media/, don't print username or
105 hostname by default.
107 Usage: $progname [options]
109 Options:
111 -H, --hostname
112 Include hostname in output even under "/media/".
113 -h, --help
114 Show this help.
115 -q, --quiet
116 Be more quiet. Can be repeated to increase silence.
117 -u, --username
118 Include local user name in output.
119 -v, --verbose
120 Increase level of verbosity. Can be repeated.
121 --version
122 Print version information.
125 exit($Retval);
126 # }}}
127 } # usage()
129 sub msg {
130 # Print a status message to stderr based on verbosity level {{{
131 my ($verbose_level, $Txt) = @_;
133 if ($Opt{'verbose'} >= $verbose_level) {
134 print(STDERR "$progname: $Txt\n");
136 return;
137 # }}}
138 } # msg()
140 __END__
142 # This program is free software; you can redistribute it and/or modify
143 # it under the terms of the GNU General Public License as published by
144 # the Free Software Foundation; either version 2 of the License, or (at
145 # your option) any later version.
147 # This program is distributed in the hope that it will be useful, but
148 # WITHOUT ANY WARRANTY; without even the implied warranty of
149 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
150 # See the GNU General Public License for more details.
152 # You should have received a copy of the GNU General Public License
153 # along with this program.
154 # If not, see L<http://www.gnu.org/licenses/>.
156 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :