#!/bin/ksh
#
# HowTo: build ffmpeg on OSX (Lion 10.7.5) Java 8, NetBeans 8.0.2
#   These scripts use korn shell, not bash!
#   Derived from: CompilationGuide/MacOSX – FFmpeg - https://trac.ffmpeg.org/wiki/CompilationGuide/MacOSX
#   ?? compile with this (from: https://evermeet.cx/ffmpeg/) :
#    configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --as=yasm --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzmq --enable-version3 --disable-ffplay --disable-indev=qtkit --disable-indev=x11grab_xcb
#
#
# 1 - This MUST be run by an admin account
# 2 - System software & tools MUST be up to date. Use 'brew doctor' below to check
#
# PATH is critical:
#   If you have any of these components in it, they MUST be in this order.
#   Others may be inserted anywhere. "~/bin" is optional if you override standard commands.
#
PATH=~/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/usr/local/git/bin
#
# Prerequisites:
#   Xcode 4.6.3 ( Mountian Lion, Lion ) version will vary by OS
#     https://developer.apple.com/downloads  # search: "xcode 4.6.3"
#   NetBeans 8.0.2 + Java 8
#     http://www.oracle.com/technetwork/java/javase/downloads/jdk-netbeans-jsp-142931.html
#   homebrew
      ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
#
#
################################################################################
# check for updates first:
    brew doctor
#     A lot of warnings, look for "bad" stuff or continue & fix what breaks
#     Note: Xcode may report as 4.6 - ignore the warning
#
# unset DYLD_LIBRARY_PATH ??
#
################################################################################
#
# Install ffmpeg & all the dependencies
#
    brew install ffmpeg
    #--brew uninstall ffmpeg   # may not get all the libraries, so uninstall & do again
    brew link --overwrite pkg-config    # ???? forgot why required
    # 1 - Error due to OSX "port" detection:
    #    Warning: It appears you have MacPorts or Fink installed.
    #    Software installed with other package managers causes known problems for
    #    Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again.
    #      ./Library/Logs/Homebrew/libvorbis/config.log : missing ac_nonexistent.h
    #  2 - brew rm -f `brew list` # will force a download 
#
####  Now build & fix until successful:
#
    brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-libvorbis  --with-opus --with-x265
# output: /usr/local/Cellar/ffmpeg/2.6.2/bin/ffmpeg  # but will not work for UMS, don't try
#
###################################
#
# Now the mods:
#
# *** Mandatory change: ffmpeg.rb:74  --enable-shared => --enable-static
    ( cd /usr/local/Library/Formula \
        && sed -e 's/--enable-shared/--enable-static/' < ffmpeg.rb > xx.$$ \
          && mv -f xx.$$ ffmpeg.rb )
#
# libvps will not build. add "--with-libvpx" later when we figure out how to fix it!!
#
# --enable-static does not work on brew command (yet??), it ignores it or I don't know how:
# ?? what other libs need to be added ??
#
    brew install ffmpeg --enable-static --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-libvorbis  --with-opus --with-x265
# output: /usr/local/Cellar/ffmpeg/2.6.2/bin/ffmpeg
#
# copy to the UMS build area and make it writable (why??) or NB fails.
# Copy it from a user account (NOT admin) into your NB project:
#
    cp -f /usr/local/Cellar/ffmpeg/2.6.2/bin/ffmpeg ./target/bin/osx/
    chmod u+w ./target/bin/osx/ffmpeg




