From babbae0f6f2dd9d8744ff442595e751a24809acc Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 18 Apr 2022 02:50:08 +0200 Subject: New script to count visitors on my website --- src/cron/counter.sh | 10 +++++ src/cron/gitjoe.pl | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ src/gitjoe/chpublic.pl | 43 ------------------- src/update/gitjoe.pl | 113 ------------------------------------------------- 4 files changed, 123 insertions(+), 156 deletions(-) create mode 100755 src/cron/counter.sh create mode 100755 src/cron/gitjoe.pl delete mode 100755 src/gitjoe/chpublic.pl delete mode 100755 src/update/gitjoe.pl diff --git a/src/cron/counter.sh b/src/cron/counter.sh new file mode 100755 index 0000000..f3a7656 --- /dev/null +++ b/src/cron/counter.sh @@ -0,0 +1,10 @@ +#!/bin/sh -e + +logfile='/var/log/nginx/access.log' +destfile='/usr/local/www/jozan/index.html' +foretext='Unique visitors: ' +tmp=$(mktemp) + +sed "s/$foretext.*$/$foretext$(awk '{print $1}' $logfile | sort | uniq | wc -l | tr -d ' ')/" $destfile >"$tmp" +cat "$tmp" >$destfile +rm "$tmp" diff --git a/src/cron/gitjoe.pl b/src/cron/gitjoe.pl new file mode 100755 index 0000000..bb668b2 --- /dev/null +++ b/src/cron/gitjoe.pl @@ -0,0 +1,113 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; +use File::Copy; +use Capture::Tiny; +use constant { + TMP_DIR => '/tmp/gitjoe/', + SITE_DIR => '/usr/local/www/gitjoe/' +}; + +sub get_repos_index { + my $user = $_[0]; + my $home_dir = '/usr/home/' . $user . '/'; + opendir(DIR, $home_dir); + my @repos; + my $i = 0; + while (my $dir = readdir(DIR)) { + next if ($dir =~ m/^\./); + next if (!(-e $home_dir . $dir . '/git-daemon-export-ok')); + $repos[$i] = $dir; + $i += 1; + } + closedir(DIR); + my @sorted_repos = sort @repos; + return @sorted_repos; +} + +sub stagit_generate { + my ($user, @repos) = @_; + my $home_dir = '/usr/home/' . $user . '/'; + chdir(TMP_DIR); + mkdir($user . '/', 0755); + my $i = 0; + my $repos_line = ""; + copy(SITE_DIR . 'css/gitjoe.css', './' . $user . '/style.css'); + copy(SITE_DIR . 'img/logo.png', './' . $user . '/logo.png'); + while ($i < @repos) { + chdir(TMP_DIR . $user . '/'); + $repos_line = $repos_line . ' ' . $home_dir . $repos[$i] . '/'; + substr($repos[$i], -4) = ""; + mkdir($repos[$i] . '/', 0755); + chdir(TMP_DIR . $user . '/' . $repos[$i] . '/'); + $repos[$i] = $repos[$i] . '.git'; + print "Indexing " . colored($user . '/' . $repos[$i], 'bold') . ".\n"; + system( + '/usr/local/bin/stagit', + $home_dir . $repos[$i] . '/' + ); + copy('../style.css', './style.css'); + copy('../logo.png', './logo.png'); + $i += 1; + } + chdir(TMP_DIR . $user . '/'); + system( + '/usr/local/bin/dash', + '-c', + '/usr/local/bin/stagit-index ' . $repos_line . '>index.html' + ); + system( + '/usr/local/bin/dash', + '-c', + "/usr/local/bin/gsed -i 's/" . $user . "<\\/td>/" . $user . "<\\/td>/g' index.html" + ); + system( + '/usr/local/bin/dash', + '-c', + "/usr/local/bin/gsed -i 's/Repositories<\\/span><\\/td>/

