#!/usr/bin/env bash # The purpose of this script is to clear our custom pacman repository # i.e. remove old versions of packages # This script should NOT be run as root. # Make sure that AWK rules file is present, readable and correctly set in this script's parameters init() { # Only run if the user is not root if [[ $USER = 'root' ]] ; then echo "You cannot run this script as root!" exit 1 fi source variables.conf repo_db_file=$repo_name.db.tar repo_db_filesfile=$repo_name.files.tar awk_rules_file='parser.awk' if [ ! -e $repo_directory/$repo_db_file ] ; then echo "Could not find db file. Exiting..." exit 1 fi echo "ArchRepo clearing script" } clearRepo() { if [ "$(uname)" = "OpenBSD" ]; then # TODO This could probably be done better (like with actually checking if tar is GNU...) gnutar="gtar" else gnutar="tar" fi stream=$($gnutar xOf $repo_directory/$repo_db_file --wildcards */desc | awk -f $awk_rules_file) OIFS=$IFS IFS='|' stream="${stream:1}" current="" for package in $stream do IFS=' ' count=0 for line in $package; do if [ $count -eq 0 ]; then current="$current $line" else continue fi ((count++)) done IFS='|' done IFS=$OIFS cd $repo_directory reg="^($repo_name.).+" for filename in *; do if [[ $filename =~ $reg || $filename = 'sync' || $filename = 'local' || $filename = 'resources' ]]; then continue fi for package in $current; do if [ $filename = $package ]; then continue 2 fi done echo "Removing $filename..." rm $filename done } run_directory_path=$(pwd) # Main process if [ "$1" = "" ]; then init clearRepo else echo "Please call this script without any arguments." exit 1 fi exit 0