Skip to content

Commit

Permalink
Refactor various bash_profile.sh functionality out to various bash libs.
Browse files Browse the repository at this point in the history
  • Loading branch information
codercowboy committed Jul 19, 2024
1 parent f0c1e86 commit e262d0b
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 399 deletions.
33 changes: 33 additions & 0 deletions bash_lib/lib_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

if [ "`type -t chrome_local_dev`" = "function" ]; then
echo "Skipping import functions from lib_dev.sh, they're already sourced in this shell"
return 0
fi

#####################
# DEVELOPMENT STUFF #
#####################

function chrome_local_dev {
# from: https://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome
open /Applications/Google\ Chrome.app --args --user-data-dir="/var/tmp/Chrome dev session" --disable-web-security
}
export -f chrome_local_dev

alias usejdk11='echo "switching to jdk 11" && export JAVA_HOME=${TOOLS}/jdk-11.0.18.jdk/Contents/Home'
alias usejdk8='echo "switching to jdk 8" && export JAVA_HOME=${TOOLS}/jdk1.8.0_411/Contents/Home'
usejdk11

export M2_HOME="${TOOLS}/apache-maven-3.8.6" # maven stuff
export MAVEN_OPTS="-Xmx3g -XX:MaxPermSize=512m" # maven stuff
export MVND_HOME="${TOOLS}/mvnd-0.8.2-darwin-amd64" # mvnd
export GOPATH=${HOME}/Documents/code/tools/go #go

export PATH="${JAVA_HOME}/bin:${M2_HOME}:${M2_HOME}/bin:${MVND_HOME}/bin:${GOPATH}/bin:${PATH}"
export PATH="${PATH}:${TOOLS}/eclipse/Eclipse.app/Contents/MacOS" # eclipse
export PATH="/usr/local/bin:${PATH}" # homebrew stuff is installed here
export PATH="/Applications/RealVNC/VNC\ Viewer.app/Contents/MacOS:${PATH}" # vnc viewer

#start a http server in current directory
alias webserverhere='python -m SimpleHTTPServer 8070'
45 changes: 45 additions & 0 deletions bash_lib/lib_files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

if [ "`type -t inspect_files`" = "function" ]; then
echo "Skipping import functions from lib_files.sh, they're already sourced in this shell"
return 0
fi

function inspect_files {
if [ "${1}" = "" -o "${2}" = "" ]; then
echo "USAGE: inspect_files [path] [output file prefix]"
return
fi
echo "Gathering checksum info to file: ${2}-checksums.txt"
md5tool.sh DISPLAY "${1}" > "${2}-checksums.txt"
# reverses output, instead of "2G ./somefile" it is now "./somefile [2G]"
SED_COMMAND="sed -E 's/^[^[:digit:]]*//' | sed -E 's/[[:space:]]/::/' | sed -E 's/(.*)::(.*)/\2 [\1]/'"
echo "Gathering dir info to file: ${2}-dirs.txt"
eval "du -h \"${1}\" | ${SED_COMMAND}" > "${2}-dirs.txt"
echo "Gathering file info w/ actual size to file: ${2}-files-real-size.txt"
eval "du -a \"${1}\" | ${SED_COMMAND}" > "${2}-files-real-size.txt"
echo "Gathering file info w/ summary size to file: ${2}-files-summary.txt"
eval "du -a -h \"${1}\" | ${SED_COMMAND}" > "${2}-files-summary.txt"
}
export -f inspect_files

function move_files_here_fn {
if [ "${1}" = "" ]; then
echo "USAGE: move_files_here [source directory]"
echo " source directory is directory to find files in"
echo " files will be moved to current working directory"
return
fi

OLD_IFS=${IFS}
IFS=$'\n'
FILES=`find "${1}" -type f | sort`
FILE_COUNT=`echo "${FILES}" | wc -l`
echo "Moving ${FILE_COUNT} files to target directory `pwd -P`"
for FILE in ${FILES}; do
echo "Moving: ${FILE}"
mv "${FILE}" .
done
IFS=${OLD_IFS}
}
alias move_files_here='move_files_here_fn ${1}'
63 changes: 63 additions & 0 deletions bash_lib/lib_macos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

if [ "`type -t terminal_open_tab`" = "function" ]; then
echo "Skipping import functions from lib_macos.sh, they're already sourced in this shell"
return 0
fi

######################
# VARIOUS OSX TRICKS #
######################

# handy secret trick to emulate a command+k terminal clear which clears scrollback buffer
alias cls='printf "\33c\e[3J"'

# unlock osx "locked files" (whatever that is)
alias unlock_files='sudo chflags nouchg ${1}/*'

# auto open sublime text to the given directory or file.
# can't get this to work as an alias, oh well.
function stext { /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl "${@}"; }
export -f stext

# from: https://stackoverflow.com/a/7177891
# opens a new tab in terminal
function terminal_open_tab {
osascript -e 'tell application "Terminal" to activate' \
-e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
}
export -f terminal_open_tab

