#!/bin/bash
help="Usage:
\$ cd sourcecode/directory
\$ autobuild [install-path] [OPTIONS]
Or:
\$ autobuild [install-path] [OPTIONS] --tarball/zip http://url.to_tarball_or_zip.package

Options:
--static[-only]    Compile static libraries
--install          Automatically install as well
--tarball FILENAME Build from the FILENAME tarball
--zip FILENAME     Build from the FILENAME zip
--cross            Cross compile for windows using MinGW
--config FLAGS     Additional flags to pass to configuration scripts
--force STEP       Forces to continue even if a step reports failure
                   (accepts bootstrap,config,make. Separate with ,)
--uninstaller      Generate an uninstall script by first installing
                   to ./prefix (Unstable)
--help             Show this help text"

# Some function definitions, mostly wrappers
################
function configmake {
prefixpicket="$(echo -n "$1" | sed -e "s/\//\\\\\//g")"
makefiles="$(ls -1 . | sed -n -e "/^Makefile/p")"
while [ "$makefiles" != "" ]; do
makefile="$(echo -n "$makefiles" | sed -n -e "1p")"
makefiles="$(echo -n "$makefiles" | sed -e "1d")"
data="$(cat "$makefile" | sed -e "s/ \/usr/ $prefixpicket/g;/install /{s/ -o root/ -o $(whoami)/g;s/ -g root/ -g $(whoami)/g;}")"
echo -n "$data" > "$makefile"
done
}

function exitwrap {
if [ "$(echo -n "$force" | sed -n -e "/,${1},/p")" = "" ]; then
echo "Build failed at the ${1} step. If this seems incorrect (that the build could continue anyway) add '--force ${1}' to the command"
exit
else
echo "WARNING: Build reported failure at the ${1} step (overriding)"
fi
}

function makewrap {
if [ "$(echo -n "$force" | sed -n -e "/,make,/p")" = "" ]; then
make $@
else
make -k $@
fi
}

function jamwrap {
if [ "$(echo -n "$force" | sed -n -e "/,make,/p")" = "" ]; then
jam -q $@
else
jam $@
fi
}

# Set some default values
################
args="$@"
if [ "$(whoami)" = "root" ]; then
prefix="/usr"
else
prefix="./prefix"
fi
prefix2="$prefix" # for --uninstaller
uninstaller="false"
static=""
install="false"
tarball=""
zipfile=""
config=""
force=""

# Handle arguments
################
while [ "$args" != "" ]; do
arg="$(echo -n "$args" | sed -e "s/ .*//;")"
args="$(echo -n "$args" | sed -e "s/^ *[^ ]* *//;")"
if [ "$arg" = "--help" ]; then echo "$help"; exit
elif [ "$arg" = "-h" ]; then echo "$help"; exit
elif [ "$arg" = "--install" ]; then install="true"
elif [ "$arg" = "--static" ]; then static="--enable-static --enable-shared"
elif [ "$arg" = "--static-only" ]; then static="--enable-static --disable-shared"
elif [ "$arg" = "--tarball" -o "$arg" = "--tar" -o "$arg" = "--tb" ]; then
  tarball="$(echo -n "$args" | sed -e "s/ .*//;")"
  args="$(echo -n "$args" | sed -e "s/^ *[^ ]* *//;")"
elif [ "$arg" = "--zip" ]; then
  zipfile="$(echo -n "$args" | sed -e "s/ .*//;")"
  args="$(echo -n "$args" | sed -e "s/^ *[^ ]* *//;")"
