#!/bin/sh
# This script was organized by Mikamikem
# Some parts of this script were re-written ideas or exact copies of a script
# written by Gerry and his original script can be found here:
# http://delirare.com/stuff/nwnbink2ogm.php
# Most of the ideas that were used from this script have been re-written
# however some ideas may have been copied directly
# Please do not copy this script or any part without giving credit to
# the original creator and please leave this entire header in this script
# if you decide to host it.

# Please only edit these lines (although the program will ask you for these values anyway)

wine="cedega"
mencoder="mencoder"
binkconv="binkconv.exe"
path="/home/michael/nwn/movies"
vpasses="2"
bitrate="1200"

#After this point you should not need to edit anything

orig=1
orig1=0
orig2=0
orig3=0
orig4=0
orig5=0
orig6=0
orig7=0
orig8=0
orig9=0
orig10=0
orig11=0
orig12=0
sou=1
sou1=0
sou2=0
sou3=0
sou4=0
hotu=1
hotu1=0
movies=0
all=0

function convert()
{
echo "Please wait at least 30 seconds for the conversion utility to start."
echo "Once it reaches 100% please press done."
$wine -- $binkconv $1.bik $1.jpg
echo "Video Conversion is complete and if the program didn't throw any errors everything worked fine!"
echo "Please wait at least 30 seconds for the conversion utility to start."
echo "Once it reaches 100% please press done."
$wine -- $binkconv $1.bik $1.wav
echo "Audio Conversion is complete and if the program didn't throw any errors everything worked fine!"
if [ "$3" == "1" ]; then
    echo "The video encoding will begin now..."
    $mencoder mf://*.jpg -mf fps=$2 -o $1.avi -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$4 -oac copy -audiofile $1.wav
    echo "If you recieve a segfault at the end it is likely the video will work just fine so it should be safe to ignore that error for now."
    echo "Video Conversion should be complete."
fi
if [ "$3" == "2" ]; then
    echo "The first pass of the encoding will begin now..."
    $mencoder mf://*.jpg -mf fps=$2 -o $1.avi -ovc lavc -lavcopts vcodec=mpeg4:vpass=1:vbitrate=$4 -oac copy -audiofile $1.wav
    echo "Video Conversion is almost done."
    echo "If you recieve a segfault at the end it is likely the video will work just fine so it should be safe to ignore that error for now."
    echo "The second pass of the encoding will begin now..."
    $mencoder mf://*.jpg -mf fps=$2 -o $1.avi -ovc lavc -lavcopts vcodec=mpeg4:vpass=2:vbitrate=$4 -oac copy -audiofile $1.wav
    echo "Video Conversion should be complete."
fi
echo "Cleaning up."
rm -f *.jpg *.wav *.log
echo "Would you like to preview the video (N/y)?"
read preview
if [ "$preview" == "y" ]; then
    mplayer $1.avi
fi
}

echo "Please enter your wine executable name ($wine):"
read wineinput

if [ "$wineinput" != "" ]; then
    wine=$wineinput
fi

echo "Checking for the bink utilities..."
if [ -x "binkconv.exe" ]; then
   echo "Found the conversion utility."