function terminal_tab_execute {
COMMAND="${1}"
COMMAND="tell application \"Terminal\" to do script \"${1}\" in selected tab of the front window"
# echo "Command is: \"${COMMAND}\""
osascript -e 'tell application "Terminal" to activate'
osascript -e "${COMMAND}"
}
export -f terminal_tab_execute

function thin_local_snapshots {
echo "Looking for local time machine backups to remove."
REMOVAL_COUNT=0
for SNAPSHOT in `tmutil listlocalsnapshots / | grep -v "Snapshots for disk"`; do
SNAPSHOT_DATE=`echo "${SNAPSHOT}" | sed 's/com.apple.TimeMachine.//' | sed 's/.local//'`
echo "Removing snapshot '${SNAPSHOT}', date: ${SNAPSHOT_DATE}"
tmutil deletelocalsnapshots ${SNAPSHOT_DATE}
REMOVAL_COUNT=$((REMOVAL_COUNT+1))
done
echo "Finished removing time machine backups, removed ${REMOVAL_COUNT} backups."
}
export -f thin_local_snapshots

function clean_dot_files {
if [ -z "${1}" ]; then
echo "USAGE: clean_dot_files [directory]"
return;
fi
echo "Removing ._* files"
find "${1}" -type f -name "._*" -exec rm -rv {} \;
echo "Removing .DS_Store files"
find "${1}" -type f -name ".DS_Store" -exec rm -rv {} \;
}
export -f clean_dot_files
34 changes: 34 additions & 0 deletions bash_lib/lib_media.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

if [ "`type -t ffmpeg_convert_webm_to_mp3`" = "function" ]; then
echo "Skipping import functions from lib_media.sh, they're already sourced in this shell"
return 0
fi

# ffmpeg stuff

function ffmpeg_convert_webm_to_mp3() {
if [ -z "${1}" ]; then
echo "USAGE: ffmpeg_convert_webm_to_mp3 [DIRECTORY]"
echo " This will convert each webm in the given directory to mp3."
return
fi
OLD_IFS=${IFS}
IFS=$'\n'
FILES=`find ${1} -type f | grep webm`
for FILE in ${FILES}; do
echo -e "Processing video: ${FILE}";
ffmpeg -i "${FILE}" -codec:a libmp3lame -b:a 320k -ar 44100 -y "${1}/${FILE%.webm}.mp3";
done;
IFS=${OLD_IFS}
}
export -f ffmpeg_convert_webm_to_mp3

# youtube-dl stuff

# NOTE: yt_dlp is available easily via homebrew

# youtube-dl -f bestaudio --restrict-filenames --max-downloads 999 -r 5000K --buffer-size 16K --audio-quality 0 --sleep-interval 60 --max-sleep-interval 300 "https://www.youtube.com/playlist?list=PLVNmxQCckyw5bo9u008T5cZNJ_QwWW5o1"
YT_DLP_FILE_FORMAT="%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s"
alias yt_dlp_mp3='yt-dlp -x --audio-format mp3 --audio-quality 0 -o "${YT_DLP_FILE_FORMAT}" ${1}'

82 changes: 82 additions & 0 deletions bash_lib/lib_rsync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

if [ "`type -t my_rsync_checksum`" = "function" ]; then
echo "Skipping import functions from lib_rsync.sh, they're already sourced in this shell"
return 0
fi

###############
# RSYNC STUFF #
###############

# export these functions so child processes, such as scripts running, can see them
# https://stackoverflow.com/questions/33921371/command-not-found-when-running-defined-function-in-script-file

# rsync arguments
# -v = verbose
# -r = recursive
# -t = preserve times
# -W = transfer whole file
# --del = delete non existing files
# -stats = print stats at end
# --progress = show progress while transfering
# --chmod = change perms on target
# -c = skip based on checksum rather than mod-time/size
# --size-only = skip only based on size changes, not checksum or modtime
# --modify-window = allow a mod time drive of N seconds, useful for fat32 with less precise mod-time storage
# -i = show the reason rsync is transfering the file
# -n = dry run (test mode)

# itemized changes output example:
# >f..t.... file.txt
# c = checksum differs, s = size differs, t = mod time differs ,p = perms differ
# o = owner differs, g = group differs

function my_rsync_checksum {
rsync -cvrthW --del --stats --progress --chmod=u=rwx "${@}"
}
export -f my_rsync_checksum

function my_rsync_checksum_test {
rsync -incvrthW --del --stats --progress --chmod=u=rwx "${@}"
}
export -f my_rsync_checksum_test

function my_rsync() {
rsync -vrthW --del --stats --progress --chmod=u=rwx "${@}"
echo ""
echo "WARNING: my_rsync only skips files based on size / mod time differences!"
echo " for a more secure checksum-based transfer, use my_rsync_checksum"
}
export -f my_rsync

