Last active
June 16, 2020 18:14
-
-
Save pettazz/2194e05ecceefc1f722e991a475404b2 to your computer and use it in GitHub Desktop.
boy is this some extremely specific shit huh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
###################################################################### | |
# # | |
# da config zone # | |
# # | |
###################################################################### | |
# where to put all the converted files, relative to where this script lives | |
OUTPUT_DIR="output-dir" | |
# the good file extension we will convert stuff to (without the .) | |
TARGET_EXT=mdb | |
# file extensions that we'll look for to change to TARGET_EXT, no ., separated by spaces | |
CONVERTIBLE_EXTS="chg dat horse butt" | |
# name of the .jar converter tool file to use, in the same directory as this script | |
JAR_TOOL="client-0.0.5.jar" | |
# url to download the jar tool if needed, used if JAR_TOOL above is not found | |
JAR_DOWNLOAD="https://search.maven.org/remotecontent?filepath=com/rebasedata/client/0.0.5/client-0.0.5.jar" | |
###################################################################### | |
# # | |
# end of da config zone # | |
# # | |
###################################################################### | |
# hello, it me | |
mydir="$(cd "$(dirname "$0")" && pwd)" | |
cd "$mydir" | |
# check for java, die if missing | |
if ! type java > /dev/null; then | |
echo "java is not installed! go get a JRE or JDK!" 1>&2 | |
exit 1 | |
fi | |
# check if the output dir already exists, die if it does so we don't overwrite anything | |
if [ -d $OUTPUT_DIR ]; then | |
echo "Output directory \`$mydir/$OUTPUT_DIR\` already exists!" 1>&2 | |
echo "Exiting before we overwrite something." 1>&2 | |
exit 1 | |
fi | |
# check for the conversion tool, download if missing | |
if [ ! -f "$mydir/$JAR_TOOL" ]; then | |
echo "$JAR_TOOL is missing, downloading a new one..." | |
curl -L -# -o "$mydir/$JAR_TOOL" $JAR_DOWNLOAD | |
fi | |
# assemble the find command based on extensions to change | |
arg_str="" | |
first=true | |
for ext in $CONVERTIBLE_EXTS; do | |
arg="" | |
if ! $first; then | |
arg="$arg -o " | |
fi | |
first=false | |
arg="$arg-iname \\*.$ext" | |
arg_str="$arg_str$arg" | |
done | |
find_cmd="find \"$mydir\" -maxdepth 1 -type f $arg_str" | |
# rename all the files in the current dir with the given extensions to the target extension | |
eval $find_cmd | while read -r fp ; do | |
fp_escaped=$(printf %q "$fp") | |
target_fp="$mydir/$(basename "$fp" .${fp_escaped##*.}).$TARGET_EXT" | |
echo "renaming $fp -> $target_fp" | |
mv "$fp" "$target_fp" | |
done | |
# create output dir, temp dir | |
tmpdir="$mydir/tmpout" | |
mkdir -p "$tmpdir" | |
mkdir "$mydir/$OUTPUT_DIR" | |
# iterate over the mdb files | |
eval "find \"$mydir\" -maxdepth 1 -type f -iname \\*.mdb" | while read -r infile ; do | |
# we can't control the output filenames | |
# so, convert to csv files in a temp directory | |
filename=$(basename "$infile" .${infile##*.}) | |
mytmp="$tmpdir/$filename" | |
mkdir "$mytmp" | |
echo "converting $filename..." | |
java -jar "$mydir/$JAR_TOOL" convert --output-format=csv "$infile" "$mytmp" > /dev/null | |
# move all the temp csv files to the real output dir, | |
# while renaming using the original mdb file name. | |
# we ignore the .Columns because they seem pointless, really | |
mytmp_escaped=$(printf %q "$mytmp") | |
eval "find $mytmp_escaped -maxdepth 1 -type f -iname \\*.csv" | while read -r infile ; do | |
infile_base=$(basename "$infile") | |
mv "$infile" "$mydir/$OUTPUT_DIR/$filename-$infile_base" | |
done | |
done | |
rm -r "$tmpdir" | |
echo "done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment