#!/bin/bash
# apter
# simplify aptosid package installation and updates
# Copyright (c) 2011 Mithat Konar <m i t h a t _ k o n a r (a t e) y a h o o (d o t s) c o m>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# 2011-12-15
# Requires: gxmessage
### Constants
# UI strings associated with "user mode"
uiENTER_PACKAGES="Enter list of new packages."
uiENTER_PACKAGES_TITLE="Apter"
uiNEXT_STEPS="To install your new packages and/or upgrade existing packages:
(1) Type 'Ctl-Alt-F1'. (This screen will disappear, so note the following!)
(2) Login as root.
(3) Enter 'apter' and follow the prompts."
uiNEXT_STEPS_TITLE="Apter: next steps"
# misc. GUI constants
uiDEFAULT_HEIGHT=0
uiDEFAULT_WIDTH=600
# UI strings associated with "root mode"
# TODO (after debugging and checking with the godz)
# File where list of pending packages will be stored. Should be writable
# by user.
PACKAGE_PATH=/tmp/apter_pending_packages
### Functions
function do_user()
# prompt user to enter package names to be installed and write them to
# PACKAGE_PATH.
{
local package_list
local result
touch $PACKAGE_PATH # create it if it doesn't exist
package_list=`cat $PACKAGE_PATH` # get what's already there
# prompt user for additions
package_list=`gxmessage -entrytext "$package_list" \
-title "$uiENTER_PACKAGES_TITLE" \
-geometry ${uiDEFAULT_WIDTH}x${uiDEFAULT_HEIGHT} \
-buttons GTK_STOCK_CANCEL:1,GTK_STOCK_OK:101 \
-default GTK_STOCK_OK \
"$uiENTER_PACKAGES"`
result=$?
if [[ "x$result" == "x1" ]] ; then
#~ echo "user cancelled" 1>&2
exit 1
fi
echo $package_list > $PACKAGE_PATH # write the new list
# let user know what to do next
gxmessage -title "$uiNEXT_STEPS_TITLE" \
-geometry ${uiDEFAULT_WIDTH}x${uiDEFAULT_HEIGHT} \
-buttons GTK_STOCK_OK:101 \
-default GTK_STOCK_OK \
"$uiNEXT_STEPS"
}
function do_root()
# hold root's hand while killing X, upgrading and installing packages.
{
# TODO: Put the ui strings into Constants after consulting the godz.
local resp
local package_list
# Go to runlevel 3 if runlevel > 3
# TODO: and if we got here via 'C-A-F1'.
rl=(`runlevel`)
rl=${rl[1]}
if [[ $rl > 3 ]] ; then
echo -n "Shutting down X server ... "
init 3
sleep 3
echo
fi
read -p "Update repositories? [Y/n]: " resp
if is_affirmative $resp ; then
apt-get upgrade
echo
fi
read -p "Upgrade existing packages? [Y/n]: " resp
if is_affirmative $resp ; then
apt-get dist-upgrade
echo
fi
read -p "Install new packages? [Y/n]: " resp
if is_affirmative $resp ; then
package_list=`cat $PACKAGE_PATH`
#~ echo "DEBUG: package_list=$package_list"
apt-get install $package_list
echo
fi
echo -n "Cleaning up downloaded packages ... "
apt-get clean
echo "Done."
echo
#~ read -p "Restart X server? [Y/n]: " resp
#~ if is_affirmative $resp ; then
#~ echo "Restarting X server ..."
#~ init 5 && exit
#~ fi
echo "To restart X, enter 'init 5 && exit'"
}
function is_affirmative()
# return 0 if $1 is an affirmative response string; 1 otherwise
{
if [[ "x$1" == "x" || "x$1" == "xy" || "x$1" == "xY" ]] ; then
return 0
fi
return 1
}
### "MAIN"
# if user is not root...
if [[ $EUID -ne 0 ]]; then
do_user
else
do_root
fi