#!/bin/bash
PK3="$1"
err=0
errmsg=""

myifs=$IFS
IFS="
"


function fError {
echo "Error: $1"
echo "#############################################################"
echo "## Extracts the mapnames from Wolfenstein ET map pk3 files"
echo "##                                                         "
echo "## usage:  $0 /path/to/a/pk3-file                          "
echo "##                                                         "
echo "## example $0 /home/et/.wolfet/etmain/pak0.pk3             "
echo "##                                                         "
echo "##                  Version 1.0                            "
echo "##                                                         "
echo "## Requirements: unzip, grep, gnutools                     "
echo "##      by schnoog                                         "
echo "##                                                         "
echo "#############################################################"
}

if [ "$PK3" == "" ]
then
	errmsg="Missing argument 'pk3 file'" 
err=1
fi

if [ ! -e "$PK3" ]
then
	errmsg="File not found"
err=1
fi


if [ "$err" == "0" ]
then
	work=`unzip -l "$PK3" | grep ".bsp" | cut -f "2" -d "/"`
	workcnt=`unzip -l "$PK3" | grep ".bsp" | cut -f "2" -d "/" | wc -l`
		if [ $workcnt -gt 0 ]
		then
			for bn in $work			
			do
			work1=`basename "$bn" .bsp`
			out="$work1 $out"
			done
		else
			out=""
		fi
echo $out
else
fError "$errmsg"
fi



IFS=$myifs