#!/usr/bin/env bash set -euo pipefail aur_packagedir="aur-maintain" aur_packagelist="$HOME/.config/packages-aur.txt" community_packagedir="svn-community" community_upstream="svn+ssh://svn-community@repos.archlinux.org/srv/repos/svn-community/svn" community_packagelist="$HOME/.config/packages-community.txt" packagedir="$HOME/packages/" create_tmp_packagelist() { local list_file=$(basename "$1") mktemp -p "$XDG_RUNTIME_DIR/" "$list_file.XXXX" } add_aur_package() { local tmp_file=$(create_tmp_packagelist "$aur_packagelist") echo $1 | cat "$aur_packagelist" - |uniq |sort > "$tmp_file" cat "$tmp_file" > "$aur_packagelist" rm "$tmp_file" } add_community_package() { local tmp_file=$(create_tmp_packagelist "$community_packagelist") echo $1 | cat "$community_packagelist" - |uniq |sort > "$tmp_file" cat "$tmp_file" > "$community_packagelist" rm "$tmp_file" } remove_aur_package() { local tmp_file=$(create_tmp_packagelist "$aur_packagelist") cat "$aur_packagelist" |grep -vE "^$1$" |uniq |sort > "$tmp_file" cat "$tmp_file" > "$aur_packagelist" rm "$tmp_file" } remove_community_package() { local tmp_file=$(create_tmp_packagelist "$community_packagelist") cat "$community_packagelist" |grep -vE "^$1$" |uniq |sort > "$tmp_file" cat "$tmp_file" > "$community_packagelist" rm "$tmp_file" } create_aur_dir() { if [ ! -d "$packagedir$aur_packagedir" ];then echo "Creating AUR dir." mkdir -pv "$packagedir$aur_packagedir" fi } create_aur_dir() { if [ ! -d "$packagedir$aur_packagedir" ];then echo "Creating AUR dir." mkdir -pv "$packagedir$aur_packagedir" fi } create_community_dir() { if [ ! -d "$packagedir$community_packagedir" ];then echo "Checking out [community] folder the first time." cd $packagedir svn checkout -N $community_upstream svn-community fi } print_help() { echo "Usage: pkgs -" } clean_aur_packages() { if [ -d "$packagedir$aur_packagedir" ];then cd "$packagedir$aur_packagedir" for package in $(cat "$aur_packagelist"); do if [ -d $package ]; then echo "Cleaning package '$package'." cd $package git clean -f cd .. fi done else echo "There are no AUR packages." fi } clean_community_packages() { if [ -d "$packagedir$community_packagedir" ]; then cd "$packagedir$community_packagedir" for package in $(cat "$community_packagelist"); do if [ -d $package ]; then cd $package svn cleanup --remove-unversioned . cd .. fi done else echo "There are no community packages." fi } update_aur_packages() { create_aur_dir cd "$packagedir$aur_packagedir" for package in $(cat "$aur_packagelist"); do if [ -d $package ]; then echo "Pulling package '$package'." cd $package git pull cd .. else echo "Cloning package '$package' from the AUR for the first time." git clone aur@aur.archlinux.org:$package fi done } update_community_packages() { create_community_dir cd "$packagedir$community_packagedir" for package in $(cat "$community_packagelist"); do svn update $package done } add_mode=0 aur_mode=0 clean_mode=0 community_mode=0 list_mode=0 remove_mode=0 update_mode=0 package="" if [ ${#@} -gt 0 ]; then while getopts ':a:chlur:AC' flag; do case "${flag}" in a) add_mode=1 if [ -n "${OPTARG}" ]; then package="${OPTARG}" else echo "Package name can not be empty." exit 1 fi ;; c) clean_mode=1 ;; h) print_help ;; l) list_mode=1 ;; r) remove_mode=1 if [ -n "${OPTARG}" ]; then package="${OPTARG}" else echo "Package name can not be empty." exit 1 fi ;; u) update_mode=1 ;; A) aur_mode=1 ;; C) community_mode=1 ;; *) echo "Error. Unrecognized option: ${flag}." exit 1 ;; esac done else print_help fi # incompatible modes if [ $add_mode -eq 1 ] && \ [ $remove_mode -eq 1 ]; then echo "Add and remove mode are not complementary." print_help exit 1 fi # add_mode (works with aur_mode and community_mode) if [ $add_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 0 ];then echo "A repository must be defined for adding a package." print_help exit 1 elif [ $add_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 1 ];then echo "Only one repository can be defined for adding a package." print_help exit 1 elif [ $add_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 1 ];then echo "Adding $package to list of community packages." add_community_package $package elif [ $add_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 0 ];then echo "Adding $package to list of AUR packages." add_aur_package $package fi # remove_mode (works with aur_mode and community_mode) if [ $remove_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 0 ];then echo "A repository must be defined for removing a package." print_help exit 1 elif [ $remove_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 1 ];then echo "Only one repository can be defined for removing a package." print_help exit 1 elif [ $remove_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 1 ];then echo "Removing $package from list of community packages." remove_community_package $package elif [ $remove_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 0 ];then echo "Removing $package from list of AUR packages." remove_aur_package $package fi # list_mode (works with aur_mode and community_mode) if [ $list_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 0 ];then echo "Define which repository to list." print_help exit 1 elif [ $list_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 0 ];then cat "$aur_packagelist" elif [ $list_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 1 ];then cat "$community_packagelist" elif [ $list_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 1 ];then echo "All packages:" cat "$aur_packagelist" "$community_packagelist" fi # update_mode (works with aur_mode and community_mode) if [ $update_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 0 ];then echo "Define which repository to update." print_help exit 1 elif [ $update_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 0 ];then echo "Updating AUR packages." update_aur_packages elif [ $update_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 1 ];then echo "Updating community packages." update_community_packages elif [ $update_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 1 ];then echo "Updating all packages:" update_aur_packages update_community_packages fi # clean_mode (works with aur_mode and community_mode) if [ $clean_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 0 ];then echo "Define which repository to clean." print_help exit 1 elif [ $clean_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 0 ];then echo "Cleaning AUR packages." clean_aur_packages elif [ $clean_mode -eq 1 ] && \ [ $aur_mode -eq 0 ] && \ [ $community_mode -eq 1 ];then echo "Cleaning community packages." clean_community_packages elif [ $clean_mode -eq 1 ] && \ [ $aur_mode -eq 1 ] && \ [ $community_mode -eq 1 ];then echo "Cleaning all packages:" clean_aur_packages clean_community_packages fi exit 0