From 270058507061600c4451eab173ab485644430166 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Wed, 20 Jul 2016 17:56:50 +0200
Subject: improved title and last-run reporting


diff --git a/rhautoimport b/rhautoimport
index 93d7823..6214348 100755
--- a/rhautoimport
+++ b/rhautoimport
@@ -26,51 +26,13 @@ if [ -z "$1" ]; then
   exit 1
 fi
 
-TITLE="unknown"
-case "$1" in
-  btl)
-    TITLE="Between the Lines"
-  ;;
-  dn)
-    TITLE="Democracy Now!"
-  ;;
-  er)
-    TITLE="Ergo"
-  ;;
-  eu)
-    TITLE="Economic Update"
-  ;;
-  fl)
-    TITLE="Frequenz Leitwolf"
-  ;;
-  nw)
-    TITLE="netwatcher"
-  ;;
-  oi)
-    TITLE="Onda-Info"
-  ;;
-  po)
-    TITLE="Panoptikum"
-  ;;
-  ra)
-    TITLE="radio%attac"
-  ;;
-  rs)
-    TITLE="Radio Stimme"
-  ;;
-  tr)
-    TITLE="Tierrechtsradio"
-  ;;
-  wr)
-    TITLE="Werkstatt-Radio"
-  ;;
-  *)
-    echo "unknown importer: $1"
-    exit 2
-  ;;
-esac
+if [ ! -x "/usr/bin/rhautoimport-$1" ]; then
+  echo "unknown importer: $1"
+  exit 2
+fi
 
 LOG_FILE=`mktemp --tmpdir rhautoimport-XXXXXX.log`
+RESULT_FILE=`mktemp --tmpdir rhautoimport-XXXXXX.result`
 MAIL_TO=`sed 's/#.*//' /etc/rhautoimport/$1.mail 2> /dev/null | xargs`
 if [ -z "$MAIL_TO" ]; then
   MAIL_TO="root"
@@ -79,8 +41,28 @@ fi
 (
   /usr/bin/flock -x -w 10 200
 
-  /usr/bin/rhautoimport-$1 ${@:2} > $LOG_FILE 2>&1
-  case $? in
+  /usr/bin/rhautoimport-$1 ${@:2} > $LOG_FILE 2>&1 3> $RESULT_FILE
+  RETURN_CODE=$?
+  TITLE=""
+  WAS_LAST=0
+  idx=0
+  while IFS= read -r line; do
+    case $idx in
+      0)
+        TITLE=$line
+        ;;
+      1)
+        WAS_LAST=$line
+        ;;
+      *)
+        break
+        ;;
+    esac
+    idx=$((idx+1))
+  done < $RESULT_FILE
+  rm -f $RESULT_FILE
+
+  case $RETURN_CODE in
     0)
       bsd-mailx -a "Content-Type: text/plain; charset=utf-8" -s "$TITLE - Imported Successfully" $MAIL_TO < $LOG_FILE
     ;;
@@ -91,7 +73,7 @@ fi
       # do nothing - the script didn't import anything but no message should be sent
     ;;
     *)
-      if [ "$2" == 'last' ]; then
+      if [ $WAS_LAST -eq 1 ]; then
         subject="$TITLE - Import Error - last attempt!!!"
       else
         subject="$TITLE - Import Error - will retry"
diff --git a/rhautoimport-btl b/rhautoimport-btl
index b07c5b9..d8138dc 100755
--- a/rhautoimport-btl
+++ b/rhautoimport-btl
@@ -38,6 +38,7 @@ my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-btl.stat";
 my $RSS_URL = "http://www.btlonline.org/rss/btl128.xml";
 my $RD_GROUP = "betweenlin";
 my $PV_ID = '221';
+my $TITLE="Between the Lines";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -47,6 +48,7 @@ binmode(STDERR, ":utf8");
 my $curweek = RHRD::utils::get_rd_week();
 if($curweek == 2 || $curweek == 4) {
   print "please don't run this script in week 2 or 4!\n";
+  rhautoimport::report_title_and_last($TITLE, 0);
   exit 42;
 }
 
@@ -54,6 +56,7 @@ if($#ARGV >= 0 && $ARGV[0] eq 'last') {
   print "!!!This is the last attempt, there won't be a retry on error!!!\n";
   $LAST_RUN = 1;
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
 
 
 my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP);
