#!/bin/sh
addgroup -g ${GID} torrent && adduser -h /home/torrent -s /bin/sh -G torrent -D -u ${UID} torrent

mkdir -p /data/torrents
mkdir -p /data/.watch
mkdir -p /data/.session
mkdir -p /data/Media/Movies
mkdir -p /data/Media/TV
mkdir -p /data/Media/Animes
mkdir -p /data/Media/Music
mkdir /tmp/fastcgi /tmp/scgi /tmp/client_body

if [ $WEBROOT != "/" ]; then
    sed -i 's|<webroot>|'${WEBROOT}'|g' /sites/rutorrent.conf
    sed -i 's|<folder>||g' /sites/rutorrent.conf
    mv /var/www/torrent /var/www${WEBROOT}
else
    sed -i 's|<webroot>|/|g' /sites/rutorrent.conf
    sed -i 's|<folder>|/torrent|g' /sites/rutorrent.conf
fi

chown -R torrent:torrent /data /var/www /home/torrent /var/lib/nginx /filebot /tmp
rm -f /data/.session/rtorrent.lock

/usr/bin/supervisord -c /etc/supervisord.conf
