summaryrefslogtreecommitdiff
path: root/pkgself.sh
blob: faba888c6ef55b03da6bab67f6bec42bc424c109 (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   # Copy the run script into the tmp dir
  109   install "${pkgdir}/run.sh" "${PKGSRC}/run.sh"
  110 
  111   # Download all the source files (if needed)
  112   cd "${pkgdir}"
  113   for src in ${source[@]}; do
  114     download_src "${src}"
  115   done
  116 
  117   cd "${PKGSRC}"
  118   build
  119 
  120   cd "${tmp}"
  121 
  122   printf "Reticulating splines...\n"
  123 
  124   headsize=$(wc -c < ${output})
  125 
  126   # Compress and calculate byte size for run.sh
  127   xz -c ${PKGSRC}/run.sh > ${tmp}/run.sh.xz
  128   runsize="$(wc -c < ${tmp}/run.sh.xz)"
  129 
  130   # Compress and calculate byte size for payload
  131   tar -c "$(basename ${PKG})" | xz -v > ${PKG}.tar.xz
  132   payloadsize="$(wc -c < ${PKG}.tar.xz)"
  133 
  134 
  135   # Print space padded values to meet expected character length.
  136   # Expected length: 8 + 8 + 12 + 2 == 30
  137   # 1. header:     8  byte digits allows a 95 MB header
  138   # 2. run script: 8  byte digits allows a 95 MB run script
  139   # 3. payload:    12 byte digits allows for a 931 GB file
  140   lens="$(printf '%8s %8s %12s' ${headsize} ${runsize} ${payloadsize})"
  141 
  142   # Interpolate chunk lengths
  143   # This space between ( and ) must be 60 chars so as to not change the header
  144   # lenth after interpolation.
  145   sed -i "s/LENS=(.*)/LENS=(${lens})/g" "${output}"
  146 
  147   # Append chunks to output file
  148   cat "${tmp}/run.sh.xz" >> "${output}"
  149   cat "${PKG}.tar.xz" >> "${output}"
  150 
  151   # Cleanup
  152   rm -rf ${tmp}
  153 }
  154 
  155 main ${@}

Generated by cgit