#!/bin/sh
BAKDIR=/mmc/; #备份路径
RESUMEDIR=/mmc/root/transmission/resume; #resume文件所在路径
time=3; #停顿时间,如果备份失败,可以尝试加大停顿时间
resume=resume; #备份文件名,不包含扩展名.
#COMMAND="transmission-remote 127.0.0.1:9091 --auth=transmission登录用户:登录密码"
COMMAND="transmission-remote 127.0.0.1:9091";

$COMMAND -t $TR_TORRENT_ID  -S > /dev/null;
sleep $time;
if [ ! -e $BAKDIR/$resume.tar.bz2 ] && [ -e $BAKDIR/AddByResumeBaker ] ; then
	mv -f $BAKDIR/AddByResumeBaker $BAKDIR/AddByResumeBaker.old;
fi
if [ -e $BAKDIR/$resume ] ; then
	mv -f $BAKDIR/$resume $BAKDIR/$resume.old;
fi

if [ ! -e $BAKDIR/AddByResumeBaker ] ; then
	if [ -e $BAKDIR/$resume.tar.bz2 ] ; then
		mv -f $BAKDIR/$resume.tar.bz2 $BAKDIR/$resume.tar.bz2.old
	fi
	cd $RESUMEDIR ;
	/opt/bin/tar -rf $BAKDIR/$resume ./;
	touch $BAKDIR/AddByResumeBaker;
elif [ -e $BAKDIR/$resume.tar.bz2 ] && [ -e $BAKDIR/AddByResumeBaker ] ; then
	RESUMEFILE=$TR_TORRENT_NAME.`echo $TR_TORRENT_HASH|cut -c1-16`.resume;
	cd $BAKDIR;
	/opt/bin/tar -jxf $BAKDIR/$resume.tar.bz2;
	cd $RESUMEDIR ;
	/opt/bin/tar -rf $BAKDIR/$resume ./$RESUMEFILE;
fi

cd $BAKDIR;
/opt/bin/tar -jcf $BAKDIR/$resume.tar.bz2 ./$resume;
$COMMAND -t $TR_TORRENT_ID  -s > /dev/null;
rm -f $BAKDIR/$resume;
exit 0;
