#!/bin/bash
#Make sure bash is in bin if it isnt try locate bash

#Packer Made for DOD
FILEID="FILE_ID.DIZ"
NFO="DOD.NFO"

###########################################################################
echo "Make sure you are running this program from the dir where the files"
echo "are you want to pack. Also make sure the FILE_ID.DIZ & DOD.NFO are"
echo "in this dir (pay attention to the CAPITAL letters) to change the "
echo "nfo or file_id.diz name edit the variables in the first lines."
echo ""


echo -n "Enter the name of the zip files, eg. dodmon: "
read NAME
if [ "$NAME" = "" ]; then
echo "No name suplied, exiting"
exit
fi

echo -n "Enter the format of the files that are to be packed eg. arj [arj]: "
read FORMAT
if [ "$FORMAT" = "" ]; then
FORMAT=arj
fi

echo -n "Enter the files that are to be packed (wildcards) eg. mon.a*: "
read FILES2PACK
if [ "$FILES2PACK" = ""  ]; then
echo "No files supplied, exiting"
exit
fi

if [ "`ls $FILES2PACK`" = "" ]; then
echo "Files don't exist! Exiting"
exit
fi

echo -n "10 files or more? (Y/N): "
read BOOLEAN
if [ "$BOOLEAN" = "Y" -o "$BOOLEAN" = "y" ]; then
 BOOLEAN=1
else
 BOOLEAN=0
fi
NUMBER=1

echo -n "Files to Add to all Zip-Files ($FILEID is standard): "
read FILES2ADD
if [ "$FILES2ADD" = "" ]; then
FILES2ADD="$FILEID"
else
FILES2ADD=`echo "$FILES2ADD $FILEID"`
fi

if [ ! -f "$FILEID" ]; then
echo "$FILEID not Found! Exiting."
exit
fi
cp $FILEID tempfile_id.diz

echo -n "Files to Add to all Zip-File ($NFO is standard): "
read FILES2ADD2FIRST
if [ "$FILES2ADD2FIRST" = "" ]; then
FILES2ADD2FIRST="$NFO $FILEID"
else
FILES2ADD2FIRST="$FILES2ADD2FIRST $NFO $FILEID"
fi

if [ ! -f "$NFO" ]; then
echo "$NFO not Found! Exiting."
exit
fi
echo ""
echo ""
echo "I have added the option of automatic $FILEID updates"
echo "so the $FILEID disk 1 of a 30 disk release would contain"
echo "[o1/30] and disk 24 of the same release [24/30]"
echo "To use this make sure your $FILEID is formatted like this:"
echo "Releaseinfowhatevercrap [xx/xx]"
echo "the [xx/xx] is the important part because the program searches"
echo "for this,"
echo ""
echo ""

echo -n "Do you wish to use this option? (Y/N): "
read SECONDBOOLEAN
if [ "$SECONDBOOLEAN" = "Y" -o "$SECONDBOOLEAN" = "y" ]; then
 SECONDBOOLEAN=1
else
 SECONDBOOLEAN=0
fi

INFOLINE=`cat $FILEID|grep -i "xx/xx"`
RESTOFFILEID=`cat $FILEID|grep -vi "xx/xx"`
RELEASEPART=`echo "$INFOLINE"|cut -d"[" -f1`
TOTALDISK=$[`ls -1 $FILES2PACK|wc -l` + 0]

if [ $TOTALDISK -lt "10" ]; then
TOTALDISK="0$TOTALDISK"
fi

LINE=`ls -1 $FILES2PACK`
  FIRSTFILE=`echo "$LINE"|grep -i "$FORMAT"`
  REST=`echo "$LINE"|grep -vi "$FORMAT"`
  if [ "$SECONDBOOLEAN" = 1 ]; then
  echo "$RELEASEPART [0$NUMBER"/"$TOTALDISK]" > $FILEID
  echo "$RESTOFFILEID" >> $FILEID
  fi
  if [ $BOOLEAN = "1" ]; then
  FILENAME=`echo "$NAME"0"$NUMBER.zip"`
  else
  FILENAME=`echo "$NAME$NUMBER.zip"`
  fi
  zip $FILENAME $FIRSTFILE $FILES2ADD2FIRST> /dev/null
echo "$REST"|while read LINE;do
  NUMBER=$[$NUMBER + 1]
  if [ $BOOLEAN = "1" -a "$NUMBER" -lt "10" ]; then
	  if [ "$SECONDBOOLEAN" = 1 ]; then
  echo "$RELEASEPART [0$NUMBER"/"$TOTALDISK]" > $FILEID
  echo "$RESTOFFILEID" >> $FILEID
  fi
  FILENAME=`echo "$NAME"0"$NUMBER.zip"`
  else
  if [ "$SECONDBOOLEAN" = 1 ]; then
  echo "$RELEASEPART [$NUMBER"/"$TOTALDISK]" > $FILEID
  echo "$RESTOFFILEID" >> $FILEID
  fi
  FILENAME=`echo "$NAME$NUMBER.zip"`
  fi
  zip $FILENAME $LINE $NFO $FILES2ADD > /dev/null
done

echo ""
echo ""

mv tempfile_id.diz $FILEID
