#!/bin/sh

# script to generate the car 3D description file *.acc

## Set this PATH to the cars directory, where TORCS is installed!!!
TORCS_DIR=/usr/local/share/games/torcs/cars

currentdir=`pwd`

CAR='lnx-car1';

if [ $# -lt 1 ]
  then 
	echo "Usage: $0 Car_Name"
	echo "$CAR default used!"
  else
	CAR=$1
fi

# check car directory
if [ -d $TORCS_DIR ]
	then
	cardir=${TORCS_DIR}/$CAR
	else
	cardir=${currentdir}
	tbv="no"
fi

if [ -d $cardir ]
	then
		WORKDIR=${cardir}
	else
		BASEDIR=`pwd`
		WORKDIR=${BASEDIR}
fi


if [ ! -d ${WORKDIR} ]
 then
 	echo "ERROR: ${WORKDIR}/ directory doesnt exist!"
	exit 1
fi

cd $WORKDIR

if [ ! -r ${CAR}.ac ]
  then
  	echo "ERROR: ${WORKDIR}/$CAR.ac file doesnt exit!"
	exit 1
fi

echo " ======= Cleaning the $CAR.ac file ======"
	echo "*** Crease lines *** (remove all lines with 'Crease value')"
   	cp $CAR.ac $CAR-bak.ac
   	sed '/crease/d' $CAR.ac > $CAR.ac2 && mv $CAR.ac2 $CAR.ac
	rm $CAR-bak.ac

echo -n "Remove the Mesh lines ? (N/y)?  "

	echo "*** Mesh lines *** (remove all lines with 'Mesh value')"
	sed '/Mesh/d' $CAR.ac > $CAR-bak.ac && mv $CAR-bak.ac $CAR.ac
	echo ""


if [ -e $CAR.acc ]
  then
  	echo "$CAR.acc already exist!"
	echo "Do you want delete $cAR.acc "
	echo -n "Type 'yes' to delete it, (No/yes)?  "
	read FILEDEL
	if [ "$FILEDEL" != "yes" ]
	    then
	        echo "$0 aborted !"
	        exit 1
	    else
	       rm -f $CAR.acc
	fi
fi
	echo "generate $CAR.acc file"
	accc +es 0 $CAR.ac $CAR.acc

echo ""
count=''
echo " *** Generate the LOD files ***"
if [ -e $CAR-lod1.ac ]
   then
   echo "generate $CAR-lod1.acc file"
   	accc +es 30 $CAR-lod1.ac $CAR-lod1.acc
	count=1
fi

if [ -e $CAR-lod2.ac ]
   then
      echo "generate $CAR-lod2.acc file"
      accc +es 30 $CAR-lod2.ac $CAR-lod2.acc
      count=2
fi

if [ -e $CAR-lod3.ac ]
   then
      echo "generate $CAR-lod3.acc file"
      accc +es 30 $CAR-lod3.ac $CAR-lod3.acc
      count=3
fi

if [ -e $CAR-lod4.ac ]
   then
      echo "generate $CAR-lod4.acc file"
      accc +es 30 $CAR-lod4.ac $CAR-lod4.acc
      count=4
fi

if [ -z $count ]
  then
  	echo "No reduce acc files used"
  elif [ ! -e $CAR-lod4.acc ]
    	then
		echo "$CAR-lod4.acc not generated!"
    elif [ ! -e $CAR-lod3.acc ]
         then
                echo "$CAR-lod3.acc not generated!"
    else
  	echo "$count files generated"
  	echo "All done!"
fi

echo ""
exit 0