else
    if [ -f "binkconv.exe" ]; then
        chmod +x binkconv.exe
        echo "Found conversion utility and attempted to make it executable."
        echo "If there is an error message above please CTRL-C out and make binkconv.exe executable."
    else
        echo "No conversion utility found in this folder."
        echo "Do you have the WINDOWS Bink utilities on the computer?"
        read utilitiesyes
        if [ "$utilitiesyes" == "y" ]; then
            echo "Please copy the bink utilities to this folder and restart the script."
            exit 0
        else
            echo "Would you like the script to download the program for you?"
            read download
            if [ "$download" == "y" ]; then
                echo "Downloading the file now."
                wget -c http://www.radgametools.com/down/Bink/RADTools.exe
                echo "File downloaded."
                echo "Please make sure that you install the RAD Game Tools to this folder once the installer launches."
                echo "Press enter whenever you are ready to start the installation process."
                read start
                echo "Launching the installer now.  Please wait a little while for it to launch."
		$wine -- RADTools.exe
                echo "If the program did not throw any errors everything should now be installed."
                echo "Ensuring everything is installed properly..."
                cp RADVideo/* ./
                echo "Checking for the conversion utility..."
                if [ -x "binkconv.exe" ]; then
                   echo "Found the conversion utility."
                else
                    if [ -f "binkconv.exe" ]; then
                        chmod +x binkconv.exe
                        echo "Found conversion utility and attempted to make it executable."
                        echo "If there is an error message above please CTRL-C out and make binkconv.exe executable."
                    else
                        echo "No conversion utility found in this folder."
                        echo "Please manually install the binkconv.exe executable to this folder."
                        echo "The program can be found at http://www.radgametools.com/bnkdown.htm"
                        exit 0
                    fi
                fi
            else
                echo "Please manually install the binkconv.exe executable to this folder."
                echo "The program can be found at http://www.radgametools.com/bnkdown.htm"
                exit 0
            fi
        fi
    fi
fi
      
echo "Please enter your mencoder executable name ($mencoder):"
read mencoderinput

if [ "$mencoderinput" != "" ]; then
    mencoder=$mencoderinput
fi

echo "Please enter the binkconversion utility executable name ($binkconv):"
read binkconvinput

if [ "$binkconvinput" != "" ]; then
    binkconv=$binkconvinput
fi

echo "Please enter the full path to the original bink movie files here ($path):"
read pathinput

if [ "$pathinput" != "" ]; then
    path=$pathinput
fi

echo "Linking the original movies to this location..."
ln -s $path/*.bik .
echo "Done."

echo "Checking for NWN original movies..."
echo -n "AtariLogo.bik..."
if [ -f "AtariLogo.bik" ]; then
    echo "Found."
    orig1=1
else
    echo "Missing."
    orig=0
fi

echo -n "BiowareLogo.bik..."
if [ -f "BiowareLogo.bik" ]; then
    echo "Found."
    orig2=1
else
    echo "Missing."
    orig=0
fi

echo -n "Chap1_Chap2.bik..."
if [ -f "Chap1_Chap2.bik" ]; then
    echo "Found."
    orig3=1
else
    echo "Missing."
    orig=0
fi

echo -n "Chap2_Chap3.bik..."
if [ -f "Chap2_Chap3.bik" ]; then
    echo "Found."
    orig4=1
else
    echo "Missing."
    orig=0
fi

echo -n "Chap3_Chap4.bik..."
if [ -f "Chap3_Chap4.bik" ]; then
    echo "Found."
    orig5=1
else
    echo "Missing."
    orig=0
fi

echo -n "credits.bik..."
if [ -f "credits.bik" ]; then
    echo "Found."
    orig6=1
else
    echo "Missing."
    orig=0
fi

echo -n "ending.bik..."
if [ -f "ending.bik" ]; then
    echo "Found."
    orig7=1
else
    echo "Missing."
    orig=0
fi

echo -n "fge_logo_black.bik..."
if [ -f "fge_logo_black.bik" ]; then
    echo "Found."
    orig8=1
else
    echo "Missing."
    orig=0
fi

echo -n "NWNintro.bik..."
if [ -f "NWNintro.bik" ]; then
    echo "Found."
    orig9=1
else
    echo "Missing."
    orig=0
fi

echo -n "prelude.bik..."
if [ -f "prelude.bik" ]; then
    echo "Found."
    orig10=1
else
    echo "Missing."
    orig=0
fi

echo -n "prelude_chap1.bik..."
if [ -f "prelude_chap1.bik" ]; then
    echo "Found."
    orig11=1
else
    echo "Missing."
    orig=0
fi

echo -n "WOTCLogo.bik..."
if [ -f "WOTCLogo.bik" ]; then
    echo "Found."
    orig12=1
else
    echo "Missing."
    orig=0
fi

echo "Checking for NWN: SoU movies..."
echo -n "XP1_Chap1_Chap2.bik..."
if [ -f "XP1_Chap1_Chap2.bik" ]; then
    echo "Found."
    sou1=1
else
    echo "Missing."
    sou=0
fi

echo -n "XP1_Chap2_Chap3.bik..."
if [ -f "XP1_Chap2_Chap3.bik" ]; then
    echo "Found."
    sou2=1
else
    echo "Missing."
    sou=0
fi

echo -n "XP1_Closing.bik..."
if [ -f "XP1_Closing.bik" ]; then
    echo "Found."
    sou3=1
else
    echo "Missing."
    sou=0
fi

echo -n "XP1_Intro.bik..."
if [ -f "XP1_Intro.bik" ]; then
    echo "Found."
    sou4=1
else
    echo "Missing."
    sou=0
fi

echo "Checking for HotU movies..."
echo -n "XP2_Intro.bik..."
if [ -f "XP2_Intro.bik" ]; then
    echo "Found."
    hotu1=1
else
    echo "Missing."
    hotu=0
fi

echo "If the full path to the movies you gave earlier was correct you can convert the following movies:"
if [ "$orig" == "1" ]; then
    echo "0) All of the NWN Original movies"
    movies=1
fi
if [ "$orig1" == "1" ]; then
    echo "1) AtariLogo.bik"
    movies=1
fi
if [ "$orig2" == "1" ]; then
    echo "2) BiowareLogo.bik"
    movies=1
fi
if [ "$orig3" == "1" ]; then
    echo "3) Chap1_Chap2.bik"
    movies=1
fi
if [ "$orig4" == "1" ]; then
    echo "4) Chap2_Chap3.bik"
    movies=1
fi
if [ "$orig5" == "1" ]; then
    echo "5) Chap3_Chap4.bik"
    movies=1
fi
if [ "$orig6" == "1" ]; then
    echo "6) credits.bik"
    movies=1
fi
if [ "$orig7" == "1" ]; then
    echo "7) ending.bik"
    movies=1
fi
if [ "$orig8" == "1" ]; then
    echo "8) fge_logo_black.bik"
    movies=1
fi
if [ "$orig9" == "1" ]; then
    echo "9) NWNintro.bik"
    movies=1
fi
if [ "$orig10" == "1" ]; then
    echo "10) prelude.bik"
    movies=1
fi
if [ "$orig11" == "1" ]; then
    echo "11) prelude_chap1.bik"
    movies=1
fi
if [ "$orig12" == "1" ]; then
    echo "12) WOTCLogo.bik"
    movies=1
fi
if [ "$sou" == "1" ]; then
    echo "13) All of the SoU Movies"
    movies=1
fi
if [ "$sou1" == "1" ]; then
    echo "14) XP1_Chap1_Chap2.bik"
    movies=1
fi
if [ "$sou2" == "1" ]; then
    echo "15) XP1_Chap2_Chap3.bik"
    movies=1
fi
if [ "$sou3" == "1" ]; then
    echo "16) XP1_Closing.bik"
    movies=1
fi
if [ "$sou4" == "1" ]; then
    echo "17) XP1_Intro.bik"
    movies=1
fi
if [ "$hotu" == "1" ]; then
    echo "18) All of the HotU movies"
    movies=1
fi
if [ "$hotu1" == "1" ]; then
    echo "19) XP2_Intro.bik"
    movies=1
fi
if [ "$orig" == "1" ]; then
    if [ "$sou" == "1" ]; then
        if [ "$hotu" == "1" ]; then
            echo "20) All of them"
        fi
    fi
fi
if [ "$movies" == "0" ]; then
    echo "Unfortunately I was unable to find any of the videos in this directory please make sure you specified the correct location for the original movies and restart the script."
    exit 0
fi

echo "Which of the above would you like to convert (Enter one of the above numbers)?"
read convert

if [ "$convert" == "" ]; then
    exit 0
fi

echo "Would you like to do one or two pass video?"
echo "By using two pass you will increase the quality of the file greatly although it will take twice the time."
echo "I highly recommend using two pass especially for movies like the original intro, however it can improve quality throughout all the movies."
echo "How many passes should I use? ($vpasses)"
read vpassesinput

if [ "$vpassesinput" != "" ]; then
    vpasses=vpassesinput
fi

echo "What would you like to set the bitrate to?"
echo "The bitrate can be any number you would like however here are the two I would recommend."
echo "800 - Mostly worthless quality but good if you really need to cut down on the final size"
echo "1200 - Optimum quality IMO for these movies"
echo "NOTE: The bitrate you choose now will not affect the size required to process the images it will only affect the final movie size."
echo "What would you like the bitrate to be? ($bitrate)"
read bitrateinput

if [ "$bitrateinput" != "" ]; then
    bitrate=$bitrateinput
fi

if [ "$convert" == "20" ]; then
    echo "Converting all of the movies..."
    convert=0
    all=1
fi
if [ "$convert" == "0" ]; then
    echo "Converting all of the original NWN movies..."
    convert=1
    autoincrement=1
fi
if [ "$convert" == "1" ]; then
    echo "Converting AtariLogo.bik..."
    convert AtariLogo 29.97 $vpasses $bitrate
    echo "Done converting AtariLogo.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "2" ]; then
    echo "Converting BiowareLogo.bik..."
    convert BiowareLogo 30 $vpasses $bitrate
    echo "Done converting BiowareLogo.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "3" ]; then
    echo "Converting Chap1_Chap2.bik..."
    convert Chap1_Chap2 15 $vpasses $bitrate
    echo "Done converting Chap1_Chap2.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "4" ]; then
    echo "Converting Chap2_Chap3.bik..."
    convert Chap2_Chap3 15 $vpasses $bitrate
    echo "Done converting Chap2_Chap3.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "5" ]; then
    echo "Converting Chap3_Chap4.bik..."
    convert Chap3_Chap4 15 $vpasses $bitrate
    echo "Done converting Chap3_Chap4.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "6" ]; then
    echo "Converting credits.bik..."
    convert credits 14.89 $vpasses $bitrate
    echo "Done converting credits.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "7" ]; then
    echo "Converting ending.bik..."
    convert ending 14.89 $vpasses $bitrate
    echo "Done converting ending.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "8" ]; then
    echo "Converting fge_logo_black.bik..."
    convert fge_logo_black 15 $vpasses $bitrate
    echo "Done converting fge_logo_black.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "9" ]; then
    echo "Converting NWNintro.bik..."
    convert NWNintro 29.97 $vpasses $bitrate
    echo "Done converting NWNintro.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "10" ]; then
    echo "Converting prelude.bik..."
    convert prelude 14.89 $vpasses $bitrate
    echo "Done converting prelude.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "11" ]; then
    echo "Converting prelude_chap1.bik..."
    convert prelude_chap1 14.89 $vpasses $bitrate
    echo "Done converting prelude_chap1.bik"
    if [ "$autoincrement" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "12" ]; then
    echo "Converting WOTCLogo.bik..."
    convert WOTCLogo 30 $vpasses $bitrate
    echo "Done converting WOTCLogo.bik"
    if [ "$all" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "13" ]; then
    echo "Converting all of the SoU movies..."
    convert=14
    autoincrement=2
fi
if [ "$convert" == "14" ]; then
    echo "Converting XP1_Chap1_Chap2.bik..."
    convert XP1_Chap1_Chap2 15 $vpasses $bitrate
    echo "Done converting XP1_Chap1_Chap2.bik"
    if [ "$autoincrement" == "2" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "15" ]; then
    echo "Converting XP1_Chap2_Chap3.bik..."
    convert XP1_Chap2_Chap3 15 $vpasses $bitrate
    echo "Done converting XP1_Chap2_Chap3.bik"
    if [ "$autoincrement" == "2" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "16" ]; then
    echo "Converting XP1_Closing.bik..."
    convert XP1_Closing 15 $vpasses $bitrate
    echo "Done converting XP1_Closing.bik"
    if [ "$autoincrement" == "2" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "17" ]; then
    echo "Converting XP1_Intro.bik..."
    convert XP1_Intro 15 $vpasses $bitrate
    echo "Done converting XP1_Intro.bik"
    if [ "$all" == "1" ]; then
        convert=$(($convert+1))
    fi
fi
if [ "$convert" == "18" ]; then
    echo "Converting all of the HotU movies..."
    convert=19
    autoincrement=3
fi
if [ "$convert" == "19" ]; then
    echo "Converting XP2_Intro.bik..."
    convert XP2_Intro 29.97 $vpasses $bitrate
    echo "Done converting XP2_Intro.bik"
fi

echo "The selected file or files were converted (hopefully) successfully."
echo "Please rerun the script if you would like to convert more movies."

exit 0

