#!/bin/sh -e
# Configuration
dir_build="$PWD/build" # Temporary directory to build packages
dir_source="$PWD/source" # Directory where the package sources will be placed
dir_install="$PWD/install" # Directory where the package will be installed
dir_sysroot="" # The root dir (The directory programs will look in and symlinks will point to)
dir_prefix="/usr" # Prefix directory
cmd_download="curl -# -L -o {dst} {src}" # Command to execute to download files
cmd_extract="tar -x -C {dst} -f {src}" # Command to execute to extract files
unset dir_satellites # Directory where the satellite files are placed.
unset options
[ -f /etc/astronaut.conf ] && . /etc/astronaut.conf
[ -f "$HOME/.astronaut.conf" ] && . "$HOME/.astronaut.conf"
# Options not in the configuration
_download_only=false
_vcs_only=false
vcs_compile=false
#_nuke_dir_install (This can be set from wrappers)
# Some printing functions
_show_help() {
echo "This astronaut will help you build some satellites.
Just describe what he's got to do in a satellite file.
Usage: $0 [-sbipr
] [-Id] [-o ]
-h/? Show this message
-b Set build directory [WARNING: Will be deleted before build]
-s Set source directory
-i Set install directory
-p Set prefix directory
-r Set root directory
-I Nuke install directory before build
-d Only download package files
-v Only build vcs package if any updates have been made to it
-o Set package options (comma-separated)"
}
_msg() {
echo "=> $@"
}
_exiterr() {
echo "===> Houston, we've got a problem: $@" 1>&2
exit 1
}
# Gather info
while getopts "h?s:b:i:p:r:IcCdvo:" opt; do
case "$opt" in
h|\?)
_show_help
exit 0
;;
s)
dir_source="$OPTARG"
;;
b)
dir_build="$OPTARG"
;;
i)
dir_install="$OPTARG"
;;
p)
dir_prefix="$OPTARG"
;;
r)
dir_sysroot="$OPTARG"
;;
I)
_nuke_dir_install=true
;;
d)
_download_only=true
;;
v)
_vcs_only=true
;;
o)
options="$options "$(printf "$OPTARG" | sed -e 's/,/ /g')""
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if [ ! "$1" ]; then
_show_help
exit 1
fi
# Tools for the astronaut
_mksum() {
echo "$(md5sum "$@" 2> /dev/null | cut -d' ' -f1)"
}
download() {
mkdir -p "$dir_source/$name"
local filename="$(basename "$1" | cut -d? -f1)"
local path="$dir_source/$name/$filename"
[ "$2" -a -f "$path" ] && local checksum="$(_mksum "$path")"
if [ ! -e "$path" -o "$checksum" != "$2" ]; then
_msg "Downloading $filename"
[ "$3" ] && local cmd="$3" || local cmd="$cmd_download"
eval $(echo "$cmd" | sed -e "s@{dst}@'$path'@g" -e "s@{src}@'$1'@g")
checksum="$(_mksum "$path")"
if [ "$2" -a "$checksum" != "$2" ]; then
_msg "Checksum: $checksum"
_exiterr "Checksum failed"
fi
fi
}
extract() {
_msg "Extracting $1"
[ "$2" ] && local dest="$2" || local dest="."
[ "$3" ] && local cmd="$3" || local cmd="$cmd_extract"
eval $(echo "$cmd" | sed -e "s@{src}@'$dir_source/$name/$1'@g" -e "s@{dst}@'$dest'@g")
}
getfile() {
cp -r "$dir_source/$name/$1" "$dir_build/$1"
}
extrafile() {
cp "$(dirname "$_satellite")/extrafiles/$_satname/$1" "$dir_build/$1"
}
dlextract() {
download "$1" "$2"
extract "$(basename "$1" | cut -d? -f1)"
}
dlfile() {
download "$1" "$2"
getfile "$(basename "$1" | cut -d? -f1)"
}
option() {
local check=$1
shift
local negate=false
case "$check" in
!*)
local negate=true
local check="$(printf "$check" | cut -c 2-)"
;;
esac
local enabled=false
local option
for option in $options; do
if [ "$option" = "$check" ]; then
local enabled=true
break
fi
done
if [ "$negate" = true ]; then
# Shitty way to do a simple "NOT" operation (come on, it's just one operation on most cpus).
[ "$enabled" = true ] && local enabled=false || local enabled=true
fi
if [ "$1" ]; then
[ "$enabled" = true ] && eval $@ || true
else
return $([ "$enabled" = true ])
fi
}
import() {
. "$(dirname "$_satellite")/functions/$1.sh"
}
# Set some functions in accordance to the _download_only option.
header_end() { :; }
if [ "$_vcs_only" = true ]; then
header_end() {
if [ ! "$vcs_compile" = true ]; then
exit
fi
}
fi
if [ "$_download_only" = true ]; then
# Disable some functions
extract() { :; }
getfile() { :; }
extrafile() { :; }
# Exit at the end of the header
header_end() { exit; }
fi
_satellite="$(realpath "$1")"
_satname="$(basename "$_satellite" .sat)"
# Try to find the satellite file if it can't be found.
if [ ! -f "$_satellite" ]; then
if [ -d "$dir_satellites" ]; then
file="$(find "$dir_satellites" -type f \( -name "$_satname.sat" -o -name "$_satname" \) -print -quit)"
if [ -f "$file" ]; then
_satellite="$(realpath "$file")"
_satname="$(basename "$_satellite" .sat)"
fi
fi
# If it still can't be found, exit.
if [ ! -f "$_satellite" ]; then
_exiterr "Can't find satellite"
fi
fi
# Create the directories
if [ "$_download_only" = false ]; then
# This variable can be set by a wrapper script in need to nuke the install dir.
if [ "$_nuke_dir_install" = true ]; then
rm -rf "$dir_install"
fi
rm -rf "$dir_build"
mkdir -p "$dir_build"
mkdir -p "$dir_install"
cd "$dir_build"
fi
# Create the satellite
. "$_satellite"