#!/usr/bin/env bash # Either set DIR to the same value as supybot.plugins.PackageInfo.aptdir, # or use the --dir command-line option. DIR="" # Be quiet by default. VERBOSE=0 ## Please don't change anything below this line, unless you really know what ## you are doing and don't bother me with whatever errors it produces :) # Print usage information. usage() { cat < Specify directory for apt-file cache. -v, --verbose Be more verbose than normal. -h, --help Display this message and exit. Note: Please separate each option with a space, eg: $0 -v -d /home/bot/aptdir Rather than: $0 -vd /home/bot/aptdir This script is intended to be run automatically (eg: cron), so it shows no output by default. You can make the script more verbose with the -v/--verbose option. The -d/--dir option sets the directory where this script looks for *.list files for apt-file. EOF } # Prints an error message, usage (above), then exits with the specified exit value. error() { local exit_val=$1 shift echo "$@" >&2 usage >&2 exit $exit_val } # Runs apt-file update in the specified directory for the specified distribution. update_apt_file() { if [ $VERBOSE -eq 0 ]; then apt-file -N -l -c "$DIR/apt-file/$DIST" -s "$DIR/$DIST.list" -a amd64 update >/dev/null 2>&1 else apt-file -N -l -c "$DIR/apt-file/$DIST" -s "$DIR/$DIST.list" -a amd64 update fi } # Check command-line arguments while [ $# -gt 0 ]; do case $1 in -h|--help) usage exit 0 ;; -v|--verbose) VERBOSE=1 ;; -d|--dir) [ -z "$2" ] && error 1 '"-d/--dir" requires an argument.' shift DIR="$1" ;; --dir=*) DIR="${1:6}" [ -z "$DIR" ] && error 1 '"--dir" requires an argument.' ;; -*) error 1 "Unknown option \"$1\"." ;; *) error 1 "This script takes no non-argument parameters." ;; esac shift done # Check if apt-file is installed and bail if not. if ! which apt-file >/dev/null 2>&1; then echo "ERROR: apt-file not found. Please install apt-file in your PATH." >&2 exit 1 fi if [ -z "$DIR" ]; then error 1 "ERROR: No DIR set in the script and no -d/--dir option given." fi items=("$DIR"/*.list) [ ! -e "${items[0]}" ] && error 1 "ERROR: Could not find any *.list files in \"$DIR\"." for DIST in "${items[@]}"; do [ -h "$DIST" ] && continue # Ignore symbolic links # Extract the distribution name from the .list file name. DIST=${DIST##*/} DIST=${DIST%.list} [ $VERBOSE -gt 0 ] && echo "INFO: Processing $DIST" mkdir -p "$DIR/apt-file/$DIST" # Create apt-file directory if it doesn't exist update_apt_file # Update apt-file cache if [ $? -ne 0 ]; then [ $VERBOSE -eq 0 ] && echo "Try passing -v to get the error message." >&2 echo "ERROR: apt-file update failed for $DIST." >&2 exit_val=$? fi done exit $exit_val