Compare commits

...
3 Commits
+121 -37
View File
@@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# Manages dotfiles and packages # Manages dotfiles and packages
# Depends: GNU getopt, pacman-contrib # Depends: GNU getopt, (pacman, pacman-contrib) / (dpkg, apt)
# User-config--------------------------- # User-config---------------------------
@@ -24,7 +24,8 @@ if [ "$(dirname "$0")" != "." ]; then
exit 1 exit 1
fi fi
help() { help()
{
B=$(tput bold) B=$(tput bold)
U=$(tput smul) U=$(tput smul)
N=$(tput sgr0) N=$(tput sgr0)
@@ -70,20 +71,26 @@ EOF
# Exit if no option is provided # Exit if no option is provided
[ "$#" -eq 0 ] && help >&2 && exit 1 [ "$#" -eq 0 ] && help >&2 && exit 1
set_files() { # Files
# --------------------------------------
setFiles()
{
files="$(find . -type f -o -type l \ files="$(find . -type f -o -type l \
| awk -v e="^./($excludeFiles)" '$0 !~ e { print $0 }')" | awk -v e="^./($excludeFiles)" '$0 !~ e { print $0 }')"
} }
list_files() { listFiles()
{
# If unset # If unset
[ -z "${files+x}" ] && set_files [ -z "${files+x}" ] && setFiles
# Remove leading ./ from filepaths # Remove leading ./ from filepaths
echo "$files" | sed 's/^\.\///' echo "$files" | sed 's/^\.\///'
} }
add() { add()
{
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1
file="$(readlink -f "$(dirname "$1")")/$(basename "$1")" file="$(readlink -f "$(dirname "$1")")/$(basename "$1")"
@@ -101,12 +108,13 @@ add() {
fi fi
} }
pull_push() { pullPush()
{
# If unset or empty string # If unset or empty string
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1
# If unset # If unset
[ -z "${files+x}" ] && set_files [ -z "${files+x}" ] && setFiles
match="^./($systemDir)/" match="^./($systemDir)/"
@@ -138,47 +146,123 @@ pull_push() {
done done
} }
pull() { pull()
pull_push "pull" {
pullPush "pull"
} }
push() { push()
pull_push "push" {
pullPush "push"
} }
packages() { # Packages
if ! pacman -Qqs pacman-contrib > /dev/null; then \ # --------------------------------------
echo 'Please install the "pacman-contrib" dependency before running this option.' >&2
osDetect()
{
id="$(sed -nE 's/^ID=(.*)/\1/p' /etc/os-release)"
idLike="$(sed -nE 's/^ID_LIKE=(.*)/\1/p' /etc/os-release)"
if [ "$id" = "arch" ]; then
os="arch"
elif echo "$idLike" | grep -q 'arch'; then
os="arch"
elif [ "$id" = "debian" ] || [ "$id" = "ubuntu" ]; then
os="debian"
elif echo "$idLike" | grep -q 'debian'; then
os="debian"
elif echo "$idLike" | grep -q 'ubuntu'; then
os="debian"
else
echo "Unsupported operating system." >&2
exit 1 exit 1
fi fi
}
osDependencies()
{
if [ "$os" = "arch" ]; then
binaryDependencyPair="
pacman:pacman
pactree:pacman-contrib
"
elif [ "$os" = "debian" ]; then
binaryDependencyPair="
apt-cache:apt
apt-mark:apt
dpkg-query:dpkg
"
fi
for pair in $binaryDependencyPair; do
binary="$(echo "$pair" | cut -d ':' -f 1)"
if ! command -v "$binary" > /dev/null; then
dependency="$(echo "$pair" | cut -d ':' -f 2)"
echo "Please install the '$dependency' dependency before running this option." >&2
exit 1
fi
done
}
packageList()
{
if [ "$os" = "arch" ]; then
filterList="$( (pacman -Qqg base base-devel; pactree -u base | tail -n +2) | sort)" filterList="$( (pacman -Qqg base base-devel; pactree -u base | tail -n +2) | sort)"
packageList="$(pacman -Qqe | grep -vx "$filterList" | sort)" packageList="$(pacman -Qqe | grep -vx "$filterList" | sort)"
elif [ "$os" = "debian" ]; then
installedList="$(dpkg-query --show --showformat='${Package}\t${Priority}\n')"
filterList="$(echo "$installedList" | grep -E 'required|important|standard' | cut -f 1)"
installedList="$(echo "$installedList" | cut -f 1)"
installedManuallyList="$(awk '/Commandline:.* install / && !/APT::/ { print $NF }' /var/log/apt/history.log)"
installedManuallyList="$( (echo "$installedManuallyList"; apt-mark showmanual) | sort -u)"
packageList="$(echo "$installedManuallyList" | grep -x "$installedList" | grep -vx "$filterList")"
fi
}
packageInstall()
{
if [ "$os" = "arch" ]; then
# Grab everything off enabled official repositories that is in the list
repoList="$(pacman -Ssq | grep -xf $packageFile)"
if [ "$1" = "install" ]; then
# Install packages
echo "$repoList" | xargs --open-tty sudo pacman -Sy --needed
fi
if [ "$1" = "install-aur" ]; then
# Determine which packages in the list are from the AUR
aurList="$(grep -vx "$repoList" < $packageFile)"
# Install AUR packages
echo "$aurList" | xargs --open-tty "$aurHelper" -Sy --needed --noconfirm
fi
elif [ "$os" = "debian" ]; then
# Grab everything off enabled official repositories that is in the list
repoList="$(apt-cache search .* | cut -d ' ' -f 1 | grep -xf $packageFile)"
# Install packages
echo "$repoList" | xargs --open-tty sudo apt install
fi
}
packages()
{
# If unset
if [ -z "$os" ]; then
osDetect
osDependencies
packageList
fi
if [ "$1" = "list" ] || [ "$1" = "" ]; then if [ "$1" = "list" ] || [ "$1" = "" ]; then
echo "$packageList" echo "$packageList"
elif [ "$1" = "store" ]; then elif [ "$1" = "store" ]; then
if [ ! -s $packageFile ]; then
touch "$packageFile"
else
truncate -s 0 "$packageFile"
fi
echo "$packageList" > "$packageFile" echo "$packageList" > "$packageFile"
elif [ "$1" = "install" ] || [ "$1" = "install-aur" ]; then elif [ "$1" = "install" ]; then
# Grab everything off enabled official repositories that is in the list packageInstall "install"
coreList="$(pacman -Ssq | grep -xf $packageFile)" elif [ "$1" = "install-aur" ]; then
packageInstall "install-aur"
if [ "$1" = "install" ]; then
# Install core packages, answer no to pacman questions (honor Ignore)
yes n | sudo pacman -Sy --needed $coreList
fi
if [ "$1" = "install-aur" ]; then
# Determine which packages in the list are from the AUR
aurList="$(grep -vx "$coreList" < $packageFile)"
# Install AUR packages
"$aurHelper" -Sy --needed --noconfirm $aurList
fi
fi fi
} }
@@ -186,7 +270,7 @@ packages() {
# -------------------------------------- # --------------------------------------
script="$(basename "$0")" script="$(basename "$0")"
options="$(getopt --options "ha:fp:ls" --longoptions "help,add:,files,packages:,pull,push" -n "$script" -- "$@" 2>&1)" options="$(getopt --options "ha:fp::ls" --longoptions "help,add:,files,packages::,pull,push" -n "$script" -- "$@" 2>&1)"
result="$?" result="$?"
# Exit if invalid option is provided # Exit if invalid option is provided
@@ -205,7 +289,7 @@ while true; do
shift 2 shift 2
;; ;;
-f | --files) -f | --files)
list_files listFiles
shift shift
;; ;;
-h | --help) -h | --help)