summaryrefslogtreecommitdiff
path: root/parabolaiso/mkmetadata
blob: a13f5f1bcf7e330d6a0bce76dbfee70da9074970 (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
67
68
69
70
#!/bin/bash

# mkmetadata - copy package lists from work directory to output directory
#              and create checksums/torrent files


readonly OUT_DIR="$1"    # eg: ./out/x86_64-systemd-cli-2022.04
readonly ISO_FILENAME=$2 # eg: parabola-x86_64-systemd-cli-2022.04-netinstall.iso
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})/${ISO_FILENAME}"
[[ -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})/${ISO_FILENAME}" | sed 's|\s|%20|')
readonly WEBSEEDS_CSV=$(sed -E "s|([^ ]+)|\1/${ISO_HOST_PATH}|g" <<<"${TORRENT_WEBSEED_URLS[@]}" | tr ' ' ',')
readonly RELEASE_SED_RX='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=> ${ISO_FILENAME}.magnet generated"
readonly TORRENT_SUCCESS_MSG="\t=> ${ISO_FILENAME}.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 "${ISO_FILENAME}" >> SHA512SUMS               && echo -e ${SHA512SUMS_SUCCESS_MSG}
  rhash --magnet "${ISO_FILENAME}"  > "${ISO_FILENAME}.magnet" && echo -e ${MAGNET_SUCCESS_MSG}

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

MakeTorrent()
{
  local release=$( sed "${RELEASE_SED_RX}" <<<${ISO_FILENAME})
  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                              \
            "${ISO_FILENAME}"
}

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

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

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