Files
scratchpkg/pkgadd
2018-12-23 00:20:45 +08:00

396 lines
11 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/>.
#
RED='\e[0;31m'
GREEN='\e[0;32m'
YELLOW='\e[0;33m'
CYAN='\e[0;36m'
CRESET='\e[0m'
trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
interrupted() {
echo
ret 1
}
nocolor() {
RED=
GREEN=
YELLOW=
CYAN=
CRESET=
}
msg() {
echo -e "${GREEN}==>${CRESET} $1"
}
msg2() {
echo -e " ${CYAN}*${CRESET} $1"
}
msgerr() {
echo -e "${RED}==> ERROR:${CRESET} $1"
}
msgwarn() {
echo -e "${YELLOW}==> WARNING:${CRESET} $1"
}
runhooks() {
if [ "$UPGRADE_PKG" ]; then
opr=upgrade
else
opr=install
fi
for hook in $(ls $HOOK_DIR/*.hook 2>/dev/null); do
description=$(grep "^# description[[:blank:]]*:" $hook | sed 's/^# description[[:blank:]]*:[[:blank:]]*//')
operation=$(grep "^# operation[[:blank:]]*:" $hook | sed 's/^# operation[[:blank:]]*:[[:blank:]]*//')
target=$(grep "^# target[[:blank:]]*:" $hook | sed 's/^# target[[:blank:]]*:[[:blank:]]*//')
if [ -n "$description" ] && [ -n "$operation" ] && [ -n "$target" ]; then
if [ "$(echo $operation | grep -w "$opr" )" ]; then
if [ "$(grep $target $INDEX_DIR/$name/.files)" ]; then
[ "$SILENT_INSTALL" ] || msg2 "$description"
. $hook
if [ "`type -t exechook`" = "function" ]; then
exechook
fi
fi
fi
fi
unset description operation target
unset -f exechook
done
}
help() {
cat << EOF
Usage:
$(basename $0) package.spkg.txz <options>
Options:
-u, --upgrade upgrade package
-r, --reinstall reinstall package
-c, --ignore-conflict ignore conflict when installing package
-v, --verbose print files installed
-s, --silent print install message in simple format
-h, --help show this help message
--no-preinstall skip preinstall script before build/install package
--no-postinstall skip postinstall script after install package
--no-preupgrade skip preupgrade script before upgrade package
--no-postupgrade skip postupgrade script after upgrade package
--no-backup skip backup when upgrading package
--no-color disable colour for output
--no-hook skip executing hook
--root=<path> install to custom root directory
Example:
$(basename $0) foobar-1.0-1.spkg.txz -uc --no-backup upgrade package foobar-1.0-1 without backup its
old configuration files and skip conflict check
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
-u | --upgrade) UPGRADE_PKG=yes ;;
-r | --reinstall) REINSTALL_PKG=yes ;;
-c | --ignore-conflict) IGNORE_CONFLICT=yes ;;
-v | --verbose) VERBOSE_INSTALL=yes ;;
-s | --silent) SILENT_INSTALL=yes ;;
-h | --help) SHOWHELP=yes ;;
--no-preinstall) NO_PREINSTALL=yes ;;
--no-postinstall) NO_POSTINSTALL=yes ;;
--no-preupgrade) NO_PREUPGRADE=yes ;;
--no-postupgrade) NO_POSTUPGRADE=yes ;;
--no-backup) NO_BACKUP=yes ;;
--no-color) NOCOLOR=yes ;;
--no-hook) NOHOOK=yes ;;
--root=*) ROOT="${1#*=}" ;;
*.spkg.txz) PKGNAME="$1" ;;
*) msg "Invalid option! ($1)"; exit 1 ;;
esac
shift
done
fi
}
ret() {
# remove lock file on exit
rm -fr $LOCK_FILE $TMP_PKGADD
exit $1
}
parse_opts $(extract_opt $@)
INDEX_DIR="$ROOT/var/lib/scratchpkg/index"
PKGADD_DIR="$ROOT/var/lib/scratchpkg"
TMP_PKGADD="$PKGADD_DIR/$(basename $0)-tmp"
LOCK_FILE="$PKGADD_DIR/spkg.lock"
TMP_PKGINSTALL="$TMP_PKGADD/$(basename $0).install"
TMP_PKGINSTALL_BKP="$TMP_PKGADD/$(basename $0).bkp.install"
HOOK_DIR=/etc/hooks
# show help page
if [ "$SHOWHELP" ] || [ -z "$PKGNAME" ]; then
help
ret 0
fi
# disable color for output
if [ "$NOCOLOR" ]; then
nocolor
fi
mkdir -p $PKGADD_DIR
# 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
mkdir -p $INDEX_DIR $TMP_PKGADD
if [ -n "$PKGNAME" ]; then
BASEPKGNAME=$(basename $PKGNAME)
fi
# check existence of package file
if [ ! -f "$PKGNAME" ]; then
msgerr "Package '$1' not exist!"
ret 1
fi
# check for root access
if [ "$UID" != "0" ]; then
msgerr "Installing package need root access!"
ret 1
fi
name=$(echo $BASEPKGNAME | sed 's/.spkg.txz//' | rev | cut -d - -f 3- | rev)
version=$(echo $BASEPKGNAME | sed 's/.spkg.txz//' | rev | cut -d - -f 2 | rev)
release=$(echo $BASEPKGNAME | sed 's/.spkg.txz//' | rev | cut -d - -f 1 | rev)
# get package information if installed
if [ -s $INDEX_DIR/$name/.pkginfo ] && [ -s $INDEX_DIR/$name/.files ]; then
iname=$(cat $INDEX_DIR/$name/.pkginfo | grep ^name | cut -d " " -f3-)
iversion=$(cat $INDEX_DIR/$name/.pkginfo | grep ^version | cut -d " " -f3-)
irelease=$(cat $INDEX_DIR/$name/.pkginfo | grep ^release | cut -d " " -f3-)
ALREADYINSTALLED=yes
fi
if [ "$ALREADYINSTALLED" = "yes" ] && [ ! "$UPGRADE_PKG" ] && [ ! "$REINSTALL_PKG" ]; then
msg "Package '$name' already installed. ($iversion-$irelease)"
ret 0
fi
echo -ne "Loading $BASEPKGNAME...\033[0K\r"
# check integrity of package
tar -tf $PKGNAME > $TMP_PKGADD/files 2>/dev/null
if [ $? != 0 ]; then
msgerr "Package '$1' is corrupted!"
ret 1
fi
echo -ne "\033[0K"
# cant reinstall if not installed and cant upgrade if up-to-date
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
if [ "$ALREADYINSTALLED" != "yes" ]; then
msgerr "Package '$name' not installed."
ret 1
fi
if [ "$UPGRADE_PKG" ]; then
if [ "$version-$release" = "$iversion-$irelease" ]; then
msg "Package '$name' is up-to-date. ($iversion-$irelease)"
ret 0
fi
fi
fi
### INSTALL PACKAGE INTO SYSTEM ###
opr=Installing; oprdone=installed
[ "$UPGRADE_PKG" ] && { opr=Upgrading; oprdone=upgraded; }
[ "$REINSTALL_PKG" ] && { opr=Reinstalling; oprdone=reinstalled; }
[ "$SILENT_INSTALL" ] || msg "$opr '$name-$version-$release'..."
#ignore conflict
if [ ! "$IGNORE_CONFLICT" ]; then
[ "$SILENT_INSTALL" ] || msg2 "Checking file conflict..."
while IFS=' ' read -r line; do
if [ "$line" = "${line%.*}.spkgnew" ]; then
line=${line%.*}
fi
if [ -e "$ROOT/$line" ]; then
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
if [ ! "$(grep -Fx "$line" "$INDEX_DIR/$name/.files")" ]; then
fileconflict+=(${line})
fi
else
fileconflict+=(${line})
fi
fi
done < <(cat $TMP_PKGADD/files | grep -Ev '(.pkginfo|.pkginstall|.pkgreadme)' | grep -v '/$')
if [ "${#fileconflict[@]}" -gt 0 ]; then
[ "$SILENT_INSTALL" ] && echo
msgerr "File conflict found:"
for fc in ${fileconflict[@]}; do
msg2 "$fc"
done
ret 1
fi
fi
if [ $(grep -x .pkginstall $TMP_PKGADD/files) ]; then
source <(tar -xf "$PKGNAME" .pkginstall -O)
fi
# run preinstall script if no --no-preinstall flag and not upgrade package
( cd $ROOT/
if [ ! "$NO_PREINSTALL" ] && [ ! "$UPGRADE_PKG" ]; then
if [ "`type -t pre_install`" = "function" ]; then
[ "$SILENT_INSTALL" ] || msg2 "Running preinstall script..."
pre_install "$version" &>/dev/null
fi
fi
)
# run preupgrade script if package upgrade
( cd $ROOT/
if [ "$UPGRADE_PKG" ] && [ ! "$NO_PREUPGRADE" ]; then
if [ "`type -t pre_upgrade`" = "function" ]; then
[ "$SILENT_INSTALL" ] || msg2 "Running preupgrade script..."
pre_upgrade "$version" "$iversion" &>/dev/null
fi
fi
)
#installing package into system
total=$(cat $TMP_PKGADD/files | grep -Ev '(.pkginfo|.pkginstall|.pkgreadme)' | wc -l)
count=0
[ "$SILENT_INSTALL" ] || msg2 "Extracting package..."
installcmd="tar --keep-directory-symlink -p -x -v -f $PKGNAME -C ${ROOT:-/} --exclude=.pkginfo --exclude=.pkginstall --exclude=.pkgreadme"
rm -f $TMP_PKGINSTALL $TMP_PKGINSTALL_BKP
$installcmd | while IFS=' ' read line; do
count=$(( $count + 1 ))
[ "$SILENT_INSTALL" ] && echo -ne "$opr $name-$version-$release [ $(( 100*$count/$total ))% ]\r"
if [ "$line" = "${line%.*}.spkgnew" ]; then
echo "$line" >> $TMP_PKGINSTALL_BKP
line=${line%.*}
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
if [ ! -e "$ROOT/$line" ] || [ "$NO_BACKUP" = yes ]; then
mv "$ROOT/$line".spkgnew "$ROOT/$line"
fi
else
mv "$ROOT/$line".spkgnew "$ROOT/$line"
fi
fi
echo "$line" >> $TMP_PKGINSTALL
done
# remove old files from old package that not exist in new package
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
[ "$SILENT_INSTALL" ] || msg2 "Removing old files..."
grep -Fxv -f $TMP_PKGINSTALL $INDEX_DIR/$name/.files > $TMP_PKGADD/$name.rmlist
grep -v '/$' $TMP_PKGADD/$name.rmlist | while IFS=' ' read line; do
rm "$ROOT/$line" &>/dev/null
done
grep '/$' $TMP_PKGADD/$name.rmlist | tac | while IFS=' ' read line; do
if [ ! "$(grep -R --exclude-dir="$name" -w "$line" "$INDEX_DIR")" ]; then
rmdir "$ROOT/$line" &>/dev/null
fi
done
fi
# register package into database
rm -fr $INDEX_DIR/$name
mkdir $INDEX_DIR/$name
echo "name = $name" > $INDEX_DIR/$name/.pkginfo
echo "version = $version" >> $INDEX_DIR/$name/.pkginfo
echo "release = $release" >> $INDEX_DIR/$name/.pkginfo
mv $TMP_PKGINSTALL $INDEX_DIR/$name/.files
[ -f $TMP_PKGINSTALL_BKP ] && mv $TMP_PKGINSTALL_BKP $INDEX_DIR/$name/.bkpfiles
tar -x -f $PKGNAME -C $INDEX_DIR/$name .pkginstall .pkgreadme >/dev/null 2>&1
( cd $ROOT/
if [ ! "$NO_POSTINSTALL" ] && [ ! "$UPGRADE_PKG" ]; then
if [ "`type -t post_install`" = "function" ]; then
[ "$SILENT_INSTALL" ] || msg2 "Running postinstall script..."
post_install "$version" &>/dev/null
fi
fi
)
( cd $ROOT/
if [ "$UPGRADE_PKG" ] && [ ! "$NO_POSTUPGRADE" ]; then
if [ "`type -t post_upgrade`" = "function" ]; then
[ "$SILENT_INSTALL" ] || msg2 "Running postupgrade script..."
post_upgrade "$version" "$iversion" &>/dev/null
fi
fi
)
if [ "$ROOT" = "" ]; then
[ "$NOHOOK" = "yes" ] || runhooks
[ $(type -p sysusers) ] && sysusers
fi
if [ "$SILENT_INSTALL" ]; then
echo
else
msg "Package '$name-$version-$release' $oprdone."
fi
# running ldconfig
if [ -x /sbin/ldconfig ]; then
/sbin/ldconfig -r $ROOT/
fi
ret 0