#!/bin/bash # # modjar.bash v0.9.2 # scribbled by Dagmar d'Surreal # GIEF CREDIT IF STEELING... I CBA to worry about putting a licence on # code this trivial. show_usage () { echo "modjar.bash newversion.jar original.jar modfile [modfile ...]" >&2 echo "Mods can be class files or a zip/jar file." >&2 echo "" >&2 exit 2 } # To ensure filesystem tidiness goodbye () { local code=${1:-0} rm -rf $WORKDIR $WORKTWO exit $code } [[ -z "$1" ]] && show_usage; NEWFILE=$1 ; shift; [[ -z "$1" ]] && show_usage; ORIGFILE=$1 ; shift [[ -z "$1" ]] && show_usage; MODS=$* for cmd in mktemp unzip zip id chown chmod chgrp find; do if ! type $cmd 2>&1 >/dev/null; then echo "Oh dear. You have no $cmd command. Game over." >&2 echo "" exit 1 fi done WORKDIR=`mktemp -t -d modjar.XXXXXX 2>/dev/null` if [ $? != 0 ]; then echo "My goodness. mktemp failed. You just lost the game." >&2 echo "" goodbye 1 fi WORKTWO=`mktemp -t -d modjar.XXXXXX 2>/dev/null` if [ $? != 0 ]; then echo "My goodness. mktemp failed. You just lost the game." >&2 echo "" goodbye 1 fi # Now begins a long litany of things that could go wrong if [ ! -r $ORIGFILE ]; then echo "Your original file must exist and be readable." >&2 goodbye 1 fi if [ ! -f $ORIGFILE ]; then echo "Your original file must be A FILE. Ahem." >&2 goodbye 1 fi if [ -d $NEWFILE ]; then echo "Your new filename must be a filename, not a directory." >&2 goodbye 1 fi unzip -qq -d $WORKDIR $ORIGFILE if [ $? != 0 ]; then echo "Something went wrong when unzipping $ORIGFILE." >&2 goodbye 1 fi for dinkyfile in $MODS; do if [ ${dinkyfile##*\.} == 'class' ]; then cp -f $dinkyfile $WORKDIR/ if [ $? != 0 ]; then echo "Something went wrong when copying $dinkyfile into $WORKDIR." >&2 goodbye 1 fi elif [ "${dinkyfile##*\.}" == 'jar' ]; then unzip -qq -d -f $WORKDIR $dinkyfile if [ $? != 0 ]; then echo "Something went wrong when exploding $dinkyfile into $WORKDIR." >&2 goodbye 1 fi elif [ "${dinkyfile##*\.}" == 'zip' ]; then unzip -qq -d -f $WORKDIR $dinkyfile if [ $? != 0 ]; then echo "Something went wrong when exploding $dinkyfile into $WORKDIR." >&2 goodbye 1 fi else echo "I have no idea what to do with $dinkyfile." >&2 goodbye 1 fi done # Now to remove the signing files... rm -rf $WORKDIR/META-INF/* # now for some really OCD cleanups -- these basically can't fail unless # the system is on fire. chown -R `id -u` $WORKDIR chgrp -R `id -g` $WORKDIR find $WORKDIR -type d -exec chmod 755 {} \; find $WORKDIR -type f -exec chmod 644 {} \; if [ -d $NEWFILE ]; then echo "There is already a directory named $NEWFILE. Aborting." >&2 goodbye 1 fi rm -f $NEWFILE ( cd $WORKDIR zip -rq $WORKTWO/newfile.zip . ) if [ $? != 0 ]; then echo "Something went wrong creating the new file." >&2 goodbye 1 fi mv $WORKTWO/newfile.zip $NEWFILE goodbye; # vim: ts=2