#!/bin/bash
#install aria2 into raspberry pi
#by frogchou.com

sudo apt install aria2
mkdir /etc/aria2
conffile=/etc/aria2/aria2.conf
if [ -e ${conffile} ];then
	rm -f ${conffile}
else
	touch ${conffie}
fi
sessionfile=/etc/aria2/aria.session
if [  -e ${sessionfile} ];then
	rm -f ${sessionfile}
else
	touch ${sessionfile}
fi

echo "## 文件保存相关" >> ${conffile}
echo "# 文件保存目录"  >> ${conffile} 
echo "dir=/mnt/route/download"  >> ${conffile}
echo "# 启用磁盘缓存, 0为禁用缓存, 需1.16以上版本, 默认:16M"  >> ${conffile}
echo "disk-cache=32M"  >> ${conffile}
echo "# 断点续传" >> ${conffile}
echo "continue=true"  >> ${conffile}

echo "# 文件预分配方式, 能有效降低磁盘碎片, 默认:prealloc" >> ${conffile}
echo "# 预分配所需时间: none < falloc ? trunc < prealloc"  >> ${conffile}
echo "# falloc和trunc则需要文件系统和内核支持" >> ${conffile}
echo "# NTFS建议使用falloc, EXT3 4建议trunc, MAC 下需要注释此项 ">> ${conffile}
echo "#file-allocation=falloc" >> ${conffile}

echo "## 下载连接相关" >> ${conffile}

echo "# 最大同时下载任务数, 运行时可修改, 默认:5" >> ${conffile}
echo "#max-concurrent-downloads=5" >> ${conffile} 
echo "# 同一服务器连接数, 添加时可指定, 默认:1" >> ${conffile}
echo "max-connection-per-server=15" >> ${conffile}
echo "# 整体下载速度限制, 运行时可修改, 默认:0（不限制）" >> ${conffile}
echo "#max-overall-download-limit=0" >> ${conffile}
echo "# 单个任务下载速度限制, 默认:0（不限制）" >> ${conffile}
echo "#max-download-limit=0"  >> ${conffile}
echo "# 整体上传速度限制, 运行时可修改, 默认:0（不限制）" >> ${conffile}
echo "#max-overall-upload-limit=0" >> ${conffile}
echo "# 单个任务上传速度限制, 默认:0（不限制）" >> ${conffile}
echo "#max-upload-limit=0" >> ${conffile}
echo "# 禁用IPv6, 默认:false" >> ${conffile}
echo "disable-ipv6=true" >> ${conffile}

echo "# 最小文件分片大小, 添加时可指定, 取值范围1M -1024M, 默认:20M" >> ${conffile}
echo "# 假定size=10M, 文件为20MiB 则使用两个来源下载\; 文件为15MiB 则使用一个来源下载" >> ${conffile}
echo "min-split-size=10M" >> ${conffile}
echo "# 单个任务最大线程数, 添加时可指定, 默认:5" >> ${conffile}
echo "split=10" >> ${conffile}

echo "## 进度保存相关 ##" >> ${conffile}

echo "# 从会话文件中读取下载任务" >> ${conffile}
echo "input-file=/etc/aria2/aria2.session" >> ${conffile}
echo "# 在Aria2退出时保存错误的、未完成的下载任务到会话文件" >> ${conffile}
echo "save-session=/etc/aria2/aria2.session" >> ${conffile}
echo "# 定时保存会话, 0为退出时才保存, 需1.16.1以上版本, 默认:0" >> ${conffile}
echo "save-session-interval=60" >> ${conffile}

echo "## RPC相关设置 ##" >> ${conffile}

echo "# 启用RPC, 默认:false" >> ${conffile}
echo "enable-rpc=true" >> ${conffile}
echo "# 允许所有来源, 默认:false" >> ${conffile}
echo "rpc-allow-origin-all=true" >> ${conffile}
echo "# 允许外部访问, 默认:false" >> ${conffile}
echo "rpc-listen-all=true" >> ${conffile}
echo "# RPC端口, 仅当默认端口被占用时修改" >> ${conffile}
echo "# rpc-listen-port=6800" >> ${conffile}
echo "# 设置的RPC授权令牌, v1.18.4新增功能, 取代 --rpc-user 和 --rpc-passwd 选项" >> ${conffile}
echo "#rpc-secret=TOKEN" >> ${conffile}

