#!/usr/bin/env bash

# PORTBUILD generated by manup packaging scripts on 2014-10-13 12:35:55
# 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="jessie-base"
ver=1.0
rel=10
desc="Debian 8.0 Jessie Base"

# Port Dependencies
# dep=(someport anotherport) are required dependencies
# depopt=(someport anotherport) are optional dependencies
dep=(profile-linux-all vimrc)
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="http://manup.mreschke.net/nwq/jessie-base/jessie-base-1.0-10.tar.gz"
filesize=123
md5sum="37dec6fbba6075f07ce6c42648946303"

# 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


# This script can only be installed once per system
if $is_installed; then
	notice "This package already installed and cannot be installed twice.  This is a one time initial build script."
	return 1
fi


# Configure apt
out "Configuring apt"
echo "Acquire::Languages \"none\";" > /etc/apt/apt.conf.d/99translations
cat > /etc/apt/sources.list << "EOF"
deb http://ftp.us.debian.org/debian jessie main contrib non-free
deb-src http://ftp.us.debian.org/debian jessie main contrib non-free
 
deb http://ftp.debian.org/debian/ jessie-updates main contrib non-free
deb-src http://ftp.debian.org/debian/ jessie-updates main contrib non-free
 
deb http://security.debian.org/ jessie/updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free
EOF
apt-get update -y


# Install additional base apps used by any wheezy system (desktop, server, vm...)
out "Installing base apps"
apt-get dist-upgrade -y
apt-get install -y \
	sudo openssh-server vim rsync curl htop mlocate python php5-cli apt-transport-https


# Configure Users and Profiles
out "Configuring Profiles"
#primaryUser=$(cat /etc/passwd|grep 1000:1000 | cut -d: -f1)
echo "" >> /root/.bashrc
echo "# Custom Profiles" >> /root/.bashrc
echo "source /etc/nwq/profile.bashrc" >> /root/.bashrc
for user in /home/*
do
	out "Configuring $user"
	username="${user##*/}"
	echo "" >> $user/.bashrc
	echo "# Path" >> $user/.bashrc
	echo "PATH=~/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" >> $user/.bashrc
	echo "" >> $user/.bashrc
	echo "# Custom Profiles" >> $user/.bashrc
	echo "source /etc/nwq/profile.bashrc" >> $user/.bashrc
	adduser $username sudo #add user to sudo group
done
echo "%sudo ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/sudo
chmod 0440 /etc/sudoers.d/sudo


# Configure SSH
echo "" >> /etc/ssh/sshd_config
echo "# mReschke" >> /etc/ssh/sshd_config
echo "ClientAliveInterval 120" >> /etc/ssh/sshd_config
sed -i 's/#   StrictHostKeyChecking ask/StrictHostKeyChecking no/g' /etc/ssh/ssh_config 
echo "    UserKnownHostsFile /dev/null" >> /etc/ssh/ssh_config
echo "    LogLevel ERROR" >> /etc/ssh/ssh_config
service ssh restart


# Sample fstab
out "Sample fstab"
echo "" >> /etc/fstab
echo "#Sample NFS" >> /etc/fstab
echo "#xenstore:/store /store nfs defaults,hard,intr 0 0" >> /etc/fstab


# Sample interfaces
out "Sample Interfaces"
cat >> /etc/network/interfaces << "EOF"

# Sample Static
#auto eth0
#iface eth0 inet static
#	address 192.168.1.101
#	netmask 255.255.255.0
#	network 192.168.1.0
#	broadcast 192.168.1.255
#	gateway 192.168.1.1

# Sample Bridge
#auto eth0
#iface eth0 inet manual
#	auto br0
#	iface br0 inet static
#		address 10.0.0.1
#		netmask 255.255.248.0
#		network 10.0.0.0
#		broadcast 10.0.7.255
#		gateway 10.0.0.1
#		bridge_ports eth0

# Sample Bond
#auto bond0
#iface bond0 inet static
#    address 10.10.10.1
#    netmask 255.255.255.0
#    slaves eth3 eth4
#    bond-mode balance-rr
#    bond-miimon 100
#    bond-downdelay 200
#    bond-updelay 200
#    mtu 9000
EOF



:;}

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!"

	apt-get -y autoremove
	apt-get -y clean

	message "Setup Complete!"

	message "Some other software you may want to install:"

	out "iotop nmap dnsutils sysstat smartmontools hdparm"
	out "nfs-common cifs-utils nfs-kernel-server ifenslave-2.6 bridge-utils unzip"
	out "links lynx ntp ntpdate multitail"
	echo
	out "If KDE, install kde-config-gtk-style gtk2-engines-oxygen gtk3-engines-oxygen"
	echo
    out "If laptop, install kde-config-touchpad (if KDE), laptop-mode-tools"

:;}

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

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

##############################################################################################