function my_rsync_test {
rsync -invrthW --del --stats --progress --chmod=u=rwx "${@}"
echo ""
echo "WARNING: my_rsync only skips files based on size / mod time differences!"
echo " for a more secure checksum-based transfer, use my_rsync_checksum"
}
export -f my_rsync_test

# fat32's modtime isn't as precise as other file systems, and there are various other problems
# such as the fat32 not storing timezones, so during DST the file's mod time looks to be off by one hour
# for this reason, on the fat32 alias we're using the --size-only command that ignores mod times
# source: https://stackoverflow.com/questions/15640570/rsync-and-backup-and-changing-timezone
# source: https://serverfault.com/questions/470046/rsync-from-linux-host-to-fat32
# fat32's

function my_rsync_fat32 {
rsync -rv --size-only --del --stats --progress "${@}"
echo ""
echo "WARNING: my_rsync_fat32 only skips files based on size difference, not mod time!"
echo "WARNING: my_rsync_fat32 does not preserve mod-times!"
}
export -f my_rsync_fat32

function my_rsync_fat32_test {
rsync -inrv --size-only --del --stats --progress "${@}"
echo ""
echo "WARNING: my_rsync_fat32 only skips files based on size difference, not mod time!"
echo "WARNING: my_rsync_fat32 does not preserve mod-times!"
}
export -f my_rsync_fat32_test
99 changes: 99 additions & 0 deletions bash_lib/lib_security.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash

if [ "`type -t ssh_setup_passwordless`" = "function" ]; then
echo "Skipping import functions from lib_security.sh, they're already sourced in this shell"
return 0
fi

#################
# SSH SHORTCUTS #
#################

# local port 2121 point to screen share on server
# local port 5901 is proxy for internet thru server
alias ssh_my_server='ssh -L 2121:localhost:5900 -D 5901 ${MY_USER}@${MY_SERVER}'

# open osx's screenshare via terminal
alias screenshare_my_server_ssh='open vnc://localhost:2121'
alias screenshare_my_server_local='open vnc://${MY_SERVER_NAME}._rfb._tcp.local'

# from: http://www.linuxproblem.org/art_9.html
function ssh_setup_passwordless {
if [ "${1}" = "" ]; then
echo "USAGE: ssh_setup_passwordless user@host"
return
fi

echo "Setting up passwordless ssh on ${1}"

# create the key
if [ ! -e ~/.ssh/id_rsa ]; then
echo "generating rsa key: ~/.ssh/id_rsa"
ssh-keygen -t rsa -q -N "" -f ~/.ssh/id_rsa
else
echo "rsa key for ssh already exists: ~/.ssh/id_rsa"
fi

HOST_PUBLIC_KEY=`cat ~/.ssh/id_rsa.pub`

REMOTE_COMMAND="mkdir -p ~/.ssh;
chmod 700 ~/.ssh;
echo \"${HOST_PUBLIC_KEY}\" >> ~/.ssh/authorized_keys;
chmod 644 ~/.ssh/authorized_keys;"

echo "Enter your password for the remote host, we need this to copy your public key to the remote host with ssh."

ssh "${1}" "$REMOTE_COMMAND"

echo "Passwordless setup is complete. You should now be able to verify the passwordless login with: ssh ${1}"
}
export -f ssh_setup_passwordless

###################
# MacOS VPN STUFF #
###################

if [ "${VPN_NAME}" = "" ]; then
export VPN_NAME="VPN (thin)"
fi

function vpn_connect {
echo "[CONNECTING TO VPN: ${VPN_NAME}]"

local VPN_STATUS=`networksetup -showpppoestatus "${VPN_NAME}"`
if [ "connected" = "${VPN_STATUS}" ]; then
echo "VPN is already connected"
return 0
fi

networksetup -connectpppoeservice "${VPN_NAME}"
sleep 10

local VPN_STATUS=`networksetup -showpppoestatus "${VPN_NAME}"`
if [ "connected" != "${VPN_STATUS}" ]; then
echo "ERROR: VPN didn't connect, status is: ${VPN_STATUS}"
return 1
fi

return 0
}

function vpn_disconnect {
echo "[DISCONNECTING FROM VPN: ${VPN_NAME}]"
local VPN_STATUS=`networksetup -showpppoestatus "${VPN_NAME}"`
if [ "disconnected" = "${VPN_STATUS}" ]; then
echo "VPN is already disconnected"
return 0
fi

networksetup -disconnectpppoeservice "${VPN_NAME}"
sleep 10

local VPN_STATUS=`networksetup -showpppoestatus "${VPN_NAME}"`
if [ "disconnected" != "${VPN_STATUS}" ]; then
echo "ERROR: VPN didn't disconnect, status is: ${VPN_STATUS}"
return 1
fi

return 0
}
Loading

0 comments on commit e262d0b

Please sign in to comment.