elif [ "$arg" = "--cross" ]; then
  config="${config} --host=\"$(ls -1 /usr/bin | sed -n -e "/mingw.*-g++\$/{s/-g++\$//;p;q;}")\""
elif [ "$arg" = "--config" ]; then
  config="${config} $(echo -n "$args" | sed -e "s/ .*//;")"
  args="$(echo -n "$args" | sed -e "s/^ *[^ ]* *//;")"
elif [ "$arg" = "--force" ]; then
  force="${force},$(echo -n "$args" | sed -e "s/ .*//;")"
  args="$(echo -n "$args" | sed -e "s/^ *[^ ]* *//;")"
elif [ "$arg" = "--uninstaller" ]; then
  uninstaller="true"
  prefix="$(pwd)/prefix"
else prefix="$arg"
fi
done

# [Download and ]unpack tarball, then launch a new script from that
################
if [ "$tarball" != "" ]; then
ext="$(echo -n "$tarball" | sed -e "s/^.*\.//")"
compression=""
# Check which compression the tarball has
if [ "$ext" = "gz" -o "$ext" = "tgz" ]; then compression="z"
elif [ "$ext" = "bz2" -o "$ext" = "tbz" -o "$ext" = "tbz2" ]; then compression="j"; fi
protocol="$(echo -n "$tarball" | sed -e "s/:.*//")"
# Check if the given path was an URL, if so download with wget
if [ "$protocol" = "http" -o "$protocol" = "https" -o "$protocol" = "ftp" ]; then
wget -c "$tarball"
tarball="$(echo -n "$tarball" | sed -e "s/.*\///")"
fi
# Unpack and enter directory
directory="$(tar -t${compression}f "$tarball" | sed -e "s/\/.*//;q")"
tar -x${compression}f "$tarball"
cd "$directory"
# Re-launch this script
command="$(echo -n "$0" | sed -e "s/^[^\/]/..\/&/") ${prefix}"
if [ "$prefix" != "" ]; then command="${command} ${prefix2}"; fi
if [ "$install" = "true" ]; then command="${command} --install"; fi
if [ "$static" = "--enable-static --enable-shared" ]; then command="${command} --static"; fi
if [ "$static" = "--enable-static --disable-shared" ]; then command="${command} --static-only"; fi
if [ "$config" != "" ]; then command="${command} --config '${config}'"; fi
if [ "$force" != "" ]; then command="${command} --force '${force}'"; fi
if [ "$uninstaller" != "" ]; then command="${command} --uninstaller"; fi
eval "$command"
exit
fi

# [Download and ]unpack zipfile, then launch a new script from that
################
if [ "$zipfile" != "" ]; then
ext="$(echo -n "$zipfile" | sed -e "s/^.*\.//")"
protocol="$(echo -n "$zipfile" | sed -e "s/:.*//")"
# Check if the given path was an URL, if so download with wget
if [ "$protocol" = "http" -o "$protocol" = "https" -o "$protocol" = "ftp" ]; then
wget -c "$zipfile"
zipfile="$(echo -n "$zipfile" | sed -e "s/.*\///")"
fi
# Unpack and enter directory
directory="$(unzip -qql "$zipfile" | sed -e "s/.* //" | sed -e "s/\/.*//;q")"
echo A | unzip "$zipfile"
cd "$directory"
# Re-launch this script
command="$(echo -n "$0" | sed -e "s/^[^\/]/..\/&/")"
if [ "$prefix" != "" ]; then command="${command} ${prefix2}"; fi
if [ "$install" = "true" ]; then command="${command} --install"; fi
if [ "$static" = "--enable-static --enable-shared" ]; then command="${command} --static"; fi
if [ "$static" = "--enable-static --disable-shared" ]; then command="${command} --static-only"; fi
if [ "$config" != "" ]; then command="${command} --config '${config}'"; fi
if [ "$force" != "" ]; then command="${command} --force '${force}'"; fi
if [ "$uninstaller" != "" ]; then command="${command} --uninstaller"; fi
eval "$command"
exit
fi

# Do the build process
################
force=",${force},"
if [ "$(echo "$prefix" | sed -e "/^[\/~]/d")" != "" ]; then prefix="$(pwd)/${prefix}"; fi
export PATH="${prefix}/bin:${PATH}"
export LD_LIBRARY_PATH="${prefix}/lib:${LD_LIBRARY_PATH}"
export LIBRARY_PATH="${prefix}/lib:${LD_LIBRARY_PATH}"
export PKG_CONFIG_PATH="${prefix}/lib/pkgconfig:${PKG_CONFIG_PATH}"
if [ -e "./autogen.sh" ]; then chmod +x ./autogen.sh; ./autogen.sh || exitwrap bootstrap
elif [ -e "./bootstrap.sh" ]; then chmod +x ./bootstrap.sh; ./bootstrap.sh || exitwrap bootstrap
elif [ -e "./bootstrap" ]; then chmod +x ./bootstrap; ./bootstrap || exitwrap bootstrap
elif [ -e "./bin/autogen.sh" ]; then chmod +x ./bin/autogen.sh; ./bin/autogen.sh || exitwrap bootstrap
elif [ -e "./bin/bootstrap.sh" ]; then chmod +x ./bin/bootstrap.sh; ./bin/bootstrap.sh || exitwrap bootstrap
elif [ -e "./bin/bootstrap" ]; then chmod +x ./bin/bootstrap; ./bin/bootstrap || exitwrap bootstrap
elif [ -e "./configure.ac" ]; then autoreconf -isf || exitwrap bootstrap
fi
configured="false"
if [ -e "./configure" ]; then chmod +x ./configure; eval "./configure --prefix=\"${prefix}\"${static}${config}" || exitwrap config; configured="true"
elif [ -e "./CMakeLists.txt" ]; then cmake . || exitwrap config
fi
if [ -e "./Jamfile" ]; then jamwrap || exitwrap make; if [ "$install" = "true" ]; then jamwrap install; else echo "Type 'jam install' if you want to install the software (to ${prefix})"; fi
elif [ -e "./Makefile" ]; then if [ "$configured" = "false" ]; then configmake "$prefix"; fi; makewrap || exitwrap make; if [ "$install" = "true" ]; then makewrap install; else echo "Type 'make install' if you want to install the software (to ${prefix})"; fi
# TODO: Turn this into a loop or something instead:
elif [ -e "./compile.py" ]; then python compile.py
elif [ -e "./build.py" ]; then python build.py
elif [ -e "./compile.rb" ]; then ruby compile.rb
elif [ -e "./build.rb" ]; then ruby build.rb
elif [ -e "./compile.pl" ]; then perl compile.pl
elif [ -e "./build.pl" ]; then perl build.pl
elif [ -e "./compile.sh" ]; then chmod +x ./compile.sh; ./compile.sh
elif [ -e "./build.sh" ]; then chmod +x ./build.sh; ./build.sh
elif [ -e "./compile" ]; then chmod +x ./compile; ./compile
elif [ -e "./build" ]; then chmod +x ./build; ./build
else echo "$help"
fi

# Uninstaller
################
if [ "$uninstaller" = "true" ]; then
(
echo "#!/bin/sh"
echo "cd \"${prefix2}\""
cd "$prefix"
files="$(ls -R1)"
dir="./"
while [ "$files" != "" ]; do
file="$(echo -n "$files" | tail -n 1)"
files="$(echo -n "$files" | head -n -1)"
if [ "$(echo "$file" | grep ":")" = "" ]; then
echo "rm ${dir}${file}"
else
dir="$(echo -n "$file" | sed -e "s/:\$/\//")"
echo "rmdir ${dir}"
fi
done
) > uninstall.sh
cp -R ${prefix}/* ${prefix2}
fi
