diff options
author | Toby Vincent <tobyv13@gmail.com> | 2022-03-29 15:33:22 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-03-29 15:33:22 -0500 |
commit | 835b994969c90a3caa40bc43fae8d71e1bfbca56 (patch) | |
tree | 47ba45b6f75edff6f9821af9486e9f9345916ba1 /scripts/.local | |
parent | c3cfb415f86c48488a41e9ac44b2ca9c4cc5825b (diff) |
refactor: moved scripts out of $HOME
Diffstat (limited to 'scripts/.local')
-rwxr-xr-x | scripts/.local/bin/discord-relay.sh | 2 | ||||
-rwxr-xr-x | scripts/.local/bin/git-split.sh | 208 | ||||
-rw-r--r-- | scripts/.local/bin/gpg-test.sh | 102 | ||||
-rwxr-xr-x | scripts/.local/bin/init-git.sh | 193 | ||||
-rwxr-xr-x | scripts/.local/bin/init-latex.sh | 11 | ||||
-rwxr-xr-x | scripts/.local/bin/install-crate.sh | 235 | ||||
-rwxr-xr-x | scripts/.local/bin/mega-dl.sh | 121 | ||||
-rwxr-xr-x | scripts/.local/bin/mkln.sh | 115 | ||||
-rwxr-xr-x | scripts/.local/bin/socket-relay.sh | 217 | ||||
-rwxr-xr-x | scripts/.local/bin/td.sh | 151 |
10 files changed, 1355 insertions, 0 deletions
diff --git a/scripts/.local/bin/discord-relay.sh b/scripts/.local/bin/discord-relay.sh new file mode 100755 index 0000000..3932908 --- /dev/null +++ b/scripts/.local/bin/discord-relay.sh @@ -0,0 +1,2 @@ +#!/bin/sh +exec socat UNIX-LISTEN:/var/run/discord-ipc-0,fork,group=discord,umask=007 EXEC:"npiperelay.exe -ep -s //./pipe/discord-ipc-0",nofork >/dev/null 2>&1 diff --git a/scripts/.local/bin/git-split.sh b/scripts/.local/bin/git-split.sh new file mode 100755 index 0000000..ecb2b3a --- /dev/null +++ b/scripts/.local/bin/git-split.sh @@ -0,0 +1,208 @@ +#!/usr/bin/env bash + +TEMP=$(getopt -o hvdymu:r: --long help,verbose,debug,submodule,remote:,username:,visibility: \ + -n 'javawrap' -- "$@") + +if [ $? != 0 ]; then + echo "Terminating..." >&2 + exit 1 +fi + +if ! git rev-parse --is-inside-work-tree &>/dev/null; then + echo "Must be run inside a git repository" >&2 + exit 1 +fi + +eval set -- "$TEMP" + +SCRIPT="$(basename $0)" +ARTIFACTS=(".gitignore" ".gitattributes" ".vscode" "LICENSE") +VERBOSE=false +DEBUG=false +CONFIRMED=false +SUBMODULE=false +REPO=$(git rev-parse --show-toplevel) +USERNAME="$(git config user.username)" +REMOTE= +VISIBILITY="--public" + +read -r -d '' USAGE <<-EOF +USAGE: $SCRIPT [OPTIONS] + +OPTIONS: + -h, --help Show this message + -v, --verbose Show more output + -d, --debug NOT IMPLEMENTED + -y, --confirm Do not prompt for confirmation + -m, --submodule + Migrate directory in original repository to submodule + + -u, --username=<user/repo> + Github username. Can be used instead of remote to dynamically name + the repositories. If --username and --remote are both ommited, defaults + to $(git config user.username) + + -r, --remote=<user/repo> + Github remote, defaults to ${username}/${directory} + + --private + Create the new remote repository as private. It is created + as public if ommited + +ARGS: + <DIRECTORY>... Directory to split out of original repository, + defaults to current directory +EOF + +while true; do + case "$1" in + -h | --help) + echo "$USAGE" + exit 0 + ;; + -v | --verbose) + VERBOSE=true + shift + ;; + -d | --debug) + DEBUG=true + shift + ;; + -y) + CONFIRMED=true + shift + ;; + -m | --submodule) + SUBMODULE=true + shift + ;; + -u | --username) + USERNAME="$2" + shift 2 + ;; + -r | --remote) + REMOTE="$(basename $(dirname ${2#"git@github.com:"}))/$(basename ${2#"git@github.com:"})" + shift 2 + ;; + --public | --private) + VISIBILITY="$1" + shift + ;; + --) + shift + break + ;; + *) + break + ;; + esac +done + +DIRECTORY="${1:-$(realpath --relative-to="$REPO" .)}" + +if [ ! -d "${REPO}/${DIRECTORY}" ]; then + echo "ERROR: Directory does not exist." >&2 + echo "$USAGE" + exit 1 +fi + +if [ -z "$REMOTE" ]; then + if [ -z "$USERNAME" ]; then + echo "ERROR: No remote provided and user.username was not set." >&2 + echo "$USAGE" + exit 1 + else + REMOTE="${USERNAME}/${DIRECTORY}" + fi +fi + +set -e + +cd $REPO + +git subtree split -P $DIRECTORY -b $DIRECTORY + +cd "$(mktemp -d)" + +git init && git pull $REPO $DIRECTORY && git branch -M main + +for f in "${ARTIFACTS[@]}"; do + if [[ ! -e "./${f}" && -e "${REPO}/${f}" ]]; then + printf '%s\n' "Copying ${f} to new repository..." + cp -r "${REPO}/${f}" ./ + fi +done + +git add -A && git commit -m "split out $DIRECTORY into submodule" + +# TODO check if remote already exists +if command -v gh &>/dev/null; then + yes "n" | gh repo create --confirm "${REMOTE}" "${VISIBILITY}" +else + printf '%s\n' 'gh cli tool could not be found. Explicitly adding git remote to your local repository.' + + git remote add origin https://github.com/${REMOTE} +fi + +set +e + +git push -u origin main + +if [ $? -ne 0 ]; then + printf '\n%s\n' 'Verify/create the remote repository and push to it with the following command: ' + printf '\n\t%s%s%s\n\n' '`cd ' "$(pwd)" ' && git push -u origin main`' + + printf '\t%s\n' \ + '*********************************************************************************' \ + '* The new repository was created in a temp directory! *' \ + '* If the local repository is not pushed to remote, it will be lost on reboot. *' \ + '*********************************************************************************' +else + tempdir="$(pwd)" + cd "$REPO" + rm -rf "$tempdir" +fi + +# Migrate to submodule + +if [ "$SUBMODULE" != true ]; then + exit 0 +fi + +# TODO fix and remove warning + +printf 'Migrating the original subdirectory to submodule.\n' + +if [ "$QUIET" != true ]; then + printf '\t%s\n' \ + '*************************************************************************************' \ + '* THIS IS NOT TESTED AND HAS A POSSIBLY OF DATA LOSS! *' \ + '* *' \ + '* This runs `git rm -rf` and `rm -rf` on the original subdirectory. *' \ + '* Double check you succesfully migrated the subdirectory to the new repository. *' \ + '*************************************************************************************' +fi + +if [ "$CONFIRMED" != true ]; then + read -p "Proceed? (Y/n) " -n 1 -r + printf '\n' + if [[ $REPLY =~ ^[Yy]$ ]]; then + CONFIRMED=true + else + [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 + fi +fi + +cd $REPO + +git rm -rf $DIRECTORY +rm -rf $DIRECTORY + +git submodule add git@github.com:$REMOTE $DIRECTORY +git submodule update --init --recursive + +git commit -m "split out $DIRECTORY into submodule" + +if [ $? -ne 0 ]; then + printf '%s\n' 'Submodule succesfully added. Be sure to `git push -u origin main` after verifing the migration.' +fi diff --git a/scripts/.local/bin/gpg-test.sh b/scripts/.local/bin/gpg-test.sh new file mode 100644 index 0000000..f23b83d --- /dev/null +++ b/scripts/.local/bin/gpg-test.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +TEMP=$(getopt -o hvdsea: --long help,verbose,debug,signature,encryption,authentication \ + -n 'javawrap' -- "$@") + +if [ $? != 0 ]; then + echo "Terminating..." >&2 + exit 1 +fi + +eval set -- "$TEMP" + +usage() { + cat <<EOF +usage: $0 [OPTIONS] + + -h,--help Show this message + -v,--verbose Show test output + -d,--debug NOT IMPLEMENTED + -s,--signature Test signature key + -e,--encryption Test encryption key + -a,--authentication Test authentication key +EOF +} + +VERBOSE=false +DEBUG=false +SIGNATURE=false +ENCRYPTION=false +AUTHENTICATION=false +ALL=true + +while true; do + case "$1" in + -h | --help) + usage + exit 0 + ;; + -v | --verbose) + VERBOSE=true + shift + ;; + -d | --debug) + DEBUG=true + shift + ;; + -s | --signature) + ALL=false + SIGNATURE=true + shift + ;; + -e | --encryption) + ALL=false + ENCRYPTION=true + shift + ;; + -a | --authentication) + ALL=false + AUTHENTICATION=true + shift + ;; + --) + shift + break + ;; + *) break ;; + esac +done + +function print_result() { + if [ $? -ne 0 ]; then + echo "Failed: $?" + else + echo "Succeeded" + fi + + if [ $VERBOSE -eq "true" ]; then + echo "\nOutput:\n$result" + fi +} + +if [[ $ALL -eq "true" || $SIGNITURE -eq "true" ]]; then + echo "Testing signiture key..." + result="$(echo '' | gpg --clearsign &>/dev/null 2>&1)" + print_result result +fi + +if [[ $ALL -eq "true" || $ENCRYPTION -eq "true" ]]; then + echo "Testing encryption key..." + temp="$(mktemp -d)" + echo "secret file contents: 42" >$temp/test.txt + + result="$(gpg --output $temp/test.gpg -e $temp/test.txt 2>&1 && gpg --output $temp/test.out -d $temp/test.txt.gpg 2>&1)" + grep '42' $temp/test.out 2>&1 + print_result result +fi + +if [[ $ALL -eq "true" || $AUTHENTICATION -eq "true" ]]; then + echo "Testing authentication key..." + result="$(ssh-add -l)" + print_result result +fi diff --git a/scripts/.local/bin/init-git.sh b/scripts/.local/bin/init-git.sh new file mode 100755 index 0000000..0a45b90 --- /dev/null +++ b/scripts/.local/bin/init-git.sh @@ -0,0 +1,193 @@ +#!/usr/bin/env bash + +# Global +SCRIPT="$(basename $0)" +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +GITATTRIBUTES_URL="https://gist.githubusercontent.com/tobyvin/70f3671c76016063594ea45edbb97094/raw" + +# Defaults +VERBOSE=0 +QUIET=0 +INTERACTIVE=0 +TEMPLATE="visualstudiocode" +LICENSE="mit" + +# Usage output +read -r -d '' USAGE <<USAGE +USAGE: $SCRIPT [OPTIONS] <IGNORE_TEMPLATE> + +OPTIONS: + -h, --help Show this message + -v, --verbose Show more output + -q, --quiet Suppress all output + -i, --interactive Run command interactivly + + -l, --license [LICENSE_ID] + Specify which license to generate. Defaults to '$LICENSE' + + -t, --template [IGNORE_TEMPLATE,...] + Project template(s) used when generating the .gitignore file. It + can also be a comma seperated list or templates. Use '-t list' to + see available templates. Defaults to '$TEMPLATE' +USAGE + +# Options +SHORT=hvqil:t: +LONG=help,verbose,quiet,interactive,license:,template: + +# Test getopt +getopt --test 2>/dev/null +if [[ $? -ne 4 ]]; then + read -r -d '' message <<EOF +GNU's enhanced getopt is required to run this script +You can usually find this in the util-linux package +On MacOS/OS X see homebrew's package: http://brewformulas.org/Gnu-getopt +For anyone else, build from source: http://frodo.looijaard.name/project/getopt +EOF + echo "$message" >&2 + exit 1 +fi + +# Parse options +TEMP=$(getopt \ + --options ${SHORT} \ + --longoptions ${LONG} \ + --name ${SCRIPT} \ + -- "$@") + +# Exit on failed getopt +if [ $? != 0 ]; then + echo "Error in getopt. Terminating..." >&2 + exit 1 +fi + +eval set -- "${TEMP}" +unset TEMP + +while [[ $# -gt 0 ]]; do + case "$1" in + -h | --help) + echo "$USAGE" + exit 0 + ;; + -v | --verbose) + VERBOSE=1 + shift + ;; + -q | --quiet) + QUIET=1 + shift + ;; + -i | --interactive) + INTERACTIVE=1 + shift + ;; + -l | --license) + LICENSE="$2" + shift 2 + ;; + -t | --template) + TEMPLATE="$2" + shift 2 + ;; + --) + shift + break + ;; + *) break ;; + esac +done + +VALID_TEMPLATES=$(curl -L -s "https://www.toptal.com/developers/gitignore/api/list") + +validate-template() { + local template="$1" + local templates="$2" + if [[ ",$2," == *",$template,"* ]]; then + [ "$QUIET" != 1 ] && printf "'%s' is already added.\n" "$template" >&2 + return 1 + elif ! [[ $VALID_TEMPLATES =~ "$template" ]]; then + [ "$QUIET" != 1 ] && printf "'%s' is not a valid template.\n" "$template" >&2 + return 1 + else + [ "$VERBOSE" == 1 ] && printf "Added template: %s\n" "$template" >&2 + return 0 + fi +} + +get-gitignore() { + local templates='' + IFS=',' read -ra input <<<"$1" + + for template in "${input[@]}"; do + if validate-template "$template" "$templates"; then + templates+="${template}," + fi + done + + echo ${templates%?} +} + +get-gitignore-interactive() { + local templates='' + + while true; do + read -p 'Input gitignore template(s): ' -i "$1" readInput + echo "" + + IFS=', ' input=$readInput + [ -z "$input" ] && break + if [[ "$input" == "list" ]]; then + echo "$VALID_TEMPLATES" >&2 + else + for template in $input; do + if validate-template "$template" "$templates"; then + templates+="${template}," + fi + done + fi + [ "$QUIET" != 1 ] && printf "\nTemplates: %s\n" "${templates%?}" >&2 + done + + echo "${templates%?}" +} + +get-license() { + licenseJson="$(curl -sH 'Accept: application/vnd.github.v3+json' https://api.github.com/licenses/$1)" + # not_found='"message": "Not Found"' + if [[ $licenseJson =~ '"message": "Not Found"' ]]; then + [ "$QUIET" != 1 ] && printf "'%s' is not a valid license identifier.\n" "$LICENSE" >&2 + else + echo $licenseJson | grep -oP '.*"body":\s*"\K.*(?=\s*",)' | + tr '\n' '\0' | xargs -0 printf '%b\n' | + sed "s/\\[year\\]/$(date +'%Y')/" | + sed "s/\\[fullname\\]/$(git config --get user.name)/" + fi +} + +if [[ "$TEMPLATE" == "list" ]]; then + echo "$VALID_TEMPLATES" + exit 0 +fi + +if [ $INTERACTIVE -eq 0 ]; then + gitignore_cmd=get-gitignore +else + gitignore_cmd=get-gitignore-interactive +fi + +TEMPLATES=$($gitignore_cmd $TEMPLATE) + +# .gitignore +[ "$VERBOSE" == 1 ] && printf "Creating .gitignore using: %s\n" "$TEMPLATES" >&1 +curl -L -s "https://www.toptal.com/developers/gitignore/api/${TEMPLATES}" >.gitignore + +# .gitattributes +[ "$VERBOSE" == 1 ] && printf "Creating .gitattributes\n" >&1 +curl -sL "$GITATTRIBUTES_URL" >.gitattributes + +# LICENSE +[ "$VERBOSE" == 1 ] && printf "Creating LICENSE using: %s\n" "$LICENSE" >&1 +get-license $LICENSE >LICENSE + +echo "$PWD" diff --git a/scripts/.local/bin/init-latex.sh b/scripts/.local/bin/init-latex.sh new file mode 100755 index 0000000..7b8125e --- /dev/null +++ b/scripts/.local/bin/init-latex.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +USER="tobyvin" +GIST_ID="c94716d1106256e4ab0e27aed398a0a2" + +curl -Ls "https://gist.github.com/${USER}/${GIST_ID}/download" -o tempFile +unzip -q tempFile +rm -rf tempFile +mv "${GIST_ID}-master" styles +mv styles/template.tex "$(basename "$(pwd)").tex" +mv styles/table.tex . diff --git a/scripts/.local/bin/install-crate.sh b/scripts/.local/bin/install-crate.sh new file mode 100755 index 0000000..1c1578a --- /dev/null +++ b/scripts/.local/bin/install-crate.sh @@ -0,0 +1,235 @@ +#!/bin/sh + +# modified from https://github.com/japaric/trust/blob/gh-pages/install.sh. + +set -e + +help() { + cat <<'EOF' +Install a binary release of a Rust crate hosted on GitHub + +Usage: + install-crate.sh [options] + +Options: + -h, --help Display this message + -q, --quiet Silence all output + --git SLUG Get the crate from "https://github/$SLUG" + -f, --force Force overwriting an existing binary + --crate NAME Name of the crate to install (default <repository name>) + --tag TAG Tag (version) of the crate to install (default <latest release>) + --no-tag Do not tag between the crate and the target in the url + --target TARGET Install the release compiled for $TARGET (default <`rustc` host>) + --to LOCATION Where to install the binary (default ~/.cargo/bin) + --completion + Install all the completions + + --completion-bash DIR + Install the bash completion scripts in DIR (default ~/.local/share/bash_completion.d) + + --completion-zsh DIR + Install the zsh completion scripts in DIR (default ~/.local/share/zsh/site-functions) + + --completion-fish DIR + Install the fish completion scripts in DIR (default ~/.config/fish/completions) +EOF +} + +say() { + if [ ! $quiet ]; then + echo "install-crate.sh: $1" + fi +} + +say_err() { + say "$1" >&2 +} + +err() { + if [ ! -z $td ]; then + rm -rf $td + fi + + say_err "ERROR $1" + exit 1 +} + +need() { + if ! command -v $1 >/dev/null 2>&1; then + err "need $1 (command not found)" + fi +} + +is_opt() { case $1 in "--"*) true ;; *) false ;; esac } +is_arg() { if [ "$1" ] && ! is_opt $1; then true; else false; fi; } + +quiet=false +force=false +no_tag=false +completion_bash=false +completion_zsh=false +completion_fish=false +comp_dir_bash="${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion/completions" +comp_dir_zsh="${XDG_DATA_HOME:-$HOME/.local/share}/zsh/site-functions" +comp_dir_fish="${XDG_CONFIG_HOME:-$HOME/.config}/fish/completions" +while test $# -gt 0; do + case $1 in + --quiet | -q) + quiet=true + ;; + --crate) + crate=$2 + shift + ;; + --force | -f) + force=true + ;; + --git) + git=$2 + shift + ;; + --help | -h) + help + exit 0 + ;; + --tag) + tag=$2 + shift + ;; + --no-tag) + no_tag=true + ;; + --target) + target=$2 + shift + ;; + --to) + dest=$2 + shift + ;; + --completion) + completion_bash=true + completion_zsh=true + completion_fish=true + ;; + --completion-bash) + completion_bash=true + if is_arg $2; then + comp_dir_bash=$2 + shift + fi + ;; + --completion-zsh) + completion_zsh=true + if is_arg $2; then + echo "$2" + comp_dir_zsh=$2 + shift + fi + ;; + --completion-fish) + completion_fish=true + if is_arg $2; then + comp_dir_fish=$2 + shift + fi + ;; + *) ;; + + esac + shift +done + +# Dependencies +need basename +need curl +need install +need mkdir +need mktemp +need tar + +# Optional dependencies +if [ -z $crate ] || [ -z $tag ] || [ -z $target ]; then + need cut +fi + +if [ -z $tag ]; then + need rev +fi + +if [ -z $target ]; then + need grep + need rustc +fi + +if [ -z $git ]; then + err 'must specify a git repository using `--git`. Example: `install.sh --git japaric/cross`' +fi + +url="https://github.com/$git" +say_err "GitHub repository: $url" + +if [ -z $crate ]; then + crate=$(echo $git | cut -d'/' -f2) +fi + +say_err "Crate: $crate" + +url="$url/releases" + +if [ -z $tag ]; then + tag=$(curl -s "$url/latest" | cut -d'"' -f2 | rev | cut -d'/' -f1 | rev) + say_err "Tag: latest ($tag)" +else + say_err "Tag: $tag" +fi + +if [ -z $target ]; then + target=$(rustc -Vv | grep host | cut -d' ' -f2) +fi + +say_err "Target: $target" + +if [ -z $dest ]; then + dest="$HOME/.cargo/bin" +fi + +if [ $no_tag = true ]; then + crate_tag="$crate" +elif case $tag in "$crate"*) true ;; *) false ;; esac then + crate_tag="$tag" +else + crate_tag="$crate-$tag" +fi + +say_err "Installing to: $dest" +url="$url/download/$tag/${crate_tag}-$target.tar.gz" + +say_err "Downloading: $url" + +td=$(mktemp -d || mktemp -d -t tmp) +curl -sL $url | tar -C $td -xz + +for f in $(find "$td" -type f); do + case $f in + *".bash") + [ $completion_bash ] && install -D $f "$comp_dir_bash/$crate" + ;; + *".zsh" | *"_$crate") + [ $completion_zsh ] && install -D $f "$comp_dir_zsh/_$crate" + ;; + *".fish") + [ $completion_fish ] && install -D $f "$comp_dir_fish/$crate.fish" + ;; + *) ;; + esac + + test -x $f || continue + if [ -e "$dest/$f" ] && [ $force = false ]; then + err "$(dirname $f) already exists in $dest" + else + install -Dm 755 $f $dest + fi +done + +rm -rf $td diff --git a/scripts/.local/bin/mega-dl.sh b/scripts/.local/bin/mega-dl.sh new file mode 100755 index 0000000..b19be68 --- /dev/null +++ b/scripts/.local/bin/mega-dl.sh @@ -0,0 +1,121 @@ +#!/bin/bash + +# Source: https://gist.github.com/zanculmarktum/170b94764bd9a3da31078580ccea8d7e + +# Copyright 2018, 2019, 2020 Azure Zanculmarktum +# All rights reserved. +# +# Redistribution and use of this script, with or without modification, is +# permitted provided that the following conditions are met: +# +# 1. Redistributions of this script must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED +# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# Dependencies: +# openssl + +# Usage: +# $ ./megafetch.sh https://mega.nz/#!abcdefgh!1234567890abcdefghijklmnopqrstuvwxyzABCDEFG +# http://gfs208n103.userstorage.mega.co.nz/dl/-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890A +# file.name +# abcefghijklmnopqrstuvwxyz0123456 +# 1234567890abcdef0000000000000000 +# $ wget -O file.name http://gfs208n103.userstorage.mega.co.nz/dl/-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890A +# $ cat file.name | openssl enc -d -aes-128-ctr -K abcefghijklmnopqrstuvwxyz0123456 -iv 1234567890abcdef0000000000000000 > file.name.new +# $ mv -f file.name.new file.name + +URL="" +OUTPATH="$PWD" +USAGE="Usage: ${0##*/} <url>" + +if [[ $1 =~ ^https?:\/\/mega(\.co)?\.nz ]]; then + URL="$1" +fi + +if [[ ! $URL ]]; then + echo $USAGE >&2 + exit 1 +fi + +if [[ ! -z "$2" ]]; then + OUTPATH="$2" +fi + +if [[ ! -d "$OUTPATH" ]]; then + echo $USAGE >&2 + exit 1 +fi + +CURL="curl -Y 1 -y 10" + +missing=false +for cmd in openssl; do + if [[ ! $(command -v "$cmd" 2>&1) ]]; then + missing=true + echo "${0##*/}: $cmd: command not found" >&2 + fi +done +if $missing; then + exit 1 +fi + +if [[ $URL =~ .*/file/[^#]*#[^#]* ]]; then + id="${URL#*file/}" + id="${id%%#*}" + key="${URL##*file/}" + key="${key##*#}" +else + id="${URL#*!}" + id="${id%%!*}" + key="${URL##*!}" +fi + +raw_hex=$(echo "${key}=" | tr '\-_' '+/' | tr -d ',' | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ') +hex=$( + printf "%016x" \ + $((0x${raw_hex:0:16} ^ 0x${raw_hex:32:16})) \ + $((0x${raw_hex:16:16} ^ 0x${raw_hex:48:16})) +) + +json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "g":"1", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1 +json="${json#"[{"}" +json="${json%"}]"}" +file_url="${json##*'"g":'}" +file_url="${file_url%%,*}" +file_url="${file_url//'"'/}" + +json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1 +at="${json##*'"at":'}" +at="${at%%,*}" +at="${at//'"'/}" + +json=$(echo "${at}==" | tr '\-_' '+/' | tr -d ',' | openssl enc -a -A -d -aes-128-cbc -K "$hex" -iv "00000000000000000000000000000000" -nopad | tr -d '\0') +json="${json#"MEGA{"}" +json="${json%"}"}" +file_name="${json##*'"n":'}" +if [[ $file_name == *,* ]]; then + file_name="${file_name%%,*}" +fi +file_name="${file_name//'"'/}" + +outfile="$OUTPATH/$file_name" + +$CURL -s "$file_url" | openssl enc -d -aes-128-ctr -K "$hex" -iv "${raw_hex:32:16}0000000000000000" >"$outfile" + +echo "$outfile" + +# echo "$file_url" +# echo "$file_name" +# echo "$hex" +# echo "${raw_hex:32:16}0000000000000000" diff --git a/scripts/.local/bin/mkln.sh b/scripts/.local/bin/mkln.sh new file mode 100755 index 0000000..ef174c2 --- /dev/null +++ b/scripts/.local/bin/mkln.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash + +TEMP=$(getopt -o hvqdf --long help,verbose,quiet,debug,force \ + -n 'javawrap' -- "$@") + +if [ $? != 0 ]; then + echo "Terminating..." >&2 + exit 1 +fi + +eval set -- "$TEMP" + +SCRIPT="$(basename $0)" +VERBOSE=false +QUIET=false +DEBUG=false +FORCE=false + +read -r -d '' USAGE <<-EOF +USAGE: $SCRIPT [OPTIONS] <SOURCE> <TARGET> + +OPTIONS: + -h, --help Show this message + -v, --verbose Show more output + -q, --quiet Suppress all output + -d, --debug NOT IMPLEMENTED + -f, --force Overwrite items + +ARGS: + <SOURCE> <TARGET>... File to link. +EOF + +while true; do + case "$1" in + -h | --help) + echo "$USAGE" + exit 0 + ;; + -v | --verbose) + VERBOSE=true + shift + ;; + -q | --quiet) + QUIET=true + shift + ;; + -d | --debug) + DEBUG=true + shift + ;; + -f | --force) + FORCE=true + shift + ;; + --) + shift + break + ;; + *) + break + ;; + esac +done + +if ! command -v powershell.exe &>/dev/null; then + [ "$QUIET" != true ] && echo "Powershell not found in path." >&2 + exit 1 +fi + +if [ -z "$2" ]; then + 2="$(pwd)" +fi + +[ "$VERBOSE" == true ] && echo "linking $2 -> $1" + +if [ -d "$1" ]; then + [ "$VERBOSE" == true ] && echo "$1 is a directory. Creating symbolic link." + args='/D' +fi + +mkdir -p $(dirname $2) + +source=$(wslpath -w $1) +target=$(wslpath -w $(dirname $2))\\$(basename $2) + +if ls -la "$(dirname $2)/" 2>/dev/null | grep -q "$(basename $2)"; then + + current_path=$(powershell.exe -c "(Get-Item $target).Target" 2>/dev/null) + + if [[ "${current_path/*wsl$/}" == *"${source/*wsl$/}"* ]]; then + [ "$VERBOSE" == true ] && echo "$(basename $2) is set correctly. Skipping." + exit 0 + fi + + if [ "$FORCE" == true ]; then + [ "$VERBOSE" == true ] && echo "$(basename $2) exists. Overwriting." + rm -rf "$2" + else + [ "$QUIET" != true ] && echo "$(basename $target) already exists. Use -f to overwrite." >&2 + exit 1 + fi +fi + +mkdir -p "$(dirname $2)" +cmd="cd ~; cmd /c mklink ${args} ${target} ${source}" + +if [ "$DEBUG" == true ]; then + [ "$QUIET" != true ] && printf '\nCommand: \n%s\n\n' "powershell.exe -c ${cmd} &>/dev/null" +else + if $QUIET; then + powershell.exe -c "${cmd}" &>/dev/null + else + powershell.exe -c "${cmd}" + fi +fi diff --git a/scripts/.local/bin/socket-relay.sh b/scripts/.local/bin/socket-relay.sh new file mode 100755 index 0000000..436fa54 --- /dev/null +++ b/scripts/.local/bin/socket-relay.sh @@ -0,0 +1,217 @@ +#!/usr/bin/env bash +set -euo pipefail + +OPTS=$(getopt -o hds:x: --long help,debug,sock:,exec:,ssh,gpg,gpg-extra,discord -n 'javawrap' -- "$@") + +eval set -- "$OPTS" + +PAGEANT="$HOME/.ssh/wsl2-ssh-pageant.exe" +NPIPE="$(command -v npiperelay.exe)" +SCRIPT="$(basename "$0")" + +help() { + cat <<-EOF +$SCRIPT +Toby Vincent <tobyv13@gmail.com> + +$SCRIPT description + +USAGE: + $SCRIPT [OPTIONS] <COMMAND> + $SCRIPT [OPTIONS] + +OPTIONS: + -h, --help Display this message + -s, --sock <SOCKET> Path to the linux socket + -x, --exec <CMD> Command to run for socat's EXEC: arg + --ssh Relay the ssh agent's socket + --gpg Relay the gpg agent's socket + --gpg-extra Relay the gpg agent's extra socket + +ARGS: + start (default) + Start the socat process + + stop + Kill the socat process +EOF +} + +say() { + if ! $quiet; then + echo "$SCRIPT: $1" + fi +} + +say_err() { + say "$1" >&2 +} + +err() { + say_err "ERROR: $1" + exit 1 +} + +need() { + for cmd in $@; do + if ! command -v $cmd >/dev/null 2>&1; then + err "need $cmd (command not found)" + fi + done +} + +start() { + if ! ss -a | grep -q "$sock"; then + if test -x "$relay"; then + rm -rf "$sock" + (setsid nohup socat UNIX-LISTEN:"${sock},${listen_args}" EXEC:"$cmd" >/dev/null 2>&1 &) + + else + echo >&2 "WARNING: $relay is not executable." + fi + fi + + if $gpg; then + gpg-connect-agent.exe /bye &>/dev/null + fi +} + +stop() { + if $gpg; then + gpg-connect-agent.exe KILLAGENT /bye &>/dev/null + fi + + if pgrep -f "socat.*$sock.*$relay" &>/dev/null; then + pkill -f "socat.*$sock.*$relay" + fi +} + +check() { + pgrep -fa "socat.*$sock.*$relay" +} + +quiet=false +debug=false +sock="" +cmd="" +gpg=false +ssh=false +extra=false +discord=false +while test $# -gt 0; do + case $1 in + -h | --help) + help + return 0 + ;; + -q | --quiet) + quiet=true + shift + ;; + -d | --debug) + debug=true + shift + ;; + -s | --sock) + sock=$2 + shift + shift + ;; + -x | --exec) + cmd=$2 + shift + shift + ;; + --ssh) + ssh=true + shift + ;; + --gpg) + gpg=true + shift + ;; + --gpg-extra) + gpg=true + extra=true + shift + ;; + --discord) + discord=true + shift + ;; + --) + shift + break + ;; + *) + help + exit 1 + ;; + esac +done + +need ss +need socat + +listen_args="fork" + +case true in +$ssh) + need $PAGEANT + + sock="${SSH_AUTH_SOCK:-$HOME/.ssh/agent.sock}" + cmd="$PAGEANT" + ;; +$gpg) + need $PAGEANT + need "gpg-connect-agent.exe" + + sock="${GPG_AGENT_SOCK:-$HOME/.gnupg/S.gpg-agent}" + if $extra; then + sock+=".extra" + fi + + cmd="$PAGEANT --gpg $(basename $sock)" + ;; +$discord) + need $NPIPE + + sock="${DISCORD_IPC_SOCK:-/var/run/discord-ipc-0}" + cmd="$NPIPE -ep -s //./pipe/$(basename $sock)" + + listen_args="fork,group=discord,umask=007" + ;; +$(test -z "${cmd}")) + err "No EXEC provided. Must supply either --ssh, --gpg, --gpg-extra, --pipe, or --exec" + ;; +esac + +if test -z "${sock}"; then + err "No socket provided." +fi + +relay="$(echo $cmd | head -n1 | awk '{print $1;}')" + +case ${1:-'start'} in +check) + shift + check + ;; +start) + shift + start + ;; +stop) + shift + stop + ;; +restart) + shift + stop + start + ;; +*) + help + exit 1 + ;; +esac diff --git a/scripts/.local/bin/td.sh b/scripts/.local/bin/td.sh new file mode 100755 index 0000000..968c98b --- /dev/null +++ b/scripts/.local/bin/td.sh @@ -0,0 +1,151 @@ +#!/bin/sh +TEMP=$(getopt -o hf --long help,force \ + -n 'javawrap' -- "$@") + +if [ $? != 0 ]; then + echo "Terminating..." >&2 + exit 1 +fi + +eval set -- "$TEMP" + +SCRIPT="$(basename $0)" +ENV_CACHE="${HOME}/.cache/td/.env" + +help() { + cat <<-EOF +$SCRIPT +Toby Vincent <tobyv13@gmail.com> + +$SCRIPT is a script for managing and navigating a persistent temp directory. In +order to function properly, this script must be sourced in the currently shell. +Consider setting an alias such as + + alias td=". $SCRIPT" + +USAGE: + $SCRIPT [OPTIONS] <COMMAND> + $SCRIPT [OPTIONS] + +OPTIONS: + -h, --help Display this message + -f, --force Use the --force argument when removing TD + +ARGS: + toggle (default) + Switch between TD_ORIGIN and TD. This will create TD if it does not + exist. This is the default if no command is provided. + + new | create + Creates a new temp directory and cds into it it stores the path + to the temp directory in TD, and the path to the previous directory in + TD_ORIGIN. + + rm | remove + Removes the directory stored in TD unsets TD and TD_ORIGIN. + + ls | list | show + Shows the current values for TD and TD_ORIGIN +EOF +} + +echo_err() { + echo >&2 "$SCRIPT: $@" +} + +show() { + if [ ! -n "$TD" ]; then + echo_err "Not set" + return 1 + fi + echo "TD=$TD" + echo "TD_ORIGIN=$TD_ORIGIN" +} + +remove() { + if [ ! -n "$TD" ]; then + echo_err "Not set" + return 1 + fi + + rm "$rm_args" "$TD" + + echo "removed $TD" + + if [ "$PWD" = "$TD" ]; then + cd "$TD_ORIGIN" + fi + + rm "$ENV_CACHE" -rf + unset TD + unset TD_ORIGIN +} + +create() { + remove 2>/dev/null + + td=$(mktemp -d) + + export TD="$td" + update-origin + + echo "created $TD" + cd "$TD" +} + +update-origin() { + export TD_ORIGIN="$PWD" + cat >$ENV_CACHE <<-EOF +TD=$TD +TD_ORIGIN=$TD_ORIGIN +EOF +} + +toggle() { + if [ "$PWD" = "$TD" ]; then + cd "$TD_ORIGIN" + elif [ -n "$TD" ]; then + update-origin + cd "$TD" + else + create + fi +} + +if [ -f "$ENV_CACHE" ]; then + export $(cat "$ENV_CACHE" | xargs) +fi + +rm_args="-r" +while test $# -gt 0; do + case $1 in + --help | -h) + help + return 0 + ;; + --force | -f) + rm_args="-rf" + shift + ;; + --) + shift + break + ;; + *) break ;; + esac +done + +case $1 in +ls | list | show) + show + ;; +rm | remove) + remove + ;; +new | create) + create + ;; +* | toggle) + toggle + ;; +esac |