#!/bin/sh
# ~jhs/public_html/bin/.sh/mailman_transmit.sh

# This works but is not necessary: #!/bin/sh -
# Re otional trailing hyphen on 1st line:
# The only script I see used in /etc/crontab is /usr/sbin/periodic
# which has a trailing hyphen on 1st line, not sure why
# maybe to to do with stdio ? Maybe needed to allow crontab to work ?
# Man sh says:
#  When first starting, the shell inspects
#  argument 0, and if it begins with a dash (`-'), the shell is also consid-
#  ered a login shell.  This is normally done automatically by the system
#  when the user first logs in.  A login shell first reads commands from the
#  files /etc/profile and then .profile in a user's home directory, if they
#  exist.  If the environment variable ENV is set on entry to a shell, or is
#  set in the .profile of a login shell, the shell then subjects its value
#  to parameter expansion and arithmetic expansion and reads commands from
#  the named file.

# Called by eg /etc/crontab
#  #Minute Hour    Mday    Month   Wday    Who     Command
#  20      4       *       *       7       jhs:mailman     /home/jhs/public_html/bin/.sh/mailman_transmit.sh

# A who value of jhs fails to do cd /usr/backup/host/list/mailman.tars
# A who value of mailman fails
# A who value of jhs:mailman succeeds with /usr/backup/host/list/mailman.tars
# I think a value of mailman fails, next try mailman:mailman

# See Also:
#	~/bin/.sh/majordomo_transmit.sh		Obsolete
#	~/bin/.sh/majordomo_receive.sh		Obsolete
#	~/bin/.sh/mailman_receive.sh
#	/site/domain/berklix/etc/Distfile.master

# Backs up mail list subscription lists to $bak, & mails tar of lists to home.
# Called by /etc/crontab with eg:
#	#minute hour mday month wday who command
#	21 20 * * 7 mailman /home/jhs/public_html/bin/.sh/mailman_transmit.sh
# This used to create a 50M+ file + size for encoding in /var/mail,
# so I had to consider when best to create it, now instead I mail
# prompting an ftp.

date=`date -u +%Y-%m-%dT%H:%M:%SZ`
#	Sample:	2017-09-12T06:57:52+00:00
#	From:	http://en.wikipedia.org/wiki/ISO_8601
name=`basename $0`
bak="/usr/backup/host/list/mailman.tars"
dirs="/usr/local/mailman"
#	JJLATER refine this:
#		majordomo used ~: dirs=
#					"/usr/local/mailman.lists
#					/usr/local/mailman.archives
#					/usr/local/mailman.digests"
#		See Also MAILMAN_EXCEPT in /site/domain/berklix/Distfile

recipients=jhs-list
# recipients=jhs-list,list_backup@list2
#	Uncomment if I want a copy in remote backup server mbox
# or instead backup to spare server via /etc/Distfile

# slim /etc/crontab has:
#	21 20 * * 7 mailman /home/jhs/public_html/bin/.sh/mailman_transmit.sh
# which in winter Germany in TZ=GMT+01:00 produces: 2017-12-17T20:21

chmod 770		$bak	# Block normal users
chown jhs:mailman	$bak	# Allow script to write new archive in dir.
# chown mailman:mailman	$bak	# Allow script to write new archive in dir.

# Check Access
for i in $bak $dirs ; do
	cd $i || echo "$0: Failed to cd $i" | \
		mail -s "${name}: Failed to cd $i ${date}" ${recipients}
	cd $i || printf '%s:\tFailed to cd %s\n' ${name} $i | \
		mail -s "${name}: Failed to cd $i ${date}" ${recipients}
	cd $i || exit 1
	done

# Create, then remove read permission for others, before writing data.
# tarsf="$bak/mailman.${date}.lists.tgz"
# tarsf="$tarsf $bak/mailman.${date}.archives.tgz"
# tarsf="$tarsf $bak/mailman.${date}.digests.tgz"
# JJLATER add more for refinement
tarsf="${bak}/${date}.tgz"

touch ${tarsf}
cd /usr/local/mailman && nice tar zcf ${tarsf} .
chmod 440 ${tarsf}
chown jhs:mailman ${tarsf}

# To see how much space .pdf & .jpg are wasting in archive:
#	find . -type d -name attachments | du -s -k

ls -l ${tarsf} | mail -s mailman_transmit.sh jhs
# This ls proves it does get created

# Used to mail a copy for off server remote backup
#	uuencode ${date}.tgz < ${tarsf} | \
#		mail -s "${name} ${date}" ${recipients}
# Bloat mail, causes overflows, so do not mail, as causes:
#	SIZE	WHERE
#	34M /host/slim/usr/backup/host/list/mailman.tars/2017-09-27T00:44.tgz
#	47M /host/land/var/mail/jhs.cp
#	47M /host/fire/home/jhs/mail/Cron/mailman_fetch/1
#	47M /host/lapr/crypt/fs/1700m/home/jhs/._dir/mail/Cron/mailman_fetch/1

# Mail to request manually sftp-ing it.
# printf \
#  "%s:\nDo This:\n\tgate:\tsftp `hostname`\n\t\tget ${bak}/${date}.tgz\n\tuser:\tcd /usr/backup/host/list/mailman.tars; sftp gate\n\t\tget ${date}.tgz\nThis times out on gate:\n\tfetch ftp://`hostname`${bak}/${date}.tgz" \
#	${name} | mail -s "${name} ${date}" ${recipients}

# printf \
#  "%s:\
# \nDo This:\
# \n\tgate:\tsftp `hostname`\
# \n\t\tget ${bak}/${date}.tgz\
# \n\tuser:\tcd /usr/backup/host/list/mailman.tars\
# \n\t\tsftp gate\
# \n\t\tget ${date}.tgz\
# \nThis times out on gate:\
# \n\tfetch ftp://`hostname`${bak}/${date}.tgz" \
# ${name} | mail -s "${name} ${date}" ${recipients}

printf \
 "%s:\
\nOn display host, do:\
\n cd /usr/backup/host/list/mailman.tars\
\n Optional: ssh gate\
\n sftp `hostname`\
\n get ${bak}/${date}.tgz\
\n Optional: sftp gate\
\n Optional: get ${date}.tgz\
\n gate:\tsftp `hostname`\
" ${name} | mail -s "${name} ${date}" ${recipients}

# This times out on gate: \n\tfetch ftp://`hostname`${bak}/${date}.tgz

# The ${name} of this shell is known to a rule in ~jhs/.procmailrc

# To make a manual readable copy of configs:
# cd /usr/local/mailman/lists
#	for i in * ; do
#	/usr/local/mailman/bin/config_list -o /home/jhs/mailman_config/$i $i
#	done
#	chmod 440		/home/jhs/mailman_config/*
#	chown jhs:mailman	/home/jhs/mailman_config/*
