#!/usr/bin/env sh
#
# stowpkg package manager
#
# Copyright (C) 2017 Silvio Rhatto - rhatto at riseup.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
# Parameters
VERSION="0.0.1"
DIRNAME="$(cd `dirname $0` && pwd)"
BASENAME="`basename $0`"
ACTION="$1"
__stowpkg_initialize () {
# Check for stow
if ! which stow > /dev/null 2>&1; then
echo "$BASENAME: please install stow first"
exit 1
fi
# Sudo config
if [ "`whoami`" != 'root' ]; then
SUDO="sudo"
fi
# Source config file
if [ -e "$HOME/.config/stowpkg" ]; then
. "$HOME/.config/stowpkg"
fi
# Ports
if [ -z "$PORTS" ]; then
PORTS="$DIRNAME/ports"
fi
# Set base
if [ -z "$BASE" ]; then
BASE="$DIRNAME/tree/`uname -m`"
fi
# Sources
if [ -z "$SOURCES" ]; then
SOURCES="$BASE/src"
fi
# Binaries
if [ -z "$BINARIES" ]; then
BINARIES="$BASE/stow"
fi
# Ensure folders exists
mkdir -p $BASE $PORTS $SOURCES $BINARIES
# Export
export BASE="$BASE"
export PORTS="$PORTS"
export SOURCES="$SOURCES"
export BASE="$BINARIES"
}
stowpkg_usage () {
echo "$BASENAME $VERSION - a package manager"
echo ""
echo "usage: $BASENAME [options]"
echo ""
echo "available actions:"
echo ""
grep "^stowpkg_" $0 | cut -d ' ' -f 1 | sed -e 's/stowpkg_//' | sort | xargs -L 6 | column -t -c 6 | sed -e 's/^/\t/'
echo ""
exit 1
}
stowpkg_search () {
local package="$2"
if [ ! -z "$package" ]; then
stowpkg_search | grep "$package$"
else
( cd $PORTS && find -name 'desc' | sed -e 's|^\./||' -e 's|/desc||' )
fi
}
stowpkg_install () {
local package="$2"
if [ -z "$package" ]; then
stowpkg_usage
fi
# Find package on ports
local location="$PORTS/`$0 search $package`"
if [ "$location" = "${PORTS}/" ]; then
echo "$BASENAME: package not found: $package"
exit 1
fi
if [ "`basename $location`" != "$package" ]; then
echo "$BASENAME: package not found: $package"
exit 1
fi
# Source environment
if [ -e "$location/env" ]; then
. $location/env
fi
# Set default version
if [ -z "$version" ]; then
version="latest"
fi
# Resolve default dependencies
if [ ! -z "$dependencies_stowpkg" ]; then
for dependency in $dependencies_stowpkg; do
$0 install $dependency
done
fi
# Resolve debian dependencies
if [ -e "/etc/debian_version" ]; then
if [ ! -z "$dependencies_debian" ]; then
$SUDO apt install $dependencies_debian
fi
fi
# Go to sources folder
cd $SOURCES
# Download sources if needed
if [ ! -d "${package}-${version}" ]; then
if [ "$source_format" = "git" ]; then
git clone $url ${package}-${version}
fi
fi
# Go to source folder
if [ ! -d "${package}-${version}" ]; then
echo "$BASENAME: source not found for package $package"
exit 1
fi
cd ${package}-${version}
if [ "$source_format" = "git" ]; then
git fetch --all
git checkout version
fi
# Build
stowpkg_build $package $version
}
function stowpkg_build () {
local package="$1"
local version="$2"
# TODO
# Check for package and version
# Otherwise try to guess it
# Set PREFIX
if [ -z "$PREFIX" ]; then
PREFIX="$BINARIES/${package}-${version}"
fi
# Build
if [ -e "$location/rules" ]; then
. $location/rules
else
# Try default rules
if [ -e "Makefile" ]; then
make clean
fi
if [ -e "configure" ]; then
./configure --prefix=$PREFIX
fi
if [ -e "Makefile" ]; then
make PREFIX=$PREFIX
make PREFIX=$PREFIX install
fi
fi
cd $BINARIES
if [ -d "${package}-${version}" ]; then
stow ${package}-${version} || exit 1
else
echo "$BASENAME: error building $package"
exit 1
fi
}
# Dispatch
if grep -q "^stowpkg_$ACTION () {$" $0; then
__stowpkg_initialize
stowpkg_$ACTION $*
else
stowpkg_usage
fi