summaryrefslogtreecommitdiff
path: root/parabolaiso/mkmetadata
blob: 01ab721ebd9246cb5539d59996b5d2771354bb70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash

readonly OUT_DIR=$1
readonly IMG_NAME=$2
readonly PKGLIST="$( readlink -f $3 2> /dev/null)"
readonly THIS_DIR="$(readlink -f "$(dirname $0)")"

# if a .torrent file is desired, see ./TORRENT_METADATA.sample
[[ -f "$THIS_DIR/TORRENT_METADATA" ]] && source "$THIS_DIR/TORRENT_METADATA"

which mktorrent &> /dev/null && readonly HAS_MKTORRENT=1 || readonly HAS_MKTORRENT=0
which rhash     &> /dev/null && readonly HAS_RHASH=1     || readonly HAS_RHASH=0
[[ -n "${TORRENT_ANNOUNCE_URL}" ]] && readonly SHOULD_MKTORRENT=${HAS_MKTORRENT} || \
                                      readonly SHOULD_MKTORRENT=0

[[ -n "${ISOS_URL}" ]] && readonly IMG_URL="${ISOS_URL}/$(basename ${OUT_DIR})/${IMG_NAME}"
[[ -n "${ISOS_URL}" ]] && readonly PUBLISH_MSG="The directory: '${OUT_DIR}' should be copied to the repo server entirely, preserving the directory name; such that it is accessible via the URL:\n\t${IMG_URL}/\nThe GPG signature of the ISO should be added to that directory; such that it is accessible at the URL:\n\t${IMG_URL}.sig"

readonly ISO_HOST_PATH=$(echo "$(basename ${OUT_DIR})/${IMG_NAME}" | sed 's|\s|%20|')
readonly WEBSEEDS_CSV=$(echo ${TORRENT_WEBSEED_URLS} | sed -E "s|([^ ]+)|\1${ISO_HOST_PATH}|g" | tr ' ' ',')
readonly RELEASE_DEV_CMD='s|[^-]*-\(.*\)-\(.*\)-\(.*\)-\(.*\)-\(.*\)\.iso|\L\1 \u\2/\U\3 \4 \L\5|'

readonly PKGLIST_SUCCESS_MSG="\t=> copied $(basename ${PKGLIST} 2> /dev/null)"
readonly SHA512SUMS_SUCCESS_MSG="\t=> SHA512SUMS $( [[ -f "${OUT_DIR}/SHA512SUMS" ]] && echo 'appended' || echo 'generated' )"
readonly MAGNET_SUCCESS_MSG="\t=> ${IMG_NAME}.magnet generated"
readonly TORRENT_SUCCESS_MSG="\t=> ${IMG_NAME}.torrent generated"
readonly RHASH_NOT_FOUND_MSG="[mkmetadata]: 'rhash' is not installed - metadata files will not be generated"
readonly MAKE_METADATA_MSG="[mkmetadata]: preparing metadata"


MakeMetadata()
{
  # copy package lists and generate checksums and optional torrent file
  (( ${SHOULD_MKTORRENT} )) && MakeTorrent             && echo -e ${TORRENT_SUCCESS_MSG}
  [[ -f "${PKGLIST}"     ]] && cp ${PKGLIST} .         && echo -e ${PKGLIST_SUCCESS_MSG}
  rhash --sha512 "${IMG_NAME}" >> SHA512SUMS           && echo -e ${SHA512SUMS_SUCCESS_MSG}
  rhash --magnet "${IMG_NAME}"  > "${IMG_NAME}.magnet" && echo -e ${MAGNET_SUCCESS_MSG}

  sort SHA512SUMS --key=2 --output=SHA512SUMS
}

MakeTorrent()
{
  local release=$( sed "${RELEASE_DEV_CMD}" <<<${IMG_NAME})
  local sig="$(    [[ -n "${ISOS_URL}" ]] && echo "GPG signature: '${IMG_URL}.sig")"
  local comment="$(sed 's|_RELEASE_|'"${release}"'|' <<<${TORRENT_COMMENT})\n${sig}"

  # generate torrent file
  mktorrent --announce=${TORRENT_ANNOUNCE_URL}                                 \
            --comment="${comment}"                                             \
            $([[ -n "${WEBSEEDS_CSV}" ]] && echo "--web-seed=${WEBSEEDS_CSV}") \
            --piece-length=20 --no-date --verbose                              \
            "${IMG_NAME}"
}

if   ! (( ${HAS_RHASH} ))
then echo "$RHASH_NOT_FOUND_MSG"
elif [[ -d "${OUT_DIR}" ]] && [[ -f "${OUT_DIR}/${IMG_NAME}" ]]
then echo "$MAKE_METADATA_MSG"

     pushd ${OUT_DIR} > /dev/null
     MakeMetadata
     popd             > /dev/null
fi

[[ -n "${ISOS_URL}" ]] && echo -e "\n${PUBLISH_MSG}\n"