diff --git a/rhautoimport-dn b/rhautoimport-dn
index d4bab91..b29ef1a 100755
--- a/rhautoimport-dn
+++ b/rhautoimport-dn
@@ -38,6 +38,7 @@ my $FILES_RSS_URL = "https://www.democracynow.org/podcast-stations.xml";
 my $HEADLINES_RSS_URL = "http://www.democracynow.org/podcast.xml";
 my $RD_GROUP = "democracyn";
 my $PV_ID = '111';
+my $TITLE="Democracy Now!";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -48,6 +49,8 @@ if($#ARGV >= 0 && $ARGV[0] eq 'last') {
   print "!!!This is the last attempt, there won't be a retry on error!!!\n";
   $LAST_RUN = 1;
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
+
 
 my @today = Date::Calc::Today();
 my @yesterday = Date::Calc::Add_Delta_Days(@today, -1);
diff --git a/rhautoimport-er b/rhautoimport-er
index bca53d8..dca673e 100755
--- a/rhautoimport-er
+++ b/rhautoimport-er
@@ -40,6 +40,7 @@ my $RSS_URL = "https://cba.fro.at/series/ergo/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "ergo";
 my $PV_ID = '569';
+my $TITLE="Ergo";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -54,6 +55,7 @@ if($curweek == 1 || $curweek == 3) {
     $LAST_RUN = 1;
   }
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
 
 
 my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP);
diff --git a/rhautoimport-eu b/rhautoimport-eu
index 72509c3..79867d4 100755
--- a/rhautoimport-eu
+++ b/rhautoimport-eu
@@ -34,6 +34,7 @@ my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-eu.stat";
 my $API_URL =  "https://api.audioboom.com/users/3848170/audio_clips";
 my $RD_GROUP = "ecoupdate";
 my $PV_ID = '568';
+my $TITLE="Economic Update";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -44,6 +45,8 @@ if($#ARGV >= 0 && $ARGV[0] eq 'last') {
   print "!!!This is the last attempt, there won't be a retry on error!!!\n";
   $LAST_RUN = 1;
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
+
 
 my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP);
 if(!defined $allowed_dbs[0] && defined $allowed_dbs[1]) {
diff --git a/rhautoimport-fl b/rhautoimport-fl
index 441a51a..2b8cea0 100755
--- a/rhautoimport-fl
+++ b/rhautoimport-fl
@@ -42,6 +42,7 @@ my $RSS_URL = "https://cba.fro.at/series/frequenz-leitwolf/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "leitwolf";
 my $PV_ID = '571';
+my $TITLE="Frequenz Leitwolf";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -56,6 +57,7 @@ if($curweek == 1 || $curweek == 3) {
     $LAST_RUN = 1;
   }
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
 
 
 my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP);
diff --git a/rhautoimport-nw b/rhautoimport-nw
index c8ea341..f048613 100755
--- a/rhautoimport-nw
+++ b/rhautoimport-nw
@@ -38,6 +38,7 @@ my $RSS_URL = "https://cba.fro.at/series/radio-netwatcher/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "netwatcher";
 my $PV_ID = '73';
+my $TITLE="netwatcher";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -48,6 +49,7 @@ if($#ARGV >= 0 && $ARGV[0] eq 'last') {
   print "!!!This is the last attempt, there won't be a retry on error!!!\n";
   $LAST_RUN = 1;
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
 
 
 my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP);
diff --git a/rhautoimport-oi b/rhautoimport-oi
index dd6a2fc..274ad9d 100755
--- a/rhautoimport-oi
+++ b/rhautoimport-oi
@@ -39,6 +39,7 @@ my $LAST_FILE = $ENV{'HOME'} . "/rhautoimport-oi.last";
 my $RSS_URL = "https://www.npla.de/feed/podcast/?podcast_series=onda-info";
 my $RD_GROUP = "ondainfo";
 my $PV_ID = '220';
+my $TITLE="Onda-Info";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -48,6 +49,7 @@ binmode(STDERR, ":utf8");
 my $curweek = RHRD::utils::get_rd_week();
 if($curweek == 1 || $curweek == 3) {
   print "please don't run this script in week 1 or 3!\n";
+  rhautoimport::report_title_and_last($TITLE, 0);
   exit 42;
 }
 
@@ -55,6 +57,7 @@ if($#ARGV >= 0 && $ARGV[0] eq 'last') {
   print "!!!This is the last attempt, there won't be a retry on error!!!\n";
   $LAST_RUN = 1;
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
 
 
 my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP);
