#!/bin/sh # # Copyright (C) 2013 Justin Edward Muniz # # Licensed under the GNU General Public License Version 2 # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Function: List and select from possible PackageKit backends # # There are three arguments that can be provided to this script: # Instruction: mandatory; can be either 'list' or 'set' # Backend Name: mandatory for 'set' instruction; a string that identifies the backend to use # Prefix: optional; if included, must be a valid prefix to the PackageKit installation path # Determine if the prefix being used is an accurate PackageKit installation prefix verifyPrefix() { # Make sure that the necessary configuration file exists given the prefix if [ ! -e "${PK_PREFIX}etc/PackageKit/PackageKit.conf" ]; then # Let the user know that the file was not found echo "Could not find PackageKit.conf, please check your prefix" # Advise user on usage printUsage fi # End verifyPrefix } # Store either a provided or default installation prefix for later operations setPrefix() { # Establish the default prefix PK_PREFIX="/usr/local/" # If there are three arguments, the last argument must be the prefix if [ $# -eq 3 ]; then # Assign the third argument to be the prefix PK_PREFIX="$3" # If there are two arguments and the instruction is "list" the last argument must be the prefix elif [ $# -eq 2 -a "$1" == "list" ]; then # Assign the second argument to be the prefix PK_PREFIX="$2" fi # The prefix must end in a slash (/) in order to be well-formed, test to see if the slash is missing # Evaluate the prefix to determine if it is well-formed case "${PK_PREFIX}" in # If the prefix ends with a slash *"/") # Do nothing, because the prefix appears to be well-formed ;; # For all other cases *) # Concatenate the slash onto the end of the prefix PK_PREFIX="${PK_PREFIX}/" ;; esac # Make sure that the prefix provided is correct verifyPrefix # End setPrefix } # Create a list consisting of backend options, each occupying one line and surrounded by spaces generateBackendList() { # Until this is developed further, hardcode the option set PK_BACKENDS=" ports "$'\n'" pkgng "$'\n'" dummy " # End generateBackendList } # Present the user with a list of available backends listBackends() { # Make sure that the user has requested the backend list if [ "$1" == "list" ]; then # Generate a list of backends, demarcated by newlines generateBackendList # Print a header to describe the following data echo "Available PackageKit backends:" # Print the list of backends echo "${PK_BACKENDS}" fi # End listBackends } # Configure PackageKit to use the chosen backend setBackend() { # Make sure that the user wants to select the backend if [ "$1" == "set" ]; then # Generate the new-line demarcated list of backends generateBackendList # Determine if the backend provided is valid, each option is surrounded by spaces case "${PK_BACKENDS}" in # If the provided backend is valid *" $2 "*) # Ask PackageKit to quietly close as soon as it can gdbus call --system --dest org.freedesktop.PackageKit --object-path /org/freedesktop/PackageKit --method org.freedesktop.PackageKit.SuggestDaemonQuit >/dev/null # Find the first instance of "DefaultBackend" in the configuration file and edit it sed -i .old "s/DefaultBackend=.*/DefaultBackend=$2/" ${PK_PREFIX}etc/PackageKit/PackageKit.conf # Sync the disks to make sure the configuration file is updated sync # Silently start PackageKit pkcon >/dev/null # Inform the user of success echo "PackageKit is now using the $2 backend" ;; # The default case is true if the provided backend name is not on the list of available backends *) # Notify the user of their mistake echo "The backend provided was not found, please check your entry" # Share usage advice with the user printUsage ;; esac fi # End setBackend } # Explain to the user how to use this script, then exit printUsage() { # Print the instructions to the user in an easily readable way echo "pk-setter is used to select the PackageKit backend" echo "usage: pk-setter status [prefix]" echo " pk-setter list [prefix]" echo " pk-setter set [backend] [prefix]" echo "note: prefix is optional and defaults to \"/usr/local/\"," echo " and refers to the installation prefix of PackageKit" # Returns to the shell with error code exit 1 # End printUsage } # Notify the user of the current default back end, then exit showStatus() { if [ "$1" == "status" ]; then # Load the variables from the configuration file; ignore errors, not a well-formed file . ${PK_PREFIX}etc/PackageKit/PackageKit.conf 2>/dev/null # Notify the user of the current default back end by printing the DefaultBackend variable echo "PackageKit is currently using the $DefaultBackend back end" fi # End showStatus } # Beginning of execution # Make sure there is an appropriate number of arguments if [ $# -eq 1 -o $# -eq 2 -o $# -eq 3 ]; then if [ "$1" == "list" -o "$1" == "set" -o "$1" == "status" ]; then # Determine if there is a user defined prefix, save the prefix for later setPrefix $* # List the current default back end if that is the action to do showStatus $* # If the user wants a list of backends then print it listBackends $* # Change the current backend if the user wants to setBackend $* # Return to the shell with success exit 0 fi fi # Let the user know how to use this script printUsage # End of execution