#!/usr/bin/env bash

# Create a new port
# Copyright (c) 2012, Matthew Reschke
# mReschke 2013-08-27

bin="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

input=$1
port="${input##*/}"
path="${input:0:${#input} - ${#port}}"; path="${path%?}"
repo="${path##*/}"
ver=$2
file=$3
date=`date "+%Y-%m-%d %H:%M:%S"`

if [ "$repo" == '' ] || [ "$port" == '' ] || [ "$ver" == '' ]; then
	echo "Usage:"
	echo "  create repo/name version [file]"
	echo "  create lfs-7.0/bc 1.32 http://example.com/bc-1.32.tar.gz"
	echo "  create lfs-7.0/bc 1.32 ./bc-1.32.tar.gz"
	echo
	echo "  Notice file can be local or from http"
	echo "  File is optional"
	echo 
	exit 1
fi
if [ -e "$repo/$port/PORTBUILD" ]; then
	echo "$repo/$port Already Exists.  Use the package tool to update existing ports"
	exit 1
fi

echo "Repo: $repo"
echo "Port: $port"
echo "Version: $ver"
echo "File: $file"
echo
echo -n "Please enter a port description: "; read desc
echo

if [ "$file" != "" ]; then
	filename="${file##*/}"
else
	filename="$port-$ver.tar.gz"
	mkdir -p $repo/$port/source/$port-$ver
	ln -s $port-$ver $repo/$port/source/$port-current
fi
if [ -d "$repo" ]; then
	mkdir -p $repo/$port/
	ln -s ../../package $repo/$port/
	if [ "$file" != "" ]; then
		if [[ "$file" == *"http"* ]]; then
			echo "Downloading $file to $repo/$port/"
			wget $file -O $repo/$port/$filename
		else
			echo "Copying $file to $repo/$port/"
			/bin/cp -rf $file $repo/$port/
		fi
	fi

	portbuild="$repo/$port/PORTBUILD"
	echo "Creating $portbuild File"

	cat > $portbuild << "EOF"
#!/usr/bin/env bash

# PORTBUILD generated by manup packaging scripts on {{date}}
# Visit http://manup.mreschke.net/README
# Copyright (c) 2012, Matthew Reschke

##############################################################################################
# Variables below can NOT reference other variables, manup will not expand them.
# So url=http://example.com/source/$name-$ver is not acceptable.
# Though all variables can be accessed in the build_* functions.
# The ONLY exception to this rule is the config=(...) array, where $prefix is available

# There are also more bash variables available than you see here.
# The /usr/local/lib/manup/functions.sh is sourced before this file. Check it out.
# There are also a few set by manup, like $prefix, $fakeroot, $is_installed, $installed_ver
# Visit http://manup.mreschke.com for details

# All variables and build_* function must be present, even if empty.
# Since a bash function cannot be empty, just put in some placeholder
# like null=nothing

# These files will be overwritten with every 'manup sync' command.
##############################################################################################


# Port Information
name=""
ver=
rel=
desc="Description Here"

# Port Dependencies
# dep=(someport anotherport) are required dependencies
# depopt=(someport anotherport) are optional dependencies
dep=()
depopt=()

# Port Configuration Files
# These files will NOT be overwritten during port upgrade but instead will be copied as xxxx.manupnew files
# This avoids your defined config files from being overwritten during upgrades
# configs=(/etc/myport/my.conf, /etc/defaults/myport, $prefix/share/myother.conf)
configs=()

# Port file details
fileurl=""
filesize=
md5sum=""

# Packaging Options
keep_symlinks=false


##############################################################################################
# In all of these function, your current directory is the tmp build directory,
# which is usually /tmp/manup/yourrepo/yourpackage/package-ver/
# After port_fakeroot runs the code to be deployed is copied to $fakeroot
##############################################################################################

port_configure() {
	# Configure the source code (use manup $prefix variable)
	#./configure --prefix=$prefix
:;}

port_build() {
	# Build/Compile the source code
	#make
	#make TARGET=linux2628 USE-STATIC_PCRE=1 USE_OPENSSL=1
:;}

port_fakeroot() {
	# Install all package contents into $fakeroot
	# If port is source code, then instead of using 'make install' use the included Auto-DESTDIR (make-redir) tool
	# A helper function called make_install will perform this action: make-redir DESTDIR=$fakeroot PREFIX=$prefix install
	# You can always run the make-redir command yourself if your install requires more flags:
	# make-redir MAKDIR=/usr/share/man OTHERFLAGSHERE=/something DESTDIR=$fakeroot PREFIX=$prefix install
	#make_install

	# If not source code and no make install is required then just copy the files into $fakeroot
	# A helper function called copy_to_fakeroot is used, this just runs: tar cf - . | (cd $fakeroot ; tar -xf -)
	#copy_to_fakeroot
:;}

port_preinstall() {
	# Perform any pre-install functions
	# Your current directory at this point is the build dir (/tmp/manup/somerepo/yourpackage/package-ver/)
	# Your files are pending in $fakeroot, so 'cd $fakeroot' to mod files before install...
:;}

port_install() {
	# Install all package contents from $fakeroot into /
	# install_files is helper function to do this, it simply runs: cd $fakeroot; tar cf - . | (cd / ; tar -xvhf -)
	# Your current directory at this point is the build dir (/tmp/manup/somerepo/yourpackage/package-ver/)
	# The $is_installed or $installed_ver variables are handy here to determine if fresh install or upgrading...
	#install_files
:;}

port_postinstall() {
	# Perform any post-install functions
	# There are output helper functions like out, error, notice, message
	# The $is_installed or $installed_ver variables are handy here to determine if fresh install or upgrading...
	#if $is_installed; then echo "You were just upgraded to from $installed_ver"; fi
	#if [ "$installed_ver" == "1.0.2" ]; then echo "I am going to perform 1.0.2 to 1.0.3 upgrade code now"; fi
	#notice "Please update this and that important thing"
	#message "Installed, Thanks!"
:;}

port_preuninstall() {
	# Perform any pre-uninstall functions
:;}

port_postuninstall() {
	# Perform any post-uninstall functions
:;}

##############################################################################################
EOF

	sed -i "s/{{date}}/$date/g" $portbuild
	sed -i "s/name=.*/name=\"$port\"/g" $portbuild
	sed -i "s/ver=.*/ver=$ver/g" $portbuild
	sed -i "s|fileurl=.*|fileurl=\"http://mreschke.net/$repo/$port/$filename\"|g" $portbuild
	if [ "$file" != "" ]; then
		sed -i "s/filesize=.*/filesize=$(stat -c%s $repo/$port/$filename)/g" $portbuild
		sed -i "s/md5sum=.*/md5sum=\"$(md5sum $repo/$port/$filename | cut -d\  -f1)\"/g" $portbuild
	fi
	sed -i "s/desc=.*/desc=\"$desc\"/g" $portbuild

	#echo
	#./buildtree $repo #NO because PORTBUILD is not ready yet, needs manually modified

	echo
	echo "You need to manually modify the new $portbuild file to your specifications"
	echo "Once you update your PORTBUILD you can run ./buildtree $repo from here"
	echo "Create port complete!"

else
	echo "Repo folder does not exists.  This is meant to be run from manup root"
	exit 1
fi
