| 1 | #!/bin/bash |
|---|
| 2 | # Genera los ficheros .torrent de las imágenes almacenadas en el repositorio. |
|---|
| 3 | #Version 0.3 Ejecución desde cron cada minuto, |
|---|
| 4 | ## echo "* * * * * root /opt/opengnsys/bin/torrent-creator" > /etc/cron.d/torrentcreator |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | # Comprobar si el proceso ya está en ejecución. |
|---|
| 8 | PROG=$(basename $0) |
|---|
| 9 | [ "$(pgrep "$PROG")" != "$$" ] && exit |
|---|
| 10 | |
|---|
| 11 | # Variables. |
|---|
| 12 | OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} |
|---|
| 13 | PATH=$PATH:$OPENGNSYS/bin |
|---|
| 14 | OGIMG="$OPENGNSYS/images" |
|---|
| 15 | REPOCFG="$OPENGNSYS/etc/ogAdmRepo.cfg" |
|---|
| 16 | LOGFILE="$OPENGNSYS/log/$PROG.log" |
|---|
| 17 | # Error si no está bien configurado el repositorio de imágenes. |
|---|
| 18 | [ -d $OGIMG -a -f $REPOCFG ] || exit 1 |
|---|
| 19 | source $REPOCFG |
|---|
| 20 | TRACKERURL="http://$IPlocal:6969/announce" |
|---|
| 21 | |
|---|
| 22 | # Directorio de imágenes. |
|---|
| 23 | pushd $OGIMG >/dev/null |
|---|
| 24 | |
|---|
| 25 | # Procesar ficheros de imágenes. |
|---|
| 26 | trap 'echo "`date` : Proceso interrumpido" >> $LOGFILE; exit ' 1 2 3 6 9 15 |
|---|
| 27 | for IMG in *.img; do |
|---|
| 28 | # Saltar al siguiente si no existe el fichero. |
|---|
| 29 | [ -f "$IMG" ] || continue |
|---|
| 30 | # Comprobar si ya existe el fichero Torrent para esa imagen. |
|---|
| 31 | TORRENT="$IMG.torrent" |
|---|
| 32 | if [ -f "$TORRENT" ]; then |
|---|
| 33 | FILESIZE="$(ls -l $IMG | awk '{print $5}')" |
|---|
| 34 | read -e TORRFILE TORRSIZE <<<"$(ctorrent -x $TORRENT | awk '$1~/<1>/ {print $2,$3}')" |
|---|
| 35 | [ "$(basename $IMG)" = "$TORRFILE" -a "[$FILESIZE]" = "$TORRSIZE" ] && continue |
|---|
| 36 | fi |
|---|
| 37 | # Crear fichero Torrent. |
|---|
| 38 | echo "`date` : Inicio creación de fichero $TORRENT" >> $LOGFILE |
|---|
| 39 | rm -f "$TORRENT" |
|---|
| 40 | nice -8 ctorrent -t "$IMG" -u $TRACKERURL -s "$TORRENT" |
|---|
| 41 | if [ -f "$TORRENT" ]; then |
|---|
| 42 | echo "`date` : Fin creación de fichero $TORRENT" >> $LOGFILE |
|---|
| 43 | else |
|---|
| 44 | echo "`date` : ERROR en creación de fichero $TORRENT" >> $LOGFILE |
|---|
| 45 | fi |
|---|
| 46 | done |
|---|
| 47 | |
|---|
| 48 | popd >/dev/null |
|---|
| 49 | |
|---|