#!/bin/sh
# Copyright (c) 2020-2021 Sebastian LaVine <mail@smlavine.com>
# Licensed under the GNU GPLv3. See GPLv3.txt for details.
#
# File: addpackage
# Description: Add the given package to smlss's install lists.
# Options: -a Add packages to the AUR list, not the Arch list.
# Arguments: names of packages to add
usage()
{
echo "Usage: addpackage [-$options] packages..."
}
options="a"
while getopts "$options" o; do
case "$o" in
a) aur=1 ;;
*) usage && exit ;;
esac
done
shift $((OPTIND - 1))
if [ "$#" -eq 0 ]; then
usage
exit
fi
pkgs="$*"
if [ ! "$SMLSS_DIR" ]; then
# Single quotes intended, I don't want the value of $SMLSS_DIR here.
# shellcheck disable=SC2016
echo 'addpackage: error: $SMLSS_DIR does not exist.' >&2
exit 1
fi
if [ "$aur" ]; then
file="$SMLSS_DIR/aur-packages.txt"
else
file="$SMLSS_DIR/packages.txt"
fi
echo "$pkgs" | tr ' ' '\n' | cat "$file" - | sort | sponge "$file" \
&& git -C "$SMLSS_DIR" add "$file"
msg="$(basename "$file"): Add $(echo "$pkgs" | sed -e 's/ /, /g' -e 's/, $//')"
git -C "$SMLSS_DIR" commit -vem "$msg"