blob: 6e75b33a0602cec5c7e09afd366afbec34aa2ae0 (
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 download_src() {
21 local file
22
23 for src in ${source[@]}; do
24 # Set the file path buffer for better error messages
25 # This will store local files verbatim, but remote files will be parsed out
26 # of their URIs
27 file="${src}"
28
29 # Download http files
30 if [ "${src:0:4}" == "http" ] || [ "${src:0:3}" == "ftp" ]; then
31 file="$(basename ${src})"
32 if [ ! -f "${file}" ]; then
33 printf "Downloading %s\n" "${file}"
34 curl -k -L -q -# "${src}" -o "${file}"
35 fi
36 elif [ "${src:0:1}" == "/" ] && [ -f "${src}" ]; then
37 file="$(basename ${src})"
38 if [ ! -f "${file}" ]; then
39 printf "Copying local file '%s'\n" "$(basename ${src})"
40 cp "${src}" "$(basename ${src})"
41 fi
42 elif [ "${src:0:6}" == "ssh://" ]; then
43 file="$(basename ${src})"
44 if [ ! -f "${file}" ]; then
45 printf "Downloading %s\n" "${file}"
46 scp "${src:6}" "${file}"
47 fi
48 fi
49
50 if [ -e "${file}" ]; then
51 cp -r "${file}" "${PKGSRC}/${file}"
52 else
53 printf "Error: Source '%s' does not exist\n" "${file}"
54 return 1
55 fi
56 done
57 }
58
59
60 main() {
61 local pkg="${1:-}"
62 local selfdir # Run directory of builder script
63 local pkgdir # Path to the parent directory of the Pkgfile
64 local tmp # Path to the temp directory containing build resource
65 local output # Path to the output file
66
67 if [ -z "${pkg:-}" ]; then
68 printf "Package file must be specified\n"
69 return 1
70 fi
71
72 # Absolute path to the parent directory of the build script (me!)
73 selfdir="$(cd $(dirname ${0}) && pwd)"
74 # Get the absolute path to the Pkgfile parent directory
75 pkgdir="$(cd $(dirname ${pkg}) && pwd)"
76 # Convert pkg to absolute path
77 pkg="${pkgdir}/$(basename ${pkg})"
78 tmp="$(mktemp -d /tmp/builder-XXXXXX)"
79 export PKGSRC=${tmp}/src
80 export PKG=${tmp}/pkg
81 mkdir -p "${PKGSRC}"
82 mkdir -p "${PKG}"
83
84 # Source and validate the Pkgfile
85 source ${pkg}
86 if [ -z "${name}" ]; then
87 printf "Variable 'name' is required in Pkgfile\n"
88 return 1
89 fi
90 if [ -z "${version}" ]; then
91 printf "Variable 'version' is required in Pkgfile\n"
92 return 1
93 fi
94 if [ -z "${release}" ]; then
95 printf "Variable 'release' is required in Pkgfile\n"
96 return 1
97 fi
98 if [ "$(type -t build)" != 'function' ]; then
99 printf "Function 'build' is required in Pkgfile\n"
100 return 1
101 fi
102
103 # Build the output filename
104 local output="${pkgdir}/${name}#${version}-${release}.sh"
105
106 # Copy the installer header script into the tmp directory, with the right name
107 cp ${selfdir}/install-header.sh "${output}"
108
109 # Copy in libinstall directory to include useful install libraries
110 cp -r ${selfdir}/libinstall ${tmp}/libinstall
111
112 # Copy the run script into the tmp dir
113 install "${pkgdir}/run.sh" "${PKGSRC}/run.sh"
114
115
116 # Download all the source files (if needed)
117 cd "${pkgdir}"
118 for src in ${source[@]}; do
119 download_src "${src}"
120 done
121
122 cd "${PKGSRC}"
123 build
124
125 cd "${tmp}"
126
127 printf "Reticulating splines...\n"
128
129 headsize=$(wc -c < ${output})
130
131 # Package up the libinstall libraries
132 tar -C "${tmp}" -c libinstall | xz -c > ${tmp}/libinstall.tar.xz
133 libinstallsize=$(wc -c < ${tmp}/libinstall.tar.xz)
134
135 # Compress and calculate byte size for run.sh
136 xz -c ${PKGSRC}/run.sh > ${tmp}/run.sh.xz
137 runsize="$(wc -c < ${tmp}/run.sh.xz)"
138
139 # Compress and calculate byte size for payload
140 tar -c "$(basename ${PKG})" | xz -v > ${PKG}.tar.xz
141 payloadsize="$(wc -c < ${PKG}.tar.xz)"
142
143
144 # Print space padded values to meet expected character length.
145 # Expected length: 8 + 8 + 12 + 2 == 30
146 # 1. header: 8 byte digits allows a 95 MB header
147 # 2. run script: 8 byte digits allows a 95 MB run script
148 # 3. payload: 12 byte digits allows for a 931 GB file
149 lens="$(printf '%8s %8s %8s %12s' ${headsize} ${libinstallsize} ${runsize} ${payloadsize})"
150
151 # Interpolate chunk lengths
152 # This space between ( and ) must be 60 chars so as to not change the header
153 # lenth after interpolation.
154 sed -i "s/LENS=(.*)/LENS=(${lens})/g" "${output}"
155
156 # Append chunks to output file
157 cat "${tmp}/libinstall.tar.xz" >> "${output}"
158 cat "${tmp}/run.sh.xz" >> "${output}"
159 cat "${PKG}.tar.xz" >> "${output}"
160
161 # Cleanup
162 rm -rf ${tmp}
163 }
164
165 main ${@}
|