#!/bin/bash

set -e

version="1.2"
PROGNAME=$0

die() {
    echo "$PROGNAME: $*" >&2
    exit 1
}

echop() {
    echo "${@}" >&2
}


usage() {
    if [ "$*" != "" ] ; then
        echo "Error: $*"
    fi

    cat << EOF
Usage: $PROGNAME -f SHELL_SCRIPT -d DESCRIPTION_TO_SHOW_ON_PY_PI
Madhavth shell script package creator for pyPi
Options:
-h, --help                 display this usage message and exit
-s, --setup 				   creates setup.py and make ready for build
-u, --upload			   upload package to pypi
-d, --description 		  description for package
-f, --full              creates a package under pip_packages and creates setup as well as build
-v, --version         version
-a, --author          author name for package/ default is madhavth :D
-i, --increase 		 increase version number
EOF
    exit 1
}

setup=0
upload=0
desc=""
full=1

check_if_flag_args()
{
  if [[ $2 == "-"* ]]; then
    usage "Not a valid argument for $1"
  fi
}

function update_version()
{

file=$1

if [[ -z $file ]]; then
    echo "give a file to update version number"
    exit 1
fi

old=$(cat $file | grep -Eo -m 1 'version="(.*)"$')

if [[ -z $old ]]; then
	echo "file must contain a version variable set as version=\"VERSION_NUMBER\""
	exit 1
fi

eval $old_version_string

new_version=$(awk "BEGIN {print $version + 0.1}")

sed "0,/$old/s//version=\"$new_version\"/" $file > ${file}.temp.shellpackager

rm $file

mv ${file}.temp.shellpackager $file
chmod a+x $file
echo "updated version from $version to $new_version"

}


REM_ARGS=()

while [ $# -gt 0 ] ; do
    case "$1" in
      -v|--version)
      echo "version is $version"
      exit 0
      ;;


	 -i|--increase)
		check_if_flag_args $1 $2
		update_version $2
		exit 0
		;;


    -h|--help)
        usage
        ;;

    -d|--description)
        check_if_flag_args $1 $2
        desc="$2"
        shift
        ;;

    -a|--author)
      check_if_flag_args $1 $2
      author="$2"
      shift
      ;;

	-s|--setup)
		setup=1
    full=0
		;;

	-u|--upload)
		upload=1
    full=0
		;;

  -f|--full)
      full=1
    ;;

    -*)
    usage "Unknown option $1"
      ;;

    *)
        REM_ARGS+=" $1"
      ;;
    esac
    shift
done

#echop "REM_ARGS are ${REM_ARGS[@]}"
package=${REM_ARGS[0]}
package="$(echo ${package//[[:blank:]]/})"

if [[ -z "$package" ]]; then
  echo "script not provided.. please provide a script"
  exit 1
fi


if [[ -f "$package" ]]; then
  echo "processing..."
else
  echo "file $package doesnt exists"
  exit 1
fi


function create_setup()
{
package=$1
description=$2

echo -e "\
  just a simple project
" > README.md


cat <<EOT> setup.py
#setup.py
from setuptools import setup
import re

#change version number in $package file  add/edit version="DESIRED_VERSION NUMBER" at the top of file

with open("README.md", "rb") as f:
    long_descr = f.read().decode("utf-8")

version_read = re.search(
    '^version\s*=\s*"(.*)"',
    open('$package').read(),
    re.M
)

if version_read is not None:
    version = version_read.group(1)
else:
    version = "0.1"


setup(
    name='$1',
    scripts=['$1'],
    version= "0.1",
    description = '${description:-"simple project, simple life"}',
    long_description = 'just a humble project',
    author = '${author:-madhavth}'
)
EOT

# echo -e "\
# metadata]
# name = $package
# version = 0.1
# author = madhavth
# description = A small example package
# long_description = file: README.md
# long_description_content_type = text/markdown
# " > setup.cfg

}


function twine_up()
{
file=$1
dont=$2

has_version="0"

if [[ $(ls dist/$file-${version}.tar.gz) ]]; then
  has_version="1"
fi

rm -rf dist

python3 setup.py bdist_wheel | grep $file
python3 setup.py sdist | grep $file


if [[ -z $dont ]]; then
  if [[ $has_version == "1" ]]; then
    update_version $file
  fi

twine upload dist/* --verbose
fi
}

home=~
location="$home/test/my_pip_packages"
packagelocation="$location/${package}-package"

if [[ "$full" == "1" ]]; then
  mkdir -p "$packagelocation"
  cp $package $packagelocation
  cd "$packagelocation"
  create_setup $package $desc
  twine_up $package "dont_ty"
  echo ""
  echo -e '\nHit [Ctrl]+[D] to exit this child shell.'
  echo " -------------------------------"
  echo "created required files for PyPI in directory ${packagelocation}"
  echo "---------------------------------------------------"
  echo "---------------------------------------------------"
  echo "upload package to PyPI from this directory by using the following command"
  echo "shellpackager -u $package"
  echo "---------------------------------------------------"
  echo "---------------------------------------------------"
  echo ""
  $SHELL
fi


if [[ "$setup" == "1" ]]; then
  create_setup $package $desc
  cd "$packagelocation"
fi

if [[ "$upload" == "1" ]]; then
  twine_up $package
  pip install dist/*.tar.gz
fi
