Create a mailbox
Install fetchmail and procmail
#### .fetchmailrc
set daemon 60
set logfile fetchmail.log
poll mailserver-FQDN proto POP3
user "mail-username" pass "mail-password" is "pi" postconnect "/home/pi/microbit/munpack-microbit.sh"
ssl
fetchall
no keep
no rewrite
mda "/usr/bin/procmail -f %F -d %T";
#.procmailrc.
#
SHELL=/bin/bash
LINEBUF=4096
PATH=/bin:/usr/bin:/usr/local/bin:/opt/local/bin
VERBOSE=off
MAILDIR=/home/pi/maildir
DEFAULT=$MAILDIR/inbox/ # See the slash!
LOGFILE=$HOME/procmaillog
FORMAIL=/usr/bin/formail
SENDMAIL=/usr/sbin/sendmail
## save everything in Maildir format in ~/maildir
#
:0
$MAILDIR/
This means the fetched mails will be saved as individual files in ~pi/maildir/new/
Install mpack
#!/bin/bash
ls ~pi/maildir/new/???* 2> /dev/null > /dev/null || exit 0
for mailfile in ~pi/maildir/new/*; do
munpack -C ~pi/microbit/ $mailfile ;
mv $mailfile ~pi/maildir/cur/ ;
done
disklist=""
for disk in $(echo "sda sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo"); do
[[ -L /sys/block/$disk ]] && [[ "$(cat /sys/block/$disk/size)" -le "131200" ]] && disklist="$disklist $disk"
done
for disk in $(echo $disklist); do
if [[ -L /sys/block/$disk ]]; then
#[[ "x$(cat /sys/block/$disk/size)" -eq "x16512" ]] || continue #flash size old Micro:bit firmware
#[[ "x$(cat /sys/block/$disk/size)" -eq "x131200" ]] || continue #flash size new Micro:bit firmware
[[ "$(cat /sys/block/$disk/size)" -le "131200" ]] || continue # only continue on small disks
[[ -d /tmp/$disk ]] || mkdir /tmp/$disk
[[ -e /tmp/$disk/MICROBIT.HTM ]] || sudo mount -t vfat /dev/$disk /tmp/$disk
fi
done
ls ~pi/microbit/*.hex 2> /dev/null > /dev/null || exit 0
for hexfile in ~pi/microbit/*.hex ; do
#echo $hexfile | grep -iq scroll && disklist="sdh sdi sdj" # use fixed disklist for scroll:bit
#echo $hexfile | grep -iq halo && disklist="sdg" # use fixed disklist for Kitronik ZIP Halo
[[ -d ~pi/microbit/used ]] || mkdir ~pi/microbit/used
for disk in $(echo $disklist); do
[[ -e /tmp/$disk/MICROBIT.HTM ]] && sudo cp $hexfile /tmp/$disk/
sudo sync
done
mv $hexfile ~pi/microbit/used/
done
exit 0
Note that some actions like mount require root privileges, so the user running this (pi, in our case) needs passwordless sudo privileges.
Run it
Now start fetchmail: fetchmail -f ~pi/.fetchmailrc . You can monitor the fetchmail log file (see fetchmailrc) using tail -f. Fetchmail will run as a daemon until you stop it using fetchmail -q.
Comments