+# Push everything in the directory given in $1 to the FTP server, under
+# /FTP_DIR/$2/latest
+ftp_push()
+{
+ log "Mirror $1 on FTP (${FTP_HOST})."
+ local source="${1}"
+ local target="${FTP_DIR}/${2}/latest"
+ local command="mkdir -p ${target}; cd ${target}"
+ command="${command}; mirror --reverse --only-newer --verbose ${source}"
+
+ lftp -e "${command}; exit" ${FTP_HOST}
+}
+
+# Rotate old files on FTP
+# Usage: ftp_rotate group
+ftp_rotate()
+{
+ log "Rotating backups of $1 on FTP (${FTP_HOST})."
+ local lastrun="run-${FTP_KEEP}"
+ local target="${FTP_DIR}/${1}"
+ local commands="mkdir -p ${target}; cd ${target}"
+
+ # Build commands
+ # Remove oldest run
+ if [ "$FTP_KEEP" != "0" ]; then
+ commands="$commands; rmdir ${lastrun}"
+
+ # Move everything back
+ for run in `seq $FTP_KEEP -1 2`; do
+ local newer=$run
+ let "newer -= 1"
+ commands="$commands; mv run-$newer run-$run"
+ done
+ # Move "old latest" to run-1
+ commands="$commands; mv latest run-1"
+ else
+ commands="$commands; rmdir latest"
+ fi
+
+ # Create "new latest" directory
+ commands="$commands; mkdir latest; exit"
+
+ # Run the commands on the FTP server
+ lftp -e "$commands" $FTP_HOST
+}