#!/bin/bash DISEMBEDED_LIB_LIST_1="!!!StandaloneLibraries/filelist.wau" DISEMBEDED_LIB_LIST="/tmp/buildLibList.0.$$" ALL_LIB_FILES="/tmp/buildLibList.1.$$" BUILT_LIST="/tmp/buildLibList.2.$$" SED_SCRIPT="/tmp/buildLibList.3.$$.sed" ALL_MOD_DIRS="/tmp/buildLibList.4.$$" NON_DISEMBEDED_MODS="/tmp/buildLibList.5.$$" KEEP_ACE2_TOC="Ace2/Ace2.toc" KEPT_LIST="/tmp/buildLibList.6.$$" if [ ! -f ${DISEMBEDED_LIB_LIST_1} ]; then echo "Couldn't find desembeded library list file ${DISEMBEDED_LIB_LIST}" exit 1 fi # convert the list to dos, or line endings screw up our scripts cp ${DISEMBEDED_LIB_LIST_1} ${DISEMBEDED_LIB_LIST} d2u ${DISEMBEDED_LIB_LIST} 2>/dev/null if [ -f ${ALL_LIB_FILES} ]; then rm ${ALL_LIB_FILES} fi while read libname do # read each toc file and get the list of libs from it. # we can put all of them together, remove "[lL]ibs" prefix, and then use uniq/sort to get single list if [[ -d ${libname} && -f ${libname}/${libname}.toc ]]; then # found a toc file TOCFILE="${libname}/${libname}.toc" # now remove lines starting with utf8 chars, #, empty lines and get rid of the prefix "libs\", "libs/", "Libs\", "Libs/" # echo "------------- ${TOCFILE}" if [ ${TOCFILE} = ${KEEP_ACE2_TOC} ]; then # keep this list for processing on the disembeded toc files sed -e '1s/^\xef\xbb\xbf//;/^#/d;/^$/d;s/^[lL]ibs[\\\/]//' < ${TOCFILE} | tee -a ${ALL_LIB_FILES} > ${KEPT_LIST} else sed -e '1s/^\xef\xbb\xbf//;/^#/d;/^$/d;s/^[lL]ibs[\\\/]//' < ${TOCFILE} >> ${ALL_LIB_FILES} fi echo "" >> ${ALL_LIB_FILES} fi done < ${DISEMBEDED_LIB_LIST} sort ${ALL_LIB_FILES} | uniq | sed '/^ *$/d' > ${BUILT_LIST} ## now ${BUILT_LIST} is a list of all the libraries that need to be commented out of non disembedded mods ## so take each toc file not in dirs not in the ${DISEMBEDED_LIB_LIST} ## and check it for entries that END in anything in our ${BUILT_LIST} ## easiest way to achieve this is to convert our BUILT_LIST into a sed file that searches for any of the entries ## and comments it out ## so the file: # AceLibrary\AceLibrary.lua # ... ## becomes # /^.*AceLibrary\.lua/s/^/#/ # etc ## NOTE: we assume all files are uniquely named, e.g. no other library has a different AceLibrary.lua etc. # create a file of the basenames of all libs sed 's/\\/\\\\/g' < ${BUILT_LIST} > ${BUILT_LIST}.1 while read fulldir do basename ${fulldir} >> ${BUILT_LIST}.2 done < ${BUILT_LIST}.1 # and do same with ace keeps sort ${KEPT_LIST} | uniq | sed '/^ *$/d' > ${BUILT_LIST}.ACE2 sed 's/\\/\\\\/g' < ${BUILT_LIST}.ACE2 > ${BUILT_LIST}.ACE2.1 while read fulldir do basename ${fulldir} >> ${BUILT_LIST}.ACE2.2 done < ${BUILT_LIST}.ACE2.1 # convert ${BUILT_LIST}.2 to a script sed 's/\./\\\./g;s/^/\/\^\.\*/;s/$/\/s\/\^\/\#\//' < ${BUILT_LIST}.2 > ${SED_SCRIPT} # above does: # 1. ... moved to pre-step # 2. converts . to \. # 3. inserts "/^.*" at start of every line # 4. inserts "/s/^/#/" at end of every line # do same for ace2 libs list sed 's/\./\\\./g;s/^/\/\^\.\*/;s/$/\/s\/\^\/\#\//' < ${BUILT_LIST}.ACE2.2 > ${SED_SCRIPT}.ACE2 # find all mod dirs NOT in the DISEMBEDED_LIB_LIST find -maxdepth 1 -type d -printf "%f\n" > ${ALL_MOD_DIRS} cat ${ALL_MOD_DIRS} ${DISEMBEDED_LIB_LIST} | sort | uniq -u > ${NON_DISEMBEDED_MODS} # now find the toc file in each of these dirs and apply the sed script to it if [ ! -s ${NON_DISEMBEDED_MODS} ]; then # didn't find any mods to edit echo "Could not find any mods to edit toc files for" exit 1 fi while read moddir do MODTOCFILE=$(find ${moddir} -maxdepth 1 -name \*.toc) if [[ "${MODTOCFILE}" != "" && -s ${MODTOCFILE} ]]; then echo "Processing ${MODTOCFILE}" if [ ! -s ${MODTOCFILE}.001 ]; then cp ${MODTOCFILE} ${MODTOCFILE}.001 fi sed -f ${SED_SCRIPT} < ${MODTOCFILE} > ${MODTOCFILE}.new mv ${MODTOCFILE}.new ${MODTOCFILE} fi done < ${NON_DISEMBEDED_MODS} # now process the disembeded list by removing the Ace2 libs while read libdir do LIBTOCFILE=$(find ${libdir} -maxdepth 1 -name \*.toc) if [[ "${LIBTOCFILE}" != "" && -s ${LIBTOCFILE} && ${LIBTOCFILE} != ${KEEP_ACE2_TOC} ]]; then echo "Library ${LIBTOCFILE} being processed for Ace2 libs" if [ ! -s ${LIBTOCFILE}.001 ]; then cp ${LIBTOCFILE} ${LIBTOCFILE}.001 fi sed -f ${SED_SCRIPT}.ACE2 < ${LIBTOCFILE} > ${LIBTOCFILE}.new mv ${LIBTOCFILE}.new ${LIBTOCFILE} fi done < ${DISEMBEDED_LIB_LIST} # cleanup rm ${DISEMBEDED_LIB_LIST} rm ${ALL_LIB_FILES} rm ${BUILT_LIST} rm ${BUILT_LIST}.1 rm ${BUILT_LIST}.2 rm ${BUILT_LIST}.ACE2 rm ${BUILT_LIST}.ACE2.1 rm ${BUILT_LIST}.ACE2.2 rm ${SED_SCRIPT} rm ${SED_SCRIPT}.ACE2 rm ${ALL_MOD_DIRS} rm ${NON_DISEMBEDED_MODS}