" . $user . " - Repositories<\\/h1><\\/span><\\/td><\\/tr><\\/td>Back to GitJoe<\\/a><\\/td><\\/tr>/' index.html" + ); + system( + '/usr/local/bin/dash', + '-c', + "/usr/local/bin/gsed -i 's/log.html/files.html/g' index.html" + ); + return; +} + +sub main { + my $home_dir = '/usr/home/'; + my @users; + opendir(DIR, $home_dir); + my $i = 0; + while (my $dir = readdir(DIR)) { + next if ($dir eq 'git-ro'); + next if ($dir =~ m/^\./); + $users[$i] = $dir; + $i += 1; + } + closedir(DIR); + $i = 0; + mkdir(TMP_DIR, 0755); + while ($i < @users) { + my @repos = get_repos_index($users[$i]); + stagit_generate($users[$i], @repos); + print "Removing user " . colored($users[$i], 'bold green') . " old directory from " . colored(SITE_DIR, 'bold') . ".\n"; + system( + '/bin/rm', + '-rf', + SITE_DIR . $users[$i] + ); + print "Moving user " . colored($users[$i], 'bold green') . " newly generated directory to " . colored(SITE_DIR, 'bold') . ".\n"; + move(TMP_DIR . $users[$i], SITE_DIR . $users[$i]); + $i += 1; + } + rmdir(TMP_DIR); + print "Updated GitJoe index.\n"; + exit; +} + +main(); + +__END__ diff --git a/src/gitjoe/chpublic.pl b/src/gitjoe/chpublic.pl deleted file mode 100755 index 9f17618..0000000 --- a/src/gitjoe/chpublic.pl +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/local/bin/perl - -use strict; -use warnings; -use Term::ANSIColor; - -sub main -{ - my $argc = $#ARGV + 1; - if ($argc < 2) { - print colored("Failed!\n", 'bold red') - . "Missing argument, 2 needed " - . colored("[user - reponame]", 'bold') - . "\n"; - exit 1; - } - my $usr = $ARGV[0]; - my $repo = $ARGV[1]; - my $home_dir = '/usr/home/' . $usr . '/'; - if (substr($repo, -4) ne '.git') { - $repo = $repo . '.git'; - } - my $state; - if (-e $home_dir . $repo . '/git-daemon-export-ok') { - unlink($home_dir . $repo . '/git-daemon-export-ok'); - $state = 'private'; - } - else { - open(my $fh, '>', $home_dir . $repo . '/git-daemon-export-ok'); - close($fh); - $state = 'public'; - my (undef, undef, $uid, $gid) = getpwnam($usr); - chown $uid, $gid, $home_dir . $repo . '/git-daemon-export-ok'; - } - print "Changed git repository " . colored($repo, 'bold green') - . " for user " . colored($usr, 'bold green') . " visibility " - . colored('state', 'bold') . " to " . colored($state, 'bold green') . ".\n"; - exit; -} - -main(); - -__END__ diff --git a/src/update/gitjoe.pl b/src/update/gitjoe.pl deleted file mode 100755 index bb668b2..0000000 --- a/src/update/gitjoe.pl +++ /dev/null @@ -1,113 +0,0 @@ -#!/usr/local/bin/perl - -use strict; -use warnings; -use Term::ANSIColor; -use File::Copy; -use Capture::Tiny; -use constant { - TMP_DIR => '/tmp/gitjoe/', - SITE_DIR => '/usr/local/www/gitjoe/' -}; - -sub get_repos_index { - my $user = $_[0]; - my $home_dir = '/usr/home/' . $user . '/'; - opendir(DIR, $home_dir); - my @repos; - my $i = 0; - while (my $dir = readdir(DIR)) { - next if ($dir =~ m/^\./); - next if (!(-e $home_dir . $dir . '/git-daemon-export-ok')); - $repos[$i] = $dir; - $i += 1; - } - closedir(DIR); - my @sorted_repos = sort @repos; - return @sorted_repos; -} - -sub stagit_generate { - my ($user, @repos) = @_; - my $home_dir = '/usr/home/' . $user . '/'; - chdir(TMP_DIR); - mkdir($user . '/', 0755); - my $i = 0; - my $repos_line = ""; - copy(SITE_DIR . 'css/gitjoe.css', './' . $user . '/style.css'); - copy(SITE_DIR . 'img/logo.png', './' . $user . '/logo.png'); - while ($i < @repos) { - chdir(TMP_DIR . $user . '/'); - $repos_line = $repos_line . ' ' . $home_dir . $repos[$i] . '/'; - substr($repos[$i], -4) = ""; - mkdir($repos[$i] . '/', 0755); - chdir(TMP_DIR . $user . '/' . $repos[$i] . '/'); - $repos[$i] = $repos[$i] . '.git'; - print "Indexing " . colored($user . '/' . $repos[$i], 'bold') . ".\n"; - system( - '/usr/local/bin/stagit', - $home_dir . $repos[$i] . '/' - ); - copy('../style.css', './style.css'); - copy('../logo.png', './logo.png'); - $i += 1; - } - chdir(TMP_DIR . $user . '/'); - system( - '/usr/local/bin/dash', - '-c', - '/usr/local/bin/stagit-index ' . $repos_line . '>index.html' - ); - system( - '/usr/local/bin/dash', - '-c', - "/usr/local/bin/gsed -i 's/" . $user . "<\\/td>/" . $user . "<\\/td>/g' index.html" - ); - system( - '/usr/local/bin/dash', - '-c', - "/usr/local/bin/gsed -i 's/Repositories<\\/span><\\/td>/

" . $user . " - Repositories<\\/h1><\\/span><\\/td><\\/tr><\\/td>Back to GitJoe<\\/a><\\/td><\\/tr>/' index.html" - ); - system( - '/usr/local/bin/dash', - '-c', - "/usr/local/bin/gsed -i 's/log.html/files.html/g' index.html" - ); - return; -} - -sub main { - my $home_dir = '/usr/home/'; - my @users; - opendir(DIR, $home_dir); - my $i = 0; - while (my $dir = readdir(DIR)) { - next if ($dir eq 'git-ro'); - next if ($dir =~ m/^\./); - $users[$i] = $dir; - $i += 1; - } - closedir(DIR); - $i = 0; - mkdir(TMP_DIR, 0755); - while ($i < @users) { - my @repos = get_repos_index($users[$i]); - stagit_generate($users[$i], @repos); - print "Removing user " . colored($users[$i], 'bold green') . " old directory from " . colored(SITE_DIR, 'bold') . ".\n"; - system( - '/bin/rm', - '-rf', - SITE_DIR . $users[$i] - ); - print "Moving user " . colored($users[$i], 'bold green') . " newly generated directory to " . colored(SITE_DIR, 'bold') . ".\n"; - move(TMP_DIR . $users[$i], SITE_DIR . $users[$i]); - $i += 1; - } - rmdir(TMP_DIR); - print "Updated GitJoe index.\n"; - exit; -} - -main(); - -__END__ -- cgit v1.2.3