Files
bundle-bot/bundle-bot-mirror.sh

166 lines
5.7 KiB
Bash

#!/bin/bash
# requires: eepget, i2psnark (directory access), logger
# if ran as root (you shouldn't!) owner/group for the copied file
# will be set (copied from i2psnark dir).
# otherwise the executing user + the i2p user both need
# read+write permissions on i2psnark/
SCRIPT_NAME="bundle-bot-mirror"
PATH_WORK_DIR="/home/bundle-bot/$SCRIPT_NAME"
#FILE_BUNDLE_BOT_I2P_LATEST="bundle-bot-mirror.i2p.i2p.latest"
FILE_BUNDLE_BOT_I2P_CURRENT="$SCRIPT_NAME.i2p.i2p.current"
FILE_BUNDLE_BOT_I2P_OLD="$SCRIPT_NAME.i2p.i2p.old"
FILE_LOCK="$PATH_WORK_DIR/$SCRIPT_NAME.LOCK"
FILE_LOG="$PATH_WORK_DIR/$SCRIPT_NAME.log"
LOGFACILITY="daemon"
LOGTAG=$SCRIPT_NAME
# Start logging all output
exec 1>$FILE_LOG 2>&1
# Log start
logger -s -t $LOGTAG -p $LOGFACILITY.notice "Starting run ..."
if [ -f $FILE_LOCK ]; then
logger -s -t $LOGTAG -p $LOGFACILITY.error "ERROR: lock-file existed during startup - investigate!"
exit 110
else
touch $FILE_LOCK
fi
if [ -z "$PATH_I2PSNARK" ]; then
if [ -d "$HOME/.i2p/i2psnark" ]; then
PATH_I2PSNARK="$HOME/.i2p/i2psnark"
USER_I2PSNARK=`stat -c "%U" $PATH_I2PSNARK`
GROUP_I2PSNARK=`stat -c "%G" $PATH_I2PSNARK`
elif [ -d "$HOME/i2p/i2psnark" ]; then
PATH_I2PSNARK="$HOME/i2p/i2psnark"
USER_I2PSNARK=`stat -c "%U" $PATH_I2PSNARK`
GROUP_I2PSNARK=`stat -c "%G" $PATH_I2PSNARK`
elif [ -d "/var/lib/i2p/i2p-config/i2psnark" ]; then
PATH_I2PSNARK="/var/lib/i2p/i2p-config/i2psnark"
USER_I2PSNARK=`stat -c "%U" $PATH_I2PSNARK`
GROUP_I2PSNARK=`stat -c "%G" $PATH_I2PSNARK`
else
# let's assume this is critical and NOT remove the lock-file
logger -s -t $LOGTAG -p $LOGFACILITY.error "I2PSnark directory not found or not accessible (permissions?). Manual internvetion necessary!"
exit 123
fi
fi
logger -s -t $LOGTAG -p $LOGFACILITY.notice "I2PSnark directory found at: $PATH_I2PSNARK and user/group set to $USER_I2PSNARK/$GROUP_I2PSNARK."
cd $PATH_WORK_DIR
eepget -n 5 http://ae5rbez5qu54o3lt7iczg56bmwvicpymj7al2riuhvogdllz554q.b32.i2p/torrents/
if [ $? -ne 0 ]; then
logger -s -t $LOGTAG -p $LOGFACILITY.notice "Retrieving the torrent list failed :("
rm $FILE_LOCK
exit 1
fi
awk '
BEGIN {
FS = "."
i2p_i2p_max_a=0
i2p_i2p_max_b=0
i2p_i2p_max_c=0
}
(/i2p\.i2p\.i2p-[0-9]\.[0-9]\.[0-9]\.bundle\.torrent$/) {
# print substr($3,5) " " $4 " " $5 ;
in_a=substr($3,5);
in_b=$4;
in_c=$5;
if (in_a > i2p_i2p_max_a) {
i2p_i2p_max_a = in_a;
i2p_i2p_max_b = in_b;
i2p_i2p_max_c = in_c;
next;
} else if ((in_a == i2p_i2p_max_a) && (in_b > i2p_i2p_max_b)) {
i2p_i2p_max_a = in_a;
i2p_i2p_max_b = in_b;
i2p_i2p_max_c = in_c;
next;
} else if ((in_a == i2p_i2p_max_a) && (in_b == i2p_i2p_max_b) && (in_c > i2p_i2p_max_c)) {
i2p_i2p_max_a = in_a;
i2p_i2p_max_b = in_b;
i2p_i2p_max_c = in_c;
next;
}
next;
}
END {
# print "latest version i2p.i2p: " i2p_i2p_max_a "." i2p_i2p_max_b "." i2p_i2p_max_c;
print "i2p.i2p.i2p-" i2p_i2p_max_a "." i2p_i2p_max_b "." i2p_i2p_max_c ".bundle.torrent" > "bundle-bot-mirror.i2p.i2p.latest" ;
}
' torrents
# TODO: hardcoded bundle-bot-mirror.i2p.i2p.latest should be a variable (previous line inside awk, following line in cat)
TORRENT_LATEST=`cat bundle-bot-mirror.i2p.i2p.latest`
logger -s -t $LOGTAG -p $LOGFACILITY.notice "Latest online version: $TORRENT_LATEST"
if [ -f "$FILE_BUNDLE_BOT_I2P_CURRENT" ]; then
TORRENT_CURRENT=`cat $FILE_BUNDLE_BOT_I2P_CURRENT`
logger -s -t $LOGTAG -p $LOGFACILITY.notice "Our current version: $TORRENT_CURRENT"
if [ $TORRENT_CURRENT = $TORRENT_LATEST ]; then
logger -s -t $LOGTAG -p $LOGFACILITY.notice "No new version! Exiting."
rm $FILE_LOCK
exit 0
else
logger -s -t $LOGTAG -p $LOGFACILITY.notice "New version available, attempting to retrieve ..."
fi
else
logger -s -t $LOGTAG -p $LOGFACILITY.notice "We have no current version, yet (first-time-run). Attempting to get the latest online version ..."
fi
eepget -n 5 http://ae5rbez5qu54o3lt7iczg56bmwvicpymj7al2riuhvogdllz554q.b32.i2p/torrents/$TORRENT_LATEST
if [ $? -eq 0 ]; then
logger -s -t $LOGTAG -p $LOGFACILITY.notice "Successfully retrieved the .torrent for the latest online version"
# if current exists, move it to old
if [ -f "$FILE_BUNDLE_BOT_I2P_CURRENT" ]; then
logger -s -t $LOGTAG -p $LOGFACILITY.notice "Removing (possibly existing) old version, copying current to old, and finally replacing current with new"
# if old exists, delete it (in WORK_DIR + I2PSNARK)
if [ -f "$FILE_BUNDLE_BOT_I2P_OLD" ]; then
TORRENT_OLD=`cat $FILE_BUNDLE_BOT_I2P_OLD`
# TODO delete old downloaded file here automatically?
rm --force $PATH_I2PSNARK/$TORRENT_OLD
rm --force $TORRENT_OLD
fi
mv --force $FILE_BUNDLE_BOT_I2P_CURRENT $FILE_BUNDLE_BOT_I2P_OLD
fi
# now store the latest name as our new current
echo $TORRENT_LATEST > $FILE_BUNDLE_BOT_I2P_CURRENT
# finally copy the new file to the i2psnark directory
cp $TORRENT_LATEST $PATH_I2PSNARK
if [ $? -eq 0 ]; then
logger -s -t $LOGTAG -p $LOGFACILITY.notice "Successfully copied the .torrent to i2psnark."
if [ "`whoami`" = "root" ]; then
logger -s -t $LOGTAG -p $LOGFACILITY.notice "I am groot. Setting owner/group of file ..."
chown $USER_I2PSNARK:$GROUP_I2PSNARK $PATH_I2PSNARK/`cat $FILE_BUNDLE_BOT_I2P_CURRENT`
fi
else
logger -s -t $LOGTAG -p $LOGFACILITY.error "ERROR: Copying .torrent to i2psnark failed :("
# let's assume this is critical and NOT remove the lock-file
exit 2
fi
else
logger -s -t $LOGTAG -p $LOGFACILITY.notice "FAILED: Download of .torrent unsuccessfull :("
rm $FILE_LOCK
exit 1
fi
logger -s -t $LOGTAG -p $LOGFACILITY.notice "exiting normally after a (technically) successfull run."
rm $FILE_LOCK
exit