summaryrefslogtreecommitdiff
path: root/pkgself.sh
blob: 6811dbf6bc65e63c3d85b70ed1226637fa908963 (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 file
   26 
   27   for src in ${source[@]}; do
   28     # Set the file path buffer for better error messages
   29     # This will store local files verbatim, but remote files will be parsed out
   30     # of their URIs
   31     file="${src}"
   32 
   33     # Download http files
   34     if [ "${src:0:4}" == "http" ] || [ "${src:0:3}" == "ftp" ]; then
   35       file="$(basename ${src})"
   36       if [ ! -f "${file}" ]; then
   37         printf "Downloading %s\n" "${file}"
   38         curl -k -L -q -# "${src}" -o "${file}"
   39       fi
   40     elif [ "${src:0:1}" == "/" ] && [ -f "${src}" ]; then
   41       file="$(basename ${src})"
   42       if [ ! -f "${file}" ]; then
   43         printf "Copying local file '%s'\n" "$(basename ${src})"
   44         cp "${src}" "$(basename ${src})"
   45       fi
   46     elif [ "${src:0:6}" == "ssh://" ]; then
   47       file="$(basename ${src})"
   48       if [ ! -f "${file}" ]; then
   49         printf "Downloading %s\n" "${file}"
   50         scp "${src:6}" "${file}"
   51       fi
   52     fi
   53 
   54     if [ -e "${file}" ]; then
   55       cp -r "${file}" "${PKGSRC}/${file}"
   56     else
   57       printf "Error: Source '%s' does not exist\n" "${file}"
   58       return 1
   59     fi
   60   done
   61 }
   62 
   63 
   64 main() {
   65   local pkg="${1:-}"
   66   local selfdir # Run directory of builder script
   67   local pkgdir  # Path to the parent directory of the Pkgfile
   68   local tmp     # Path to the temp directory containing build resource
   69   local output  # Path to the output file
   70 
   71   if [ -z "${pkg:-}" ]; then
   72     printf "Package file must be specified\n"
   73     return 1
   74   fi
   75 
   76   # Absolute path to the parent directory of the build script (me!)
   77   selfdir="$(cd $(dirname ${0}) && pwd)"
   78   # Get the absolute path to the Pkgfile parent directory
   79   pkgdir="$(cd $(dirname ${pkg}) && pwd)"
   80   # Convert pkg to absolute path
   81   pkg="${pkgdir}/$(basename ${pkg})"
   82   tmp="$(mktemp -d /tmp/builder-XXXXXX)"
   83   export PKGSRC=${tmp}/src
   84   export PKG=${tmp}/pkg
   85   mkdir -p "${PKGSRC}"
   86   mkdir -p "${PKG}"
   87   
   88   # Source and validate the Pkgfile
   89   source ${pkg}
   90   if [ -z "${name}" ]; then
   91     printf "Variable 'name' is required in Pkgfile\n"
   92     return 1
   93   fi
   94   if [ -z "${version}" ]; then
   95     printf "Variable 'version' is required in Pkgfile\n"
   96     return 1
   97   fi
   98   if [ -z "${release}" ]; then
   99     printf "Variable 'release' is required in Pkgfile\n"
  100     return 1
  101   fi
  102   if [ "$(type -t build)" != 'function' ]; then
  103     printf "Function 'build' is required in Pkgfile\n"
  104     return 1
  105   fi
  106 
  107   # Build the output filename
  108   local output="${pkgdir}/${name}#${version}-${release}.sh"
  109 
  110   # Copy the installer header script into the tmp directory, with the right name
  111   cp ${selfdir}/install-header.sh "${output}"
  112 
  113   # Copy in libinstall directory to include useful install libraries
  114   cp -r ${selfdir}/libinstall ${tmp}/libinstall
  115 
  116   # Copy the run script into the tmp dir
  117   install "${pkgdir}/run.sh" "${PKGSRC}/run.sh"
  118 
  119 
  120   # Download all the source files (if needed)
  121   cd "${pkgdir}"
  122   for src in ${source[@]}; do
  123     download_src "${src}"
  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 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 "$(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 ${@}

Generated by cgit