echo "## BT/PT下载相关 " >> ${conffile}

echo "# 当下载的是一个种子以.torrent结尾时, 自动开始BT任务, 默认:true" >> ${conffile}
echo "#follow-torrent=true" >> ${conffile}
echo "# 客户端伪装, PT需要" >> ${conffile}
echo "peer-id-prefix=-TR2770-" >> ${conffile}
echo "user-agent=Transmission/2.77" >> ${conffile}
echo "# 强制保存会话, 即使任务已经完成, 默认:false" >> ${conffile}
echo "# 较新的版本开启后会在任务完成后依然保留.aria2文件" >> ${conffile}
echo "#force-save=false" >> ${conffile}
echo "# 继续之前的BT任务时, 无需再次校验, 默认:false" >> ${conffile}
echo "bt-seed-unverified=true" >> ${conffile}
echo "# 保存磁力链接元数据为种子文.torrent文件, 默认:false" >> ${conffile}
echo "bt-save-metadata=true" >> ${conffile}

aria2c --conf-path=${conffile} -D

dfile=/etc/init.d/aria2c
if [ -e ${dfile} ];then
	rm -f ${dfile}
else
	touch ${dfile}
fi

echo "#!/bin/sh" >>${dfile}
echo "### BEGIN INIT INFO" >>${dfile}
echo "# Provides: aria2c" >>${dfile}
echo "# Required-Start:    \$network \$local_fs \$remote_fs" >>${dfile}
echo "# Required-Stop:     \$network \$local_fs \$remote_fs" >>${dfile}
echo "# Default-Start:     2 3 4 5" >>${dfile}
echo "# Default-Stop:      0 1 6" >>${dfile}
echo "# Short-Description: aria2c RPC init script." >>${dfile}
echo "# Description: Starts and stops aria2 RPC services." >>${dfile}
echo "### END INIT INFO" >>${dfile}

echo "USER=root" >>${dfile}
echo "RETVAL=0" >>${dfile}

echo "case "\$1" in  " >>${dfile}
echo "    start)  " >>${dfile}
echo "        echo \"Starting service Aria2...\"" >>${dfile}
echo "        aria2c --conf-path=/etc/aria2/aria2.conf -D  " >>${dfile}
echo "        echo \"Start service done.\"  " >>${dfile}
echo "    ;;  " >>${dfile}
echo "    stop)  " >>${dfile}
echo "        echo \"Stoping service Aria2...\"  " >>${dfile}
echo "        killall aria2c   " >>${dfile}
echo "        echo \"Stop service done.\"  " >>${dfile}
echo "    ;;  " >>${dfile}
echo "esac  " >>${dfile}

echo "exit \$RETVAL" >>${dfile}
chmod +x /etc/init.d/aria2c
apt-get -y install chkconfig wget
chkconfig --add aria2c
apt-get -y install nginx
rm -rf /var/www/html
mkdir -p /var/www/html
wget http://vpn.frogchou.com/linux/software/aria-ng.tar.gz -O /var/www/html/aria-ng.tar.gz
tar -zxvf /var/www/html/aria-ng.tar.gz -C /var/www/html/
service nginx start

send -i ‘$a/etc/init.d/nginx start/g’ /etc/rc.local

echo #########################################################################
echo  安装完成
echo  内网访问地址：http://树莓派IP/aria-ng
echo  注意：
echo  如果出现下载慢或者没有下载进度，建议下载frogchou的updatebt-tracker.sh脚本
echo  外网访问注意在路由器上做NAT映射，个人用户注意80端口运营商一般是封锁的。
echo  不要把外网访问的端口设置成80
echo  寻求帮助访问：frogchou.com
echo ##########################################################################
