blob: cc11ee09f9f24a8c21f45666b9f2db31fd9c3d0d (
plain)
1 #!/usr/bin/env bash
2 #
3 # Portimg uses Crux port-like system for creating software deployment images.
4 # Copyright (C) 2016 Aaron Ball <nullspoon@oper.io>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 source ${LIBDIR}/log.sh # Logging support
21 source ${LIBDIR}/template.sh # Included so each port can use templates
22 source ${LIBDIR}/port.sh # Basic port functions
23
24 export PORTSDIR=${PORTSDIR:-/usr/ports} # Path to port store
25 export PORTTMP='' # Path to the port temp dir
26
27 function pkg_download_src {
28 local _port=${1:-}
29 local _pkgsrc=${2:-}
30 shift && shift
31 local _src=${@}
32
33 # Download from Pkgfile src spec
34 for file in ${_src[@]}; do
35 if [[ -f ${file} ]]; then
36 # If the source file exists locally, just copy from here
37 cp -p ${file} ${_pkgsrc}
38 else
39 if [ -f $(basename ${file}) ]; then
40 linfo "Source '${file}' already downloaded"
41 else
42 linfo "Downloading ${file}"
43 download_src ${file} ${_port}
44 fi
45 linfo "Extracting $(basename ${file}) to ${_pkgsrc}"
46 archive_extract ${_port}/$(basename ${file}) ${_pkgsrc}
47 fi
48 done
49 }
50
51
52 #
53 # TODO: Describe this
54 #
55 function pkgmk {
56 local _port=${1:-}
57 local _ref_porttmp=${2:-}
58
59 local _retval # Return value variable
60 local _tmpdir # Temporary working directory to build the pkg
61 # NOTE: These are "global", so the Pkgfile can access them
62 local PKG # Path to the port pkg dir (binary install dest)
63 local PKGSRC # Path to the port src dir
64
65 # Change context to port dir
66 cd ${PORTSDIR}/${_port}
67
68 # Include the port Pkgfile
69 source Pkgfile
70
71 # Create the port build and install directory structure
72 _tmpdir=$(mktemp -d /tmp/pkgmk-${_port}.XXXX)
73 PKGSRC=${_tmpdir}/src
74 PKG=${_tmpdir}/pkg
75 mkdir -p {${PKGSRC},${PKG}}
76
77 # Download the package source files
78 pkg_download_src "${PORTSDIR}/${_port}" "${PKGSRC}" "${source[@]}"
79
80 cd ${PKGSRC}
81
82 # Call the build process if defined
83 if [[ $(type -t pkgbuild) == 'function' ]]; then
84 linfo "Running ${_port} build process"
85 pkgbuild
86 _retval=$?
87
88 # Set the pkg temp dir (this is our return value)
89 eval "${_ref_porttmp}=${_tmpdir}"
90 return ${_retval}
91 else
92 lerror "Port ${_port} has no pkgbuild function. Aborting."
93 return 1
94 fi
95
96 return 1
97 }
98
99
100 #
101 # Installs the specified port's compiled package to the specified destination.
102 #
103 # @param _port Name of the port (contained in PORTSDIR) to be installed
104 # @param _dest Path to install the port to
105 #
106 function pkgadd {
107 local _port=${1:-}
108 local _dest=${2:-}
109
110 local _pkgname # Pkgfile value for 'name'
111 local _pkgversion # Pkgfile value for 'version'
112 local _pkgrelease # Pkgfile value for 'release'
113 local _pkgbuildname # Pkgfile composite of name, version, and release
114
115 # Ensure Pkgfile exists (we can't assemble the package filename without this)
116 if [ ! -f ${PORTSDIR}/${_port}/Pkgfile ]; then
117 lerror "Could not find Pkgfile for port ${_port}"
118 return 1
119 fi
120
121 # Import the Pkgfile values
122 source ${PORTSDIR}/${_port}/Pkgfile
123 _pkgname=${name} && unset 'name'
124 _pkgversion=${version} && unset 'version'
125 _pkgrelease=${release} && unset 'release'
126 _pkgbuildname="${_pkgname}#${_pkgversion}_${_pkgrelease}"
127
128 # Ensure the built package exists
129 if [ ! -f "${PORTSDIR}/${_port}/${_pkgbuildname}.tar.xz" ]; then
130 lerror "Could not find compiled package for ${_port}"
131 return 1
132 fi
133
134 # Extract the package to the destination directory
135 cd ${_dest}
136 tar -xf ${PORTSDIR}/${_port}/${_pkgbuildname}.tar.xz
137 }
|