2022-03-09 10:13:45 +00:00
#!/usr/bin/env bash
2023-01-16 17:47:47 +00:00
BOOTSTRAP_DIR = ${ BOOTSTRAP_DIR :- $( pwd ) /bootstrap }
INPUTFILES = $( find " ${ BOOTSTRAP_DIR } " -type f -name 'packages*.tsv' )
OUTPUTFILE = ${ BOOTSTRAP_DIR } /packages_testing.tsv
2022-03-09 10:13:45 +00:00
pkg_all = $( pacman -Qqett | grep -v " $( pacman -Qqg base-devel) " )
pkg_repo = $( pacman -Qqn)
pkg_aur = $( pacman -Qqm)
2023-01-16 17:47:47 +00:00
while getopts "nvhf:" opt; do
2022-04-27 11:00:09 +00:00
case " $opt " in
n) DRYRUN = true ; ;
v) VERBOSE = true ; ;
2023-01-16 17:47:47 +00:00
f) OUTPUTFILE = " $OPTARG " ; ;
2022-04-27 11:00:09 +00:00
h | *)
{
printf "\nUpdate the list of installed packages.\n\nWill compare packages committed to the dotfile repository\nand those currently installed (on an Arch system, using pacman).\nUpdates the list of committed packages in repository\nand prints out the differences as a diff.\n\nOptions:\n\n\t-h\tDisplay this help.\n\t-v\tShow verbose information.\n\t-n\tPrint out changes without changing anything (dry-run).\n"
exit 1
}
; ;
esac
done
2023-01-16 17:47:47 +00:00
# get all existing written packages
if [ -n " $INPUTFILES " ] ; then
INPUT = $( cat $INPUTFILES | grep -v -e '^Name Description Source Target' | sort)
else
INPUT = ""
fi
2022-04-27 11:00:09 +00:00
print_msg( ) {
# shellcheck disable=2059
[ -n " $VERBOSE " ] && printf " $@ "
}
2022-03-09 10:13:45 +00:00
# tsv file:
# packagename, description, source, target
# toot a toot manager A D
2023-01-16 17:47:47 +00:00
if [ -f " ${ OUTPUTFILE } _TEMP " ] ; then
rm " ${ OUTPUTFILE } _TEMP "
2022-03-09 10:13:45 +00:00
fi
2023-01-16 17:47:47 +00:00
touch " ${ OUTPUTFILE } _TEMP "
2022-03-09 10:13:45 +00:00
# create new package list
for pkg in $pkg_all ; do
source = ""
2022-04-27 11:00:09 +00:00
if echo " $pkg_repo " | grep -F -q -x " $pkg " ; then
2022-03-09 10:13:45 +00:00
source = "R"
2022-04-27 11:00:09 +00:00
elif echo " $pkg_aur " | grep -F -q -x " $pkg " ; then
2022-03-09 10:13:45 +00:00
source = "A"
else
echo " ERROR: The package $pkg could not be found in repositories or AUR. "
exit 1
fi
desc = $( pacman -Qs " $pkg " | grep -A1 --color " local/ $pkg \s " | tail -n1)
#remove leading whitespace
desc = " ${ desc # " ${ desc %%[![ : space : ]]* } " } "
target = ""
2023-01-16 17:47:47 +00:00
found_line = $( echo " $INPUT " | grep -e " ^ $pkg " )
2022-03-09 10:13:45 +00:00
if [ -n " $found_line " ] ; then
target = $( echo " $found_line " | cut -f4)
2022-04-27 11:00:09 +00:00
print_msg "Updating pkg: %s:%s from: %s, for: %s\n" " $pkg " " $desc " " $source " " $target "
2022-03-09 10:13:45 +00:00
else
2022-04-27 11:00:09 +00:00
print_msg "Adding pkg: %s:%s from: %s, for: %s\n" " $pkg " " $desc " " $source " " $target "
2022-03-09 10:13:45 +00:00
fi
2023-01-16 17:47:47 +00:00
printf "%s\t%s\t%s\t%s\n" " $pkg " " $desc " " $source " " $target " >>" ${ OUTPUTFILE } _TEMP "
2022-03-09 10:13:45 +00:00
done
# notify on any removed packages
2023-01-16 17:47:47 +00:00
while read -r line; do
if ! echo " $line " | cut -f1 | xargs -I _ grep -F -q -x _ <( echo " $pkg_all " ) ; then
printf "REMOVED: %s\n" " $line "
fi
done <<< " <(echo $INPUT | tail +2) "
2022-04-27 11:00:09 +00:00
# show file changes
2023-01-16 17:47:47 +00:00
if [ -f " $OUTPUTFILE " _TEMP ] ; then
changes = $( diff --color= always -y --suppress-common-lines <( echo " $INPUT " ) <( sort " $OUTPUTFILE " _TEMP | tail -n+2) )
2022-04-27 11:00:09 +00:00
printf "FILE CHANGES:\n=============\n%s" " $changes "
2022-03-09 10:13:45 +00:00
fi
2023-01-16 17:47:47 +00:00
# actually write changes to file
2022-04-27 11:00:09 +00:00
if [ -z " $DRYRUN " ] ; then
2023-01-16 17:47:47 +00:00
while IFS = read -r line; do
sed -i -e " /^ ${ line // \/ / \\ / } $/d " " $OUTPUTFILE " _TEMP
done <<< " $INPUT "
cat <( printf "Name\tDescription\tSource\tTarget\n" ) " ${ OUTPUTFILE } _TEMP " > " $OUTPUTFILE "
2022-04-27 11:00:09 +00:00
fi
2023-01-16 17:47:47 +00:00
rm " ${ OUTPUTFILE } _TEMP "