#!/bin/sh
#
# linbo_mktorrent
# thomas@linuxmuster.net
# 20260520
#

usage(){
  RC="$1"
  echo
  echo "Creates torrent file for image."
  echo
  echo "Usage: linbo_mktorrent <imagefile> | [help]"
  echo
  exit "$RC"
}

# print help
[ -z "$1" -o "$1" = "help" ] && usage 0

# get environment
source /usr/share/linbo/shell_functions
echo "### $timestamp $(basename "$0") $@ ###"

isinteger "$PIECELEN" && plopt="-l $PIECELEN"

echo "### linbo_mktorrent $1"

linbo_mountcache &> /dev/null || exit 1

cd /cache

[ -s "$1" ] || usage 1

RC="0"
echo "Creating $1.torrent ..."
rm -f "$1".torrent
buildtorrent --announce http://$LINBOSERVER:6969/announce "$1" "$1.torrent" || RC="1"
# save torrent hash
LANG=C btcheck -ni "$1".torrent | grep ^'Torrent Hash' | awk '{print $4}' > "$1".hash || RC="1"

exit "$RC"
