Skip to content

Instantly share code, notes, and snippets.

@connrs
Last active May 7, 2018 19:32
Show Gist options
  • Save connrs/7d3303de8986d338c2454ed4d07a4177 to your computer and use it in GitHub Desktop.
Save connrs/7d3303de8986d338c2454ed4d07a4177 to your computer and use it in GitHub Desktop.
mkcarpet - bash script to create a carpet .jar file

mkcarpet

My idea was to make a bash script that takes a URL to a Carpet server release from Scicraft's mods channel and to add it as the first parameter when you call mkcarpet. It will then:

  • Check if you already have a cached copy of 1.12 server (if you don't then it will download it to ~/.mkcarpet)
  • Check if it can unzip/unrar the path that you've provided
  • Check if it's a URL (if it spots http(s) it assumes a URL and otherwise defaults to a local path)
  • Downloads the URL to a temp directory
  • Creates a temporary output directory
  • Unpacks the server in to it
  • Unpacks the Carpet release in to the output directory
  • Zips it back up
  • Gives it a .jar extension
  • Moves it to your specified output directory

Usage:

mkcarpet carpetpathurl outputdir

carpetpathurl: This is the HTTP(S) URL to a Carpet release or a local file path to a pre-downloaded .zip or .rar file outputdir: This is the directory you'd like the final Carpet release to be moved to.

Installation

Caveats/Warnings

I've set it to exit immediately on any unexpected errors and I've set it to output useful debugging info when it's running. If it exits in a random spot then the last line of logging may help identify how far the process reached and could be used to fix bugs in this script or make improvements.

In an ideal world, everyone's machine would already have all the packages that you need to use this little bashscript. If the carpet releases are always a .zip file then there's a good chance that a Mac or Linux machine will already have the following binaries installed:

  • curl
  • unzip
  • zip

If the carpet release is a .rar file then you may need to check that you have an unrar binary installed. On a Debian/Ubuntu based system this may need you to run sudo apt-get install unrar-free in a terminal. On Red Hat/Fedora based systems you may need to have the EPEL yum repo activated and I suspect that sudo yum install unrar will work for you. On a mac you will likely need to install homebrew.

Fixes

I've tried to comment this as well as possible but I'd welcome any suggestions. If there's a way to make the code clearer to read or if there's a comment that needs some tweaking to be easier to understand then do fire any feedback my way. I'm in full time work with a family so apologies in advance if I'm a little slow to resolve things.

You're welcome to make your own adjustments and to fork this and steal this. For those who prefer to have an explicit OSS licence I've opted to use the Unlicense so that it's clear it's free to use and tweak.

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org

#!/bin/bash
# Just exit on any error
set -e
if [[ -z "$1" && -z "$2" ]]; then
echo -e "[\033[32mHelp\033[0m] How to use mkcarpet:"
echo
echo "Usage: mkcarpet carpetzip outputdir"
echo ""
echo "carpetzip: This is the path/URL to a valid Carpet .zip file"
echo "outputdir: This is the directory that you wish to output the finished server jar file"
else
if [[ -z "$1" || -z "$2" ]]; then
echo -e "[\033[31mUsage error\033[0m] mkcarpet carpetzip outputdir"
exit 0
fi
fi
# MKCARPETDIR
mkcdir="$HOME/.mkcarpet"
# URL to a valid download of Minecraft Server 1.12
serverurl="https://s3.amazonaws.com/Minecraft.Download/versions/1.12/minecraft_server.1.12.jar"
# Just the filename
filename=$(basename "$1")
# Work out the extension
extension="${filename##*.}"
# Work out the 'version' e.g. Carpet_vXX_XX_XX.zip will give a 'version' of Carpet_vXX_XX_XX
# This is used later for the output file. Carpet_vXX_XX_XX.zip result in a final product of: Carpet_vXX_XX_XX.jar
version="${filename%.*}"
# Work out whether this is a zip file (ideal scenario) or a rar (might not be supported on some systems so do a check)
if [ "$extension" == "zip" ]; then
mode=zip
# Stop halting on error
set +e
# Check for an unzip programme
if [ "$(which unzip)" == "" ]; then
echo -e "[\033[31mPath error\033[0m] Could not find unzip binary"
exit 1
fi
# Resume halting on error
set -e
elif [ "$extension" == "rar" ]; then
# rar
mode=rar
# Stop halting on error
set +e
# Check for an unzip programme
if [ "$(which unrar)" == "" ]; then
echo -e "[\033[31mPath error\033[0m] Could not find unrar binary"
exit 1
fi
# Resume halting on error
set -e
else
# blah
echo -e "[\033[31mPath error\033[0m] Could not determine file type from path/URL"
exit 1
fi
# Assume the server zip is in the base dir
serverzip="$mkcdir"/"minecraft_server.1.12.zip"
# Output directory
outputdir=$(readlink -f "$2")
# Output file
outputzip="$outputdir"/"$version".jar.zip
outputjar="$outputdir"/"$version".jar
# Check if the file already exists. If it's already there then we need to remove it
if [ -s "$outputjar" ]; then
echo -en "[\033[31mWarning\033[0m] The file $outputjar already exists. "
read -p "Overwrite it? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# If Y or y then remove the file
rm "$outputjar"
else
# They didn't say yes so just gracefully exit
exit 0
fi
fi
if [ ! -d "$mkcdir" ]; then
echo -e "[\033[33mWarning\033[0m] Core directory does't exist. Creating $mkcdir"
mkdir -p "$mkcdir"
fi
if [ ! -s "$serverzip" ]; then
echo -e "[\033[33mWarning\033[0m] No minecraft server jar is downloaded. Downloading..."
curl "$serverurl" > "$serverzip"
fi
# Set up a temporary directory
tmpfilename=`mktemp -d -p "$mkcdir"`
echo -e "[\033[32mInfo\033[0m] Creating temp directory: $tmpfilename"
mkdir "$tmpfilename/out"
# Change to this temp dir
cd "$tmpfilename"
regex="^https?://"
if [[ $1 =~ $regex ]]; then
# Now we think this is a URL so let's download it
echo -e "[\033[32mInfo\033[0m] Downloading"
curl "$1" > "$filename"
# The path to the 'zip' is right here
carpetzip=$(readlink -f "$filename")
else
# Here we just know it's a path to the file so let's
# just work with it
carpetzip=$(readlink -f "$1")
fi
cd out
# Unzip server
echo -e "[\033[32mInfo\033[0m] Unzipping server zip inside $tmpfilename/out"
unzip -qo "$serverzip"
# Unpack over the top
echo -e "[\033[32mInfo\033[0m] Unpacking $version over unzipped server files"
if [ "$mode" == "zip" ]; then
unzip -qo "$carpetzip"
elif [ "$mode" == "rar" ]; then
unrar -f "$carpetzip" . > /dev/null
fi
# Make initial zip
echo -e "[\033[32mInfo\033[0m] Creating server zip: $outputzip"
zip -qr "$outputzip" .
# Changing name
echo -e "[\033[32mInfo\033[0m] Renaming zip to jar: $outputjar"
mv "$outputzip" "$outputjar"
# Clean up temp dir
echo -e "[\033[32mInfo\033[0m] Removing temp directory: $tmpfilename"
rm -rf "$tmpfilename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment