diff --git a/scripts/archive.sh b/scripts/archive.sh new file mode 100644 index 0000000..852d38d --- /dev/null +++ b/scripts/archive.sh @@ -0,0 +1,161 @@ +#!/usr/bin/env bash +# -*- coding: utf-8 -*- + +set -euo pipefail + +# Version 2024/01/28 - Changelog: https://gist.github.com/AriRexouium/0712ffed7cb066c4ddf5c221722d9337 +# This script provides a wrapper to compress and extract files/directories using various algorithms. +# Supported algorithms include tar, bzip2, lzma, lzip, lzop, gzip, zstd, 7z, rar, and par2. +# +# Usage: ./archive.sh +# Commands: compress, extract +# Algorithms: tar, bzip2, lzma, lzip, lzop, gzip, zstd, 7z, rar, par2 +# +# Examples: +# ./archive.sh compress xz my_dir +# ./archive.sh extract 7z my_file.tar.7z +# +# Exit codes include: +# 0 successful +# 1 missing args +# 2 unknown command +# 3 unknown archive format +# 4 file doesn't exist +# 5 software not installed +# +# License: +# This script is licensed under the GNU General Public License v3.0 or later. +# For more information, see https://www.gnu.org/licenses/gpl-3.0.html +# Copyright (C) 2023 Jarrett Aiken (https://achl.fr) + +# Colors +ES="\x1B" +GREEN="$ES[0;32m" +RED="$ES[0;31m" +YELLOW="$ES[0;33m" +NC="$ES[0m" + +# find echo that doesn't have -e +# ^(?!.*echo\s+.*-e).*echo\s.* + +case "${#}" in + 0) echo -e "${YELLOW}Please provide a command, algorithm, and a filename or directory.${NC}" && exit 1 ;; + 1) echo -e "${YELLOW}Please provide an algorithm and a filename or directory.${NC}" && exit 1 ;; + 2) echo -e "${YELLOW}Please provide a filename or directory.${NC}" && exit 1 ;; +esac + +if [ "${#}" -eq 3 ] && [ ! -e "${3}" ]; then + echo -e "${RED}Filename or directory does not exist.${NC}" && exit 4 +fi + +doesCommandExist() { + if ! command -v "${1}" > /dev/null 2>&1; then + echo -e "${RED} ${1} is not installed." && exit 5 + fi +} + +if [ "${#}" -eq 3 ] ; then + doesCommandExist tar +fi + +case "${1}" in + compress | create | add | new | c | a) + case "${2}" in + tar ) + tar -cf "${3}.tar" "${3}" + echo -e "${GREEN}Archived with tar, created ${3}.tar.${NC}" ;; + bzip2 | j | bz2 | tar.bz2 | tbz2 ) + doesCommandExist bzip2 + tar -cjf "${3}.tar.bz2" "${3}" + echo -e "${GREEN}Compressed with bzip2, created ${3}.tar.bz2.${NC}" ;; + xz | lzma | J | tar.xz | txz ) + doesCommandExist lzma + tar -cJf "${3}.tar.xz" "${3}" + echo -e "${GREEN}Compressed with lzma, created ${3}.tar.xz.${NC}" ;; + lzip | lz | tar.lz | tlz ) + doesCommandExist lzip + tar -cf "${3}.tar.lz" --lzip "${3}" + echo -e "${GREEN}Compressed with lzip, created ${3}.tar.lz.${NC}" ;; + lzop | lzo | tar.lzo | tzo ) + doesCommandExist lzop + tar -cf "${3}.tar.lzo" --lzop "${3}" + echo -e "${GREEN}Compressed with lzop, created ${3}.tar.lzo.${NC}" ;; + gzip | z | gz | tar.gz | tgz ) + doesCommandExist gzip + tar -czf "${3}.tar.gz" "${3}" + echo -e "${GREEN}Compressed with gunzip, created ${3}.tar.gz.${NC}" ;; + compress | Z | tar.Z | tZ ) + doesCommandExist compress + tar -cZf "${3}.tar.Z" "${3}" + echo -e "${GREEN}Compressed with compress, created ${3}.tar.Z.${NC}" ;; + zstd | zst | tar.zst | tzst ) + doesCommandExist zstd + tar -cf "${3}.tar.zst" --zstd "${3}" + echo -e "${GREEN}Compressed with zstd, created ${3}.tar.zst.${NC}" ;; + 7z | tar.7z | t7z ) + doesCommandExist 7z + tar -c "${3}" | 7z a -si "${3}.tar.7z" + echo -e "${GREEN}Compressed with 7-Zip, created ${3}.tar.7z.${NC}" ;; + rar | tar.rar | trar ) + doesCommandExist rar + tar -c "${3}" | rar a -si"${3}.tar.rar" "${3}.tar.rar" + echo -e "${GREEN}Compressed with Rar, created ${3}.tar.rar.${NC}" ;; + par2 | parchive ) + doesCommandExist par2 + redundancy=32 + par2 c -r"${redundancy}" -n1 "${3}" + echo -e "${GREEN}Archived with Parchive (${redundancy}% redundancy), created ${3}.par2 (and others).${NC}" ;; + * ) + echo -e "${RED}Unknown archive format: ${2}${NC}" && exit 3 ;; + esac ;; + decompress | extract | e | x) + case "${2}" in + tar ) + tar -xf "${3}" + echo -e "${GREEN}Extracted with tar.${NC}" ;; + bzip2 | j | bz2 | tar.bz2 | tbz2 ) + doesCommandExist bzip2 + tar -xjf "${3}" + echo -e "${GREEN}Extracted with bzip2.${NC}" ;; + xz | lzma | J | tar.xz | txz ) + doesCommandExist lzma + tar -xJf "${3}" + echo -e "${GREEN}Extracted with lzma.${NC}" ;; + lzip | lz | tar.lz | tlz ) + doesCommandExist lzip + tar -xf "${3}" --lzip + echo -e "${GREEN}Extracted with lzip.${NC}" ;; + lzop | lzo | tar.lzo | tzo ) + doesCommandExist lzop + tar -xf "${3}" --lzop + echo -e "${GREEN}Extracted with lzop.${NC}" ;; + gzip | z | gz | tar.gz | tgz ) + doesCommandExist gzip + tar -xzf "${3}" + echo -e "${GREEN}Extracted with gunzip.${NC}" ;; + compress | Z | tar.Z | tZ ) + doesCommandExist compress + tar -xZf "${3}" + echo -e "${GREEN}Extracted with compress.${NC}" ;; + zstd | zst | tar.zst | tzst ) + doesCommandExist zstd + tar -xf "${3}" --zstd + echo -e "${GREEN}Extracted with zstd.${NC}" ;; + 7z | tar.7z | t7z ) + doesCommandExist 7z + 7z x -so "${3}" | tar -x + echo -e "${GREEN}Extracted with 7-Zip.${NC}" ;; + rar | tar.rar | trar ) + doesCommandExist rar + rar p "${3}" | tar -x + echo -e "${GREEN}Extracted with Rar.${NC}" ;; + par2 | parchive ) + doesCommandExist par2 + par2 r "${3}" + echo -e "${GREEN}Repaired with Parchive.${NC}" ;; + * ) + echo -e "${RED}Unknown archive format: ${2}${NC}" && exit 3 ;; + esac ;; + * ) + echo -e "${RED}Unknown command: ${1}${NC}" && exit 2 ;; +esac diff --git a/scripts/archivegithub.sh b/scripts/archivegithub.sh new file mode 100644 index 0000000..fca6cb2 --- /dev/null +++ b/scripts/archivegithub.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env bash +# -*- coding: utf-8 -*- + +set -euo pipefail + +# Version 2024/01/28 - Changelog: https://gist.github.com/AriRexouium/9cf8cc6e3ea66d4c67de38c6500391e4 +# This script uses the GitHub API to retrieve the logged in user's repos and gists, downloads them, then archives them. +# Create a classic PAT with the 'repo', 'gist' & 'admin:public_key' scopes and update TOKEN for this script to work. +# https://github.com/settings/tokens +# repo: To clone private repositories. +# gist: To clone private gists. +# admin:public_key: To create and delete the ssh key used for cloning. +# +# Usage: ./archivegithub.sh +# +# License: +# This script is licensed under the GNU General Public License v3.0 or later. +# For more information, see https://www.gnu.org/licenses/gpl-3.0.html +# Copyright (C) 2023 Jarrett Aiken (https://achl.fr) + +TOKEN="" + +# Colors +ES="\x1B" +GREEN="$ES[0;32m" +YELLOW="$ES[0;33m" +BLUE="$ES[0;34m" +NC="$ES[0m" + +echo -e "${YELLOW}Connecting to GitHub...${NC}" +QUERY="{viewer{login repositories(first:100,ownerAffiliations:OWNER){nodes{name}}gists(first:100,privacy:ALL){nodes{name}}}}" +REQUEST=$(curl \ + -s \ + -X POST \ + -H "Authorization: Bearer ${TOKEN}" \ + -d "{\"query\":\"${QUERY}\"}" \ + "https://api.github.com/graphql" +) + +LOGIN="$(echo "${REQUEST}" | jq -r ".data.viewer.login")" +REPOS=$(echo "${REQUEST}" | jq -r ".data.viewer.repositories.nodes[].name") +GISTS=$(echo "${REQUEST}" | jq -r ".data.viewer.gists.nodes[].name") +echo -e "${GREEN}Logged in as ${LOGIN} with $(echo "${REPOS}" | wc -l) repos and $(echo "${GISTS}" | wc -l) gists.${NC}\n" + +mkdir -p "${PWD}/${LOGIN}/"{repos,gists} +_PWD="${PWD}/${LOGIN}" + +echo -e "${BLUE}Generating SSH key...${NC}" +SSHK="${_PWD}/id_ed25519" +ssh-keygen \ + -q \ + -t ed25519 \ + -f "${SSHK}" \ + -N "" +chmod 600 "${SSHK}" + +echo -e "${YELLOW}Adding SSH key to account...${NC}\n" +KEYID=$(curl \ + -s \ + -X POST \ + -H "Authorization: Bearer ${TOKEN}" \ + -d "{\"key\":\"$(cat "${SSHK}.pub")\"}" \ + "https://api.github.com/user/keys" \ + | jq -r ".id" +) + +cd "${_PWD}/repos" +echo -e "${GREEN}Cloning repos...${NC}\n" +for REPO in ${REPOS}; do + echo -e "${BLUE}Cloning ${REPO}...${NC}" + git clone \ + -q \ + -c core.sshCommand="ssh -i ${SSHK}" \ + "git@github.com:${LOGIN}/${REPO}.git" \ + && sleep 1 # so 'tar' doesn't occationally step on the toes of 'git clone'. + echo -e "${BLUE}Archiving ${REPO}...${NC}" + tar -cJf "${REPO}.tar.xz" "${REPO}" + echo -e "${YELLOW}Cleaning up ${REPO}...${NC}" + rm -rf "${REPO}" + echo +done + +cd "${_PWD}/gists" +echo -e "${GREEN}Cloning gists...${NC}\n" +for GIST in ${GISTS}; do + echo -e "${BLUE}Cloning ${GIST}...${NC}" + git clone \ + -q \ + -c core.sshCommand="ssh -i ${SSHK}" \ + "git@gist.github.com:${GIST}.git" \ + && sleep 1 # so 'tar' doesn't occationally step on the toes of 'git clone'. + echo -e "${BLUE}Archiving ${GIST}...${NC}" + tar -cJf "${GIST}.tar.xz" "${GIST}" + echo -e "${YELLOW}Cleaning up ${GIST}...${NC}" + rm -rf "${GIST}" + echo +done + +echo -e "${YELLOW}Removing SSH key from account...${NC}" +curl \ + -s \ + -X DELETE \ + -H "Authorization: Bearer ${TOKEN}" \ + "https://api.github.com/user/keys/${KEYID}" + +echo -e "${YELLOW}Removing SSH key...${NC}\n" +rm -rf "${SSHK}"* + +echo -e "${GREEN}Finished!${NC}\n" diff --git a/scripts/autoupdate.sh b/scripts/autoupdate.sh new file mode 100644 index 0000000..9670ee7 --- /dev/null +++ b/scripts/autoupdate.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# -*- coding: utf-8 -*- + +set -euo pipefail + +# Version 2024/01/28 - Changelog: https://gist.github.com/AriRexouium/4a1277a9e8405a5b357778f6ff0764ba +# This script checks for updates to a GitHub Gist and automatically updates if a new version is available. +# +# Usage: ./autoupdate.sh +# +# License: +# This script is licensed under the GNU General Public License v3.0 or later. +# For more information, see https://www.gnu.org/licenses/gpl-3.0.html +# Copyright (C) 2023 Jarrett Aiken (https://achl.fr) + +GITHUB_USERNAME="AriRexouium"; GITHUB_GIST="4a1277a9e8405a5b357778f6ff0764ba" + +UPDATE_TIME_FILE="${0}.update" +if [ -f "${UPDATE_TIME_FILE}" ]; then + UPDATE_TIME=$(cat "${UPDATE_TIME_FILE}") +else + UPDATE_TIME=0 +fi + +CURRENT_TIME=$(date +%s); COOLDOWN_TIME=86400 +if [ $((CURRENT_TIME - UPDATE_TIME)) -ge ${COOLDOWN_TIME} ]; then + CURRENT_VERSION=$(cat "${0}"); LATEST_VERSION=$(curl -s https://gist.githubusercontent.com/${GITHUB_USERNAME}/${GITHUB_GIST}/raw) + if [ "${CURRENT_VERSION}" != "${LATEST_VERSION}" ]; then + echo "${LATEST_VERSION}" > "${0}" + chmod +x "${0}" + echo "${CURRENT_TIME}" > "${UPDATE_TIME_FILE}" + exec "./${0}" "${@}" + fi +fi diff --git a/scripts/batchprotect.sh b/scripts/batchprotect.sh new file mode 100644 index 0000000..045d0b1 --- /dev/null +++ b/scripts/batchprotect.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# -*- coding: utf-8 -*- + +set -euo pipefail + +# Version 2024/01/28 - Changelog: https://gist.github.com/AriRexouium/425d07d5e651b479069d0d56e4b5fa60 +# This script provides a wrapper to batch protect your files with PAR2 and GPG. +# +# Usage: ./batchprotect.sh +# +# License: +# This shell script is based on the example from +# https://wiki.archlinux.org/title/Parchive#Batch-protecting_your_files +# which is licensed under the GNU Free Documentation License 1.3 or later +# For more information, see https://www.gnu.org/licenses/fdl-1.3.html + +# Colors +ES="\x1B" +GREEN="$ES[0;32m" +NC="$ES[0m" + +# Internal Field Separator +OIFS="${IFS}" +IFS=$'\n' + +# Variables +gpgKey="ACE142C15F53182454DEE0802B45FEC5D0B181E7" +redundancy=10 + +list=$(find . -type f | shuf) +for file in ${list}; do + ending="${file//\(.*\)\.\(.*\)$/\2/}" # Extract the file extension from the filename using sed + + # Check if the PAR2 file doesn't already exist, the file isn't a PAR2 or signature file, and the file isn't empty + if [ ! -e "${file}-${redundancy}p.par2" ] && [ "${ending}" != "par2" ] && [ "${ending}" != "sig" ] && [ "$(stat --format=%s "${file}")" != 0 ]; then + echo -e "${GREEN}Creating archive for ${file}.${NC}" + par2 c -qq -n1 -r"${redundancy}" "${file}" > /dev/null 2>&1 | tee /dev/tty + rm "${file}.par2" + mv "${file}.vol"*"par2" "${file}-${redundancy}p.par2" + fi + + # Check if the signature file doesn't already exist and the file isn't a PAR2 or signature file + if [ ! -e "${file}.sig" ] && [ "${ending}" != "par2" ] && [ "${ending}" != "sig" ]; then + echo -e "${GREEN}Creating signature for ${file}.${NC}" + gpg --quiet --default-key "${gpgKey}" --detach-sign --yes "${file}" + fi +done + +IFS="${OIFS}" diff --git a/scripts/iris.sh b/scripts/iris.sh new file mode 100644 index 0000000..9c47d2b --- /dev/null +++ b/scripts/iris.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# -*- coding: utf-8 -*- + +set -euo pipefail + +# Version 2024/01/28 - Changelog: https://gist.github.com/AriRexouium/17dde617d245a6a7e91878974719d42c +# Iris Color Library +# This script sets all forground & background colors and graphic modifiers to variables. +# Based on: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 +# +# Usage: source ./iris.sh +# +# License: +# This script is licensed under the GNU General Public License v3.0 or later. +# For more information, see https://www.gnu.org/licenses/gpl-3.0.html +# Copyright (C) 2023 Jarrett Aiken (https://achl.fr) + +ES="\x1B" + +# Foreground Bright +FBla="$ES[30m" FBBla="$ES[90m" # Black +FRed="$ES[31m" FBRed="$ES[91m" # Red +FGre="$ES[32m" FBGre="$ES[92m" # Green +FYel="$ES[33m" FBYel="$ES[93m" # Yellow +FBlu="$ES[34m" FBBlu="$ES[94m" # Blue +FPur="$ES[35m" FBPur="$ES[95m" # Purple +FCya="$ES[36m" FBCya="$ES[96m" # Cyan +FWhi="$ES[37m" FBWhi="$ES[97m" # White +RFor="$ES[39m" # Default Foreground + +# Background Bright +BBla="$ES[40m" BBBla="$ES[100m" # Black +BRed="$ES[41m" BBRed="$ES[101m" # Red +BGre="$ES[42m" BBGre="$ES[102m" # Green +BYel="$ES[43m" BBYel="$ES[103m" # Yellow +BBlu="$ES[44m" BBBlu="$ES[104m" # Blue +BPur="$ES[45m" BBPur="$ES[105m" # Purple +BCya="$ES[46m" BBCya="$ES[106m" # Cyan +BWhi="$ES[47m" BBWhi="$ES[107m" # White +RBac="$ES[49m" # Default Background + +# Graphics Reset +SBol="$ES[1m" RBol="$ES[22m" # Bold +SDim="$ES[2m" RDim="$ES[22m" # Dim +SIta="$ES[3m" RIta="$ES[23m" # Italic +SUnd="$ES[4m" RUnd="$ES[24m" # Underline +SDun="$ES[21m" RDun="$ES[24m" # Double Underline +SBli="$ES[5m" RBli="$ES[25m" # Blinking +SInv="$ES[7m" RInv="$ES[27m" # Inverse +SHid="$ES[8m" RHid="$ES[28m" # Hidden +SStr="$ES[9m" RStr="$ES[29m" # Strikethrough +RAll="$ES[0m" # Reset All