diff --git a/rhautoimport-po b/rhautoimport-po
index 8d3caeb..b8bf682 100755
--- a/rhautoimport-po
+++ b/rhautoimport-po
@@ -40,6 +40,7 @@ my $RSS_URL = "https://cba.fro.at/series/panoptikum-bildung/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "panoptikum";
 my $PV_ID = '570';
+my $TITLE="Panoptikum";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -54,6 +55,8 @@ if($curweek == 2 || $curweek == 4) {
     $LAST_RUN = 1;
   }
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
+
 
 my @today = Date::Calc::Today();
 print "today: " . Date::Calc::Date_to_Text(@today) . " (Week: " . $curweek . ")\n";
diff --git a/rhautoimport-ra b/rhautoimport-ra
index 164797f..3ec06f3 100755
--- a/rhautoimport-ra
+++ b/rhautoimport-ra
@@ -38,6 +38,7 @@ my $RSS_URL = "https://cba.fro.at/series/radioattac/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "RadioATTAC";
 my $PV_ID = '76';
+my $TITLE="radio%attac";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -48,6 +49,7 @@ if($#ARGV >= 0 && $ARGV[0] eq 'last') {
   print "!!!This is the last attempt, there won't be a retry on error!!!\n";
   $LAST_RUN = 1;
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
 
 
 my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP);
diff --git a/rhautoimport-rs b/rhautoimport-rs
index 32a57b6..21c0a1a 100755
--- a/rhautoimport-rs
+++ b/rhautoimport-rs
@@ -39,6 +39,7 @@ my $RSS_URL = "https://cba.fro.at/series/radio-stimme/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "radioStimm";
 my $PV_ID = '75';
+my $TITLE="Radio Stimme";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -62,6 +63,8 @@ if($#ARGV >= 0) {
     }
   }
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
+
 
 my @today = Date::Calc::Today();
 print "today: " . Date::Calc::Date_to_Text(@today) . "\n";
diff --git a/rhautoimport-tr b/rhautoimport-tr
index 095402c..a1aa235 100755
--- a/rhautoimport-tr
+++ b/rhautoimport-tr
@@ -38,6 +38,7 @@ my $RSS_URL = "https://cba.fro.at/series/tierrechtsradio/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "tierrecht";
 my $PV_ID = '506';
+my $TITLE="Tierrechtsradio";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -47,6 +48,7 @@ binmode(STDERR, ":utf8");
 my @today = Date::Calc::Standard_to_Business(Date::Calc::Today());
 if($today[2] == 5) {
   print "please don't run this script on fridays!\n";
+  rhautoimport::report_title_and_last($TITLE, 0);
   exit 42;
 }
 
@@ -54,6 +56,7 @@ if($#ARGV >= 0 && $ARGV[0] eq 'last') {
   print "!!!This is the last attempt, there won't be a retry on error!!!\n";
   $LAST_RUN = 1;
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
 
 
 my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP);
diff --git a/rhautoimport-wr b/rhautoimport-wr
index 97b38b3..4eb055d 100755
--- a/rhautoimport-wr
+++ b/rhautoimport-wr
@@ -40,6 +40,7 @@ my $RSS_URL = "https://cba.fro.at/series/werkstatt-radio/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "werkstatt";
 my $PV_ID = '567';
+my $TITLE="Werkstatt-Radio";
 my $LAST_RUN = 0;
 
 binmode(STDIN, ":utf8");
@@ -54,6 +55,7 @@ if($curweek == 1 || $curweek == 3) {
     $LAST_RUN = 1;
   }
 }
+rhautoimport::report_title_and_last($TITLE, $LAST_RUN);
 
 
 my @today = Date::Calc::Today();
diff --git a/rhautoimport.pm b/rhautoimport.pm
index 7c0fd50..0f9f197 100644
--- a/rhautoimport.pm
+++ b/rhautoimport.pm
@@ -45,6 +45,18 @@ use constant {
   CBA_API_KEY => '/etc/cba-api.key'
 };
 
+sub report_title_and_last
+{
+  my ($title, $last_run) = @_;
+
+  $title = "" unless $title;
+  $last_run = 0 unless $last_run;
+
+  if (open my $handle, '>&=3') {
+    print $handle $title . "\n";
+    print $handle $last_run . "\n";
+  }
+}
 
 sub get_dropboxes
 {
-- 
cgit v0.10.2