1#!/bin/bash
2
3set -e -x
4
5cd "$(dirname "$(readlink -f "$0")")"
6if [[ -f certificate.sh ]]; then
7  source ./certificate.sh
8fi
9
10if [[ -z "$1" ]] || [[ ! -f "$1" ]]; then
11  echo "Error: need path to 7z package as first argument"
12  exit 1
13fi
14
15if [[ ! -f assets/StoreLogo.png ]]; then
16  echo "Error: the assets don't seem to have been built."
17  exit 1
18fi
19
20makeappx="$(find '/c/Program Files (x86)/Windows Kits/10/bin/' -type f -iname makeappx.exe | grep -i /x64/ | sort | tail -n1)"
21makepri="$(find '/c/Program Files (x86)/Windows Kits/10/bin/' -type f -iname makepri.exe | grep -i /x64/ | sort | tail -n1)"
22signtool="$(find '/c/Program Files (x86)/Windows Kits/10/bin/' -type f -iname signtool.exe | grep -i /x64/ | sort | tail -n1)"
23
24if [[ -z "$makeappx" ]]; then
25  echo "Error: makeappx.exe not found"
26  exit 1
27fi
28
29if [[ -z "$makepri" ]]; then
30  echo "Error: makepri.exe not found"
31  exit 1
32fi
33
34if [[ -z "$signtool" ]]; then
35  echo "Error: signtool.exe not found"
36  exit 1
37fi
38
39mtxversion="${1%.7z}"
40mtxversion="${mtxversion##*-}"
41
42while [[ "$(echo $mtxversion | sed -Ee 's/[0-9]*//g')" != ... ]]; do
43  mtxversion=${mtxversion}.0
44done
45
46case "$1" in
47  *64-bit*) mtxarch=x64 ;;
48  *32-bit*) mtxarch=x86 ;;
49  *)
50    echo "Error: could not determine architecture from file name."
51    exit 1
52    ;;
53esac
54
55rm -rf package-${mtxarch}
56mkdir -p package-${mtxarch}
57cd package-${mtxarch}
58
59winpwd="$(echo "$PWD" | sed -Ee 's!^/(.)!\1:!' -e 's!/!\\!g')"
60
617z x "$1"
62rm -f mkvtoolnix/data/portable-app mkvtoolnix/MKVToolNix.url
63
64sed -E \
65    -e "s/Version=\"\"/Version=\"${mtxversion}\"/" \
66    -e "s/ProcessorArchitecture=\"\"/ProcessorArchitecture=\"${mtxarch}\"/" \
67    < ../manifest.xml > manifest.xml
68
69(
70  echo '[Files]'
71  echo "\"$PWD/manifest.xml\" \"AppxManifest.xml\""
72  echo "\"$PWD/resources.pri\" \"Resources.pri\""
73
74  find mkvtoolnix -type f | {
75    while read name ; do
76      echo "\"$PWD/${name}\" \"${name}\""
77    done
78  }
79
80  find ../assets -type f | {
81    while read name ; do
82      echo "\"$PWD/${name}\" \"Assets${name#../assets}\""
83    done
84  }
85
86) | sed -Ee 's!^"/(.)!"\1:!'  -e 's!/!\\!g' > mapping.txt
87
88msix_base="mkvtoolnix-${mtxversion}-${mtxarch}"
89
90MSYS2_ARG_CONV_EXCL=\* "${makepri}" new /pr "${winpwd}" /mn "${winpwd}/manifest.xml" /cf "${winpwd}/../priconfig.xml" /o /of "${winpwd}/resources.pri"
91MSYS2_ARG_CONV_EXCL=\* "${makeappx}" pack /f "${winpwd}/mapping.txt" /p "${winpwd}/${msix_base}.msix" /o
92
93if [[ "$2" == "--sign" ]]; then
94  cp "${msix_base}.msix" "${msix_base}-signed.msix"
95  MSYS2_ARG_CONV_EXCL=\* "${signtool}" sign /f "${CERTIFICATE_PATH}" /p "${CERTIFICATE_PASSWORD}" /fd sha256 "${winpwd}/${msix_base}-signed.msix"
96fi
97