From 9fed26b594f488570fdf0f067ee0353ed7bb9f47 Mon Sep 17 00:00:00 2001 From: David Runge Date: Mon, 26 Feb 2018 00:07:09 +0100 Subject: bin/pkgs: Updating helper script for AUR and community package handling to be able to add/remove/clean packages according to repositories. --- bin/pkgs | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 307 insertions(+), 33 deletions(-) (limited to 'bin') diff --git a/bin/pkgs b/bin/pkgs index 6db67f9..07bda95 100755 --- a/bin/pkgs +++ b/bin/pkgs @@ -2,40 +2,314 @@ set -euo pipefail -aur_packagedir=aur-maintain -aur_packagelist=~/.config/packages-aur.txt -community_packagedir=svn-community -community_root=svn+ssh://svn-community@repos.archlinux.org/srv/repos/svn-community/svn -community_packagelist=~/.config/packages-community.txt -packagedir=~/packages/ - -if [ ! -d "$packagedir$aur_packagedir" ];then - echo "Creating AUR dir." - mkdir -p "$packagedir$aur_packagedir" -fi +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/" -if [ ! -d "$packagedir$community_packagedir" ];then - echo "Checking out [community] folder the first time." - cd $packagedir - svn checkout -N $community_root svn-community -fi +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" +} -# update community packages -cd $packagedir$community_packagedir -for package in $(cat $community_packagelist); do - svn update $package -done - -# update aur packages -cd $packagedir$aur_packagedir -for package in $(cat $aur_packagelist); do - if [ -d $package ]; then - echo "Pulling package '$package'." - cd $package - git pull - cd .. +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 "Cloning package '$package' from the AUR for the first time." - git clone aur@aur.archlinux.org:$package + echo "There are no AUR packages." fi -done +} + +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 + echo "AUR packages:" + cat "$aur_packagelist" +elif [ $list_mode -eq 1 ] && \ + [ $aur_mode -eq 0 ] && \ + [ $community_mode -eq 1 ];then + echo "Community packages:" + 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." + cat "$aur_packagelist" +elif [ $update_mode -eq 1 ] && \ + [ $aur_mode -eq 0 ] && \ + [ $community_mode -eq 1 ];then + echo "Updating community packages." + cat "$community_packagelist" +elif [ $update_mode -eq 1 ] && \ + [ $aur_mode -eq 1 ] && \ + [ $community_mode -eq 1 ];then + echo "Updating all packages:" + cat "$aur_packagelist" "$community_packagelist" +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 -- cgit v1.2.3-54-g00ecf