mirror of
https://github.com/outbackdingo/scratchpkg.git
synced 2026-02-05 08:28:00 +00:00
198 lines
4.5 KiB
Bash
Executable File
198 lines
4.5 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# scratchpkg
|
|
#
|
|
# Copyright (c) 2018 by Emmett1 (emmett1.2miligrams@gmail.com)
|
|
#
|
|
# 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 3 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, see <https://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
|
|
|
|
export LC_ALL=C
|
|
|
|
interrupted() {
|
|
echo
|
|
ret 1
|
|
}
|
|
|
|
msg() {
|
|
echo -e "==> $1"
|
|
}
|
|
|
|
msg2() {
|
|
echo -e " -> $1"
|
|
}
|
|
|
|
msgerr() {
|
|
echo -e "==> ERROR: $1"
|
|
}
|
|
|
|
msgwarn() {
|
|
echo -e "==> WARNING: $1"
|
|
}
|
|
|
|
help() {
|
|
cat << EOF
|
|
Usage:
|
|
$(basename $0) [ <options> <package name> ]
|
|
|
|
Options:
|
|
-h, --help show this help message
|
|
--no-preremove don't run pre-remove script
|
|
--no-postremove don't run post-remove script
|
|
--root=<path> remove package from custom root directory
|
|
|
|
EOF
|
|
}
|
|
|
|
extract_opt() {
|
|
for opt in $@; do
|
|
case $opt in
|
|
--*) OPTS+=($opt) ;;
|
|
-*) for (( i=1; i<${#opt}; i++ )); do OPTS+=(-${opt:$i:1}); done ;;
|
|
*) OPTS+=($opt) ;;
|
|
esac
|
|
done
|
|
echo ${OPTS[@]}
|
|
}
|
|
|
|
parse_opts() {
|
|
if [ -z "$1" ]; then
|
|
SHOWHELP=yes
|
|
else
|
|
while [ "$1" ]; do
|
|
case $1 in
|
|
-h | --help) SHOWHELP=yes ;;
|
|
-v | --verbose) VERBOSE_REMOVE=yes ;;
|
|
--no-preremove) NO_PREREMOVE=yes ;;
|
|
--no-postremove) NO_POSTREMOVE=yes ;;
|
|
--root=*) ROOT="${1#*=}" ;;
|
|
-*) msg "Invalid option: ($1)"; exit 1 ;;
|
|
*) RMNAME=$1 ;;
|
|
esac
|
|
shift
|
|
done
|
|
fi
|
|
}
|
|
|
|
ret() {
|
|
# remove lock file on exit
|
|
rm -f $LOCK_FILE
|
|
exit $1
|
|
}
|
|
|
|
isinstalled() {
|
|
if [ -s $INDEX_DIR/$1/.pkginfo ] && grep -q $1 $INDEX_DIR/$1/.pkginfo; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
parse_opts $(extract_opt $@)
|
|
|
|
ROOT_DIR=${ROOT:-}
|
|
INDEX_DIR="$ROOT_DIR/var/lib/scratchpkg/index"
|
|
LOCK_FILE="$ROOT_DIR/var/lib/scratchpkg/spkg.lock"
|
|
|
|
# disable pre/post remove script when remove from custom root location
|
|
if [ "$ROOT_DIR" != "" ]; then
|
|
NO_PREREMOVE=yes
|
|
NO_POSTREMOVE=yes
|
|
fi
|
|
|
|
# show help page
|
|
if [ "$SHOWHELP" ] || [ -z "$RMNAME" ]; then
|
|
help
|
|
ret 0
|
|
fi
|
|
|
|
# check for lock file
|
|
if [ -f $LOCK_FILE ]; then
|
|
msgerr "Cant install/remove package simultaneously."
|
|
msgerr "remove '$LOCK_FILE' if no install/remove package process running."
|
|
exit 1
|
|
else
|
|
touch $LOCK_FILE
|
|
if [ "$?" != 0 ]; then
|
|
msgerr "Cant create lock file in '$LOCK_FILE'"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
if ! isinstalled $RMNAME; then
|
|
msgerr "Package '$RMNAME' not installed."
|
|
ret 1
|
|
fi
|
|
|
|
# check for root access
|
|
if [ "$UID" != "0" ]; then
|
|
echo "Installing package need root access!"
|
|
ret 1
|
|
fi
|
|
|
|
name=$(grep ^name $INDEX_DIR/$RMNAME/.pkginfo | cut -d " " -f3-)
|
|
version=$(grep ^version $INDEX_DIR/$RMNAME/.pkginfo | cut -d " " -f3-)
|
|
release=$(grep ^release $INDEX_DIR/$RMNAME/.pkginfo | cut -d " " -f3-)
|
|
|
|
if [ -z $name ] && [ -z $version ] && [ -z $release ]; then
|
|
msgerr "Package '$RMNAME' not installed but exist in database."
|
|
ret 1
|
|
fi
|
|
|
|
echo "remove: $name-$version-$release..."
|
|
|
|
if [ ! "$NO_PREREMOVE" ] && [ -f $INDEX_DIR/$name/.pkginstall ]; then
|
|
(cd "$ROOT_DIR"/ && sh $INDEX_DIR/$name/.pkginstall pre-remove "$version")
|
|
fi
|
|
|
|
if [ -f "$INDEX_DIR/$name/.bkpfiles" ]; then
|
|
while read -r line; do
|
|
if [ -e "$ROOT_DIR/$line" ] || [ -L "$ROOT_DIR/$line" ]; then
|
|
[ "$VERBOSE_REMOVE" = yes ] && echo "- $line"
|
|
rm "$ROOT_DIR/$line"
|
|
fi
|
|
done < <(tac $INDEX_DIR/$name/.bkpfiles)
|
|
fi
|
|
|
|
while read -r line; do
|
|
if [ -e "$ROOT_DIR/$line" ] || [ -L "$ROOT_DIR/$line" ]; then
|
|
[ "$VERBOSE_REMOVE" = yes ] && echo "- $line"
|
|
rm "$ROOT_DIR/$line"
|
|
fi
|
|
done < <(tac $INDEX_DIR/$name/.files | grep -v '/$')
|
|
|
|
while read -r line; do
|
|
if grep -Rqx "$line" "$INDEX_DIR"/*/.files | grep -v "$INDEX_DIR"/$name/.files; then
|
|
if [ -d "$ROOT_DIR/$line" ]; then
|
|
[ "$VERBOSE_REMOVE" = yes ] && echo "- $line"
|
|
rmdir "$ROOT_DIR/$line"
|
|
fi
|
|
fi
|
|
done < <(tac $INDEX_DIR/$name/.files | grep '/$')
|
|
|
|
if [ ! "$NO_POSTREMOVE" ] && [ -f $INDEX_DIR/$name/.pkginstall ]; then
|
|
(cd "$ROOT_DIR"/ && sh $INDEX_DIR/$name/.pkginstall post-remove "$version")
|
|
fi
|
|
|
|
rm -rf $INDEX_DIR/$name
|
|
|
|
# running ldconfig
|
|
if [ -x "$ROOT_DIR"/sbin/ldconfig ]; then
|
|
$ROOT_DIR/sbin/ldconfig -r "$ROOT_DIR"/
|
|
fi
|
|
|
|
ret 0
|