blob: c47a7131fd415c44a7957392fa1a414338bd84f3 (
plain)
1 #!/usr/bin/env bash
2 # Pkgself builds self-extracting installers
3 # Copyright (C) 2018 Aaron Ball <nullspoon@oper.io>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18 set -e
19
20 export COMPRESS='xz -z -c -T 0' # Command to compress data
21 export DECOMPRESS='xz -d -c' # Command to decompress data
22 export COMPRESSEXT='xz' # File extension for compressed data
23
24 download_src() {
25 local src="${1:-}"
26 local file
27
28 [ ! -d '.source' ] && mkdir '.source'
29
30 # Set the file path buffer for better error messages
31 # This will store local files verbatim, but remote files will be parsed out
32 # of their URIs
33 # If we don't store this value, we would need to recompute src basename a
34 # great many times.
35 file="$(basename ${src})"
36
37 # Download external files
38 if [ ! -f ".source/$(basename ${src})" ]; then
39 if [ "${src:0:4}" == "http" ] || [ "${src:0:3}" == "ftp" ]; then
40 printf "Downloading %s\n" "${file}"
41 curl -k -L -q -# "${src}" -o ".source/${file}"
42 elif [ "${src:0:6}" == "ssh://" ]; then
43 printf "Downloading %s\n" "${file}"
44 scp -o LogLevel=Error "${src:6}" ".source/${file}"
45 fi
46 else
47 printf "Source file '%s' already downloaded.\n" "${file}"
48 fi
49
50 if [ -e ".source/${file}" ]; then
51 cp -r ".source/${file}" "${PKGSRC}/${file}"
52 elif [ -e "${src}" ]; then
53 cp -r "${src}" "${PKGSRC}/${file}"
54 else
55 printf "Error: Source '%s' does not exist\n" "${file}"
56 return 1
57 fi
58 }
59
60
61 main() {
62 local pkg="${1:-}"
63 local selfdir # Run directory of builder script
64 local pkgdir # Path to the parent directory of the Pkgfile
65 local tmp # Path to the temp directory containing build resource
66 local output # Path to the output file
67
68 if [ -z "${pkg:-}" ]; then
69 printf "Package file must be specified\n"
70 return 1
71 fi
72
73 # Absolute path to the parent directory of the build script (me!)
74 selfdir="$(cd $(dirname ${0}) && pwd)"
75 # Get the absolute path to the Pkgfile parent directory
76 pkgdir="$(cd $(dirname ${pkg}) && pwd)"
77 # Convert pkg to absolute path
78 pkg="${pkgdir}/$(basename ${pkg})"
79 tmp="$(mktemp -d /tmp/builder-XXXXXX)"
80 export PKGSRC=${tmp}/src
81 export PKG=${tmp}/pkg
82 mkdir -p "${PKGSRC}"
83 mkdir -p "${PKG}"
84
85 # Source and validate the Pkgfile
86 source ${pkg}
87 if [ -z "${name}" ]; then
88 printf "Variable 'name' is required in Pkgfile\n"
89 return 1
90 fi
91 if [ -z "${version}" ]; then
92 printf "Variable 'version' is required in Pkgfile\n"
93 return 1
94 fi
95 if [ -z "${release}" ]; then
96 printf "Variable 'release' is required in Pkgfile\n"
97 return 1
98 fi
99 if [ "$(type -t build)" != 'function' ]; then
100 printf "Function 'build' is required in Pkgfile\n"
101 return 1
102 fi
103
104 # Build the output filename
105 local output="${pkgdir}/${name}#${version}-${release}.sh"
106
107 # Copy the installer header script into the tmp directory, with the right name
108 cp ${selfdir}/install-header.sh "${output}"
109
110 # Copy in libinstall directory to include useful install libraries
111 cp -r ${selfdir}/libinstall ${tmp}/libinstall
112
113 # Copy the run script into the tmp dir
114 install "${pkgdir}/run.sh" "${PKGSRC}/run.sh"
115
116
117 # Download all the source files (if needed)
118 cd "${pkgdir}"
119 for src in ${source[@]}; do
120 download_src "${src}"
121 if [ $? -gt 0 ]; then
122 return 1
123 fi
124 done
125
126 cd "${PKGSRC}"
127 build
128
129 cd "${tmp}"
130
131 printf "Reticulating splines...\n"
132
133 # Package up the libinstall libraries
134 tar -C "${tmp}" -c --mtime='1970-01-01' libinstall \
135 | ${COMPRESS} > ${tmp}/libinstall.tar.${COMPRESSEXT}
136 libinstallsize=$(wc -c < ${tmp}/libinstall.tar.${COMPRESSEXT})
137
138 # Compress and calculate byte size for run.sh
139 ${COMPRESS} ${PKGSRC}/run.sh > ${tmp}/run.sh.${COMPRESSEXT}
140 runsize="$(wc -c < ${tmp}/run.sh.${COMPRESSEXT})"
141
142 # Compress and calculate byte size for payload
143 tar -c --mtime='1970-01-01' "$(basename ${PKG})" \
144 | ${COMPRESS} -v > ${PKG}.tar.${COMPRESSEXT}
145 payloadsize="$(wc -c < ${PKG}.tar.${COMPRESSEXT})"
146
147 # Replace compress and decompress command variables
148 sed -i "s/{{ DECOMPRESS }}/${DECOMPRESS}/g" "${output}"
149
150 # NOTE: All variable interpolations in the header MUST occur before this line.
151 # This solely exclides the 'lens' array interpolation
152 headsize=$(wc -c < ${output})
153
154 # Print space padded values to meet expected character length.
155 # Expected length: 8 + 8 + 12 + 2 == 30
156 # 1. header: 8 byte digits allows a 95 MB header
157 # 2. run script: 8 byte digits allows a 95 MB run script
158 # 3. payload: 12 byte digits allows for a 931 GB file
159 lens="$(printf '%8s %8s %8s %12s' ${headsize} ${libinstallsize} ${runsize} ${payloadsize})"
160
161 # Interpolate chunk lengths
162 # This space between ( and ) must be 60 chars so as to not change the header
163 # lenth after interpolation.
164 sed -i "s/LENS=(.*)/LENS=(${lens})/g" "${output}"
165
166 # Append chunks to output file
167 cat "${tmp}/libinstall.tar.${COMPRESSEXT}" >> "${output}"
168 cat "${tmp}/run.sh.${COMPRESSEXT}" >> "${output}"
169 cat "${PKG}.tar.${COMPRESSEXT}" >> "${output}"
170
171 # Cleanup
172 rm -rf ${tmp}
173 }
174
175 main ${@}
|