Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- CLEANUP_MSG="#Install failed / Cleanup"
- CLEANUP_CPT="##INSTALLATION FAILED"
- #Cleanup on error
- cleanup() {
- echo $CLEANUP_MSG
- pkill -f deluge-state-monitor.sh
- killall deluged deluge-web
- echo "#Purge packages"
- apt-get -y purge deluge-web
- apt-get -y purge deluged
- apt-get -y purge python-libtorrent
- apt-get autoremove
- #Restore sources list if needed
- [ -f /etc/apt/sources.list.saved ] && \
- mv -f /etc/apt/sources.list.saved /etc/apt/sources.list
- #Remove startup links and config
- update-rc.d -f deluge-daemon remove
- rm -rf ~/.config/deluge /etc/init.d/deluge-daemon
- echo $CLEANUP_CPT
- exit 1
- }
- #Check if deluge is installed
- test_inst() {
- inst_allr=0
- dpkg -s python-libtorrent 2>/dev/null|grep "install ok installed">/dev/null
- [ $? -eq 0 ] && echo "##python-libtorrent is installed" && (( inst_allr++ ))
- dpkg -s deluged 2>/dev/null|grep "install ok installed">/dev/null
- [ $? -eq 0 ] && echo "##deluged is installed" && (( inst_allr++ ))
- dpkg -s deluge-web 2>/dev/null|grep "install ok installed">/dev/null
- [ $? -eq 0 ] && echo "##deluge-web is installed" && (( inst_allr++ ))
- return $inst_allr
- }
- #Purge deluge
- case "$1" in
- clean)
- CLEANUP_MSG="#Purging deluge..."
- CLEANUP_CPT="##PURGING COMPLETE"
- cleanup
- exit 0
- esac
- #exit if deluge is already installed
- echo "#Pre-install check"
- test_inst
- [ $? -ne 0 ] && echo "##Failed! Remove above packages first" && exit 1
- echo "#Backup sources list"
- mv /etc/apt/sources.list /etc/apt/sources.list.saved
- [ $? -ne 0 ] && echo "##Failed!" && cleanup
- echo "#Update sources list"
- echo deb http://ftp.us.debian.org/debian/ squeeze main >> /etc/apt/sources.list
- [ $? -ne 0 ] && echo "##Failed!" && cleanup
- echo "#Update packages list"
- apt-get update
- [ $? -ne 0 ] && echo "##Failed!" && cleanup
- echo "#Install libtorrent"
- apt-get --allow-unauthenticated install python-libtorrent
- [ $? -ne 0 ] && echo "##Failed!" && cleanup
- echo "#Update sources list"
- echo deb http://ppa.launchpad.net/deluge-team/ppa/ubuntu jaunty main > /etc/apt/sources.list
- [ $? -ne 0 ] && echo "##Failed!" && cleanup
- if [ "$(dpkg-query -W -f='${Version}' python 2>/dev/null| cut -b -3)" == "2.5" ]; then
- echo deb http://archive.debian.org/debian/ lenny main >> /etc/apt/sources.list
- else #python ver is not 2.5, perhaps 2.6
- echo deb http://ftp.us.debian.org/debian/ squeeze main >> /etc/apt/sources.list
- fi
- [ $? -ne 0 ] && echo "##Failed!" && cleanup
- echo "#Update packages list"
- apt-get update
- [ $? -ne 0 ] && echo "##Failed!" && cleanup
- echo "#Install deluged and deluge-web"
- apt-get --allow-unauthenticated install deluged deluge-web
- [ $? -ne 0 ] && echo "##Failed!" && cleanup
- mv -f /etc/apt/sources.list.saved /etc/apt/sources.list
- [ $? -ne 0 ] && echo "##Failed to restore sources list (ignored)"
- echo "#Post-install check"
- test_inst
- [ $? -ne 3 ] && echo "##Failed!" && cleanup
- #Create autostart script
- rm -f /etc/init.d/deluge-daemon
- echo '#!/bin/sh
- ### BEGIN INIT INFO
- # Provides: deluge-daemon
- # Required-Start: $local_fs $remote_fs
- # Required-Stop: $local_fs $remote_fs
- # Should-Start: $network
- # Should-Stop: $network
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: Daemonized version of deluge and webui.
- # Description: Starts the deluge daemon
- ### END INIT INFO
- # Author: Adolfo R. Brandes
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DESC="Deluge Daemon"
- NAME1="deluged"
- NAME2="deluge"
- DAEMON1=/usr/bin/deluged
- DAEMON1_ARGS="-d" # Consult `man deluged` for more options
- DAEMON2=/usr/bin/deluge-web
- DAEMON2_ARGS="" # Consult `man deluge-web` for more options
- PIDFILE1=/var/run/$NAME1.pid
- PIDFILE2=/var/run/$NAME2.pid
- UMASK=022 # Change this to 0 if running deluged as its own user
- DELUGED_USER=root
- SCRIPTNAME=/etc/init.d/deluge-daemon
- # Exit if the package is not installed
- [ -x "$DAEMON1" -a -x "$DAEMON2" ] || exit 0
- # Load the VERBOSE setting and other rcS variables
- [ -f /etc/default/rcS ] && . /etc/default/rcS
- # Define LSB log_* functions.
- # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
- . /lib/lsb/init-functions
- #
- # Function that starts the daemon/service
- #
- do_start() {
- # Return
- # 0 if daemon has been started
- # 1 if daemon was already running
- # 2 if daemon could not be started
- start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --exec $DAEMON1 \
- --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK --test > /dev/null
- RETVAL1="$?"
- start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --exec $DAEMON2 \
- --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK --test > /dev/null
- RETVAL2="$?"
- [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 1
- start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --make-pidfile --exec $DAEMON1 \
- --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON1_ARGS
- RETVAL1="$?"
- sleep 2
- start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --make-pidfile --exec $DAEMON2 \
- --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON2_ARGS
- RETVAL2="$?"
- [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 2
- sh /$DELUGED_USER/.config/deluge/deluge-state-monitor.sh&
- }
- #
- # Function that stops the daemon/service
- #
- do_stop() {
- # Return
- # 0 if daemon has been stopped
- # 1 if daemon was already stopped
- # 2 if daemon could not be stopped
- # other if a failure occurred
- pkill -f deluge-state-monitor.sh
- start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE2
- RETVAL2="$?"
- start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE1
- RETVAL1="$?"
- [ "$RETVAL1" = "2" -o "$RETVAL2" = "2" ] && return 2
- rm -f $PIDFILE1 $PIDFILE2
- [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] && return 0 || return 1
- }
- case "$1" in
- start)
- [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME1"
- do_start
- case "$?" in
- 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
- 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
- esac
- ;;
- stop)
- [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME1"
- do_stop
- case "$?" in
- 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
- 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
- esac
- ;;
- restart|force-reload)
- log_daemon_msg "Restarting $DESC" "$NAME1"
- do_stop
- case "$?" in
- 0|1)
- do_start
- case "$?" in
- 0) log_end_msg 0 ;;
- 1) log_end_msg 1 ;; # Old process is still running
- *) log_end_msg 1 ;; # Failed to start
- esac
- ;;
- *)
- # Failed to stop
- log_end_msg 1
- ;;
- esac
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
- exit 3
- ;;
- esac
- :' >/etc/init.d/deluge-daemon
- chmod 755 /etc/init.d/deluge-daemon
- update-rc.d deluge-daemon defaults
- echo "#Setting up configuration..."
- deluged
- deluge-web&
- sleep 7 #Wait 7 sec to make sure that programs have started
- killall deluged deluge-web
- [ ! -d ~/.config/deluge ] && echo "##Failed! Config dir not found" && cleanup
- echo "#Set deluged login/password"
- echo "#Note: this setting is for desktop client, not web ui"
- while [ 1 ]; do
- echo -n ">Login: "; read log1
- echo -n ">Confirm: "; read log2
- if [ $log1 -a $log1 = $log2 ]; then break;
- else echo "##Try again!"; fi
- done
- while [ 1 ]; do
- echo -n ">Password: "; read pas1
- echo -n ">Confirm: "; read pas2
- if [ $pas1 -a $pas1 = $pas2 ]; then break;
- else echo "##Try again!"; fi
- done
- echo "$log1:$pas1:10" >> ~/.config/deluge/auth
- #Allow remote connections
- conf_file=~/.config/deluge/core.conf
- if [ -f $conf_file ]; then
- sed -i 's/"allow_remote": false/"allow_remote": true/g' $conf_file
- [ $? -ne 0 ] && echo "##Failed to allow remote access (ignored)"
- sed -i 's/"download_location": "\/root"/"download_location": "\/shares\/Public"/g' $conf_file
- [ $? -ne 0 ] && echo "##Failed to change download location (ignored)"
- else echo "##Failed to allow remote access and change download location (ignored)"; fi
- #Set default connection for web-ui
- conf_file=~/.config/deluge/web.conf
- if [ -f $conf_file ]; then
- sed -i 's/"default_daemon": ""/"default_daemon": "127.0.0.1:58846"/g' $conf_file
- [ $? -ne 0 ] && echo "##Failed to set default daemon for web-ui (ignored)"
- else echo "##Failed to set default daemon for web-ui (ignored)"; fi
- #Move (if possible) state dir to /var/log so HDD can sleep
- echo "#Moving state dir to ram-disk..."
- if [ -d /var/log/deluge-state ]; then
- rm -rf ~/.config/deluge/state
- elif [ -d ~/.config/deluge/state ]; then
- rm -f /var/log/deluge-state #paranoid
- mv -f ~/.config/deluge/state /var/log/deluge-state
- else
- rm -rf ~/.config/deluge/state /var/log/deluge-state
- mkdir /var/log/deluge-state
- echo "##Couldn't move state dir to /var/log! New dir created"
- fi
- ln -s /var/log/deluge-state ~/.config/deluge/state
- #Backup torrent list on changes
- echo "#Generating state-backup monitor..."
- echo 'RAMLCT=/var/log/deluge-state
- HDDLCT=/var/log.hdd/deluge-state
- [ ! -d $RAMLCT ] && exit 1 #deluge-state dir is present
- function do_save() {
- CHKSUM=$(ls $RAMLCT/*.torrent | sum | awk "{print \$1}")
- [ $CHKSUM != $OLDCHKSUM ] && \
- rm -rf $HDDLCT && cp -rf $RAMLCT $HDDLCT
- OLDCHKSUM=$CHKSUM
- }
- trap "do_save; exit 0" SIGTERM #force copy on exit
- OLDCHKSUM=$(ls $RAMLCT/*.torrent | sum | awk "{print \$1}")
- while :; do
- sleep 60& wait $!
- [ -f /tmp/standby ] && continue #skip when sleeping
- do_save
- done' >~/.config/deluge/deluge-state-monitor.sh
- chmod 755 ~/.config/deluge/deluge-state-monitor.sh
- /etc/init.d/deluge-daemon start
- echo "##INSTALLATION COMPLETE"
Advertisement
Add Comment
Please, Sign In to add comment