#!/bin/bash

currentPath=$(cd "$(dirname "$0")";pwd)
currentPath=`echo $currentPath | sed "s/\/Release_ios/""/g"`

# 模块名称
moduleName=`echo $currentPath | awk -F "/" '{print $NF}'`
# .git 路径
gitPath="${currentPath##Release_ios}/.git"  ${str#*1}

# 组件文件
podspecPath="$currentPath/$moduleName.podspec"

# 组件版本
version=`awk '/s.version          =/{print}' $podspecPath`
# 字符串分隔提取组件版本号
version=`echo $version | awk -F "=" '{print $NF}'`
version=`echo $version | awk -F "'" '{print $2}'`
echo $version

# 组件库地址
gitRepository="git@172.16.6.11:xupd/framework/ios/${moduleName}.git"

# 是否初始化过git
if [ ! -d "$gitPath" ]; then
        echo "=======> 初始化Git仓库"
        git init
        git remote add origin $gitRepository
        remoteUrl=`git remote -v`
        echo "$remoteUrl"
        git add .
        git commit -m "Initial commit"
        git push -u origin master
else
        echo "=======> Git仓库已存在，无需初始化"
fi

# 是否配置了远程仓库地址
remoteUrl=`git remote -v`
if [ -z "$remoteUrl" ]; then
    # 未指定git地址
echo "=======> 未配置git远程地址，请参照例子配置: git remote add origin $gitRepository"
else
echo "=======> 当前远程git地址: "
echo "=======> $remoteUrl"
fi

# 查看分支是否存在
branch=$(git branch | grep devlop)
if [ -n "$branch" ]; then
    echo "=======> exits"
else
    echo "=======> 创建Develop分支"
    git checkout -b devlop master
fi

echo "=======> 创建tag ${version}"
git tag -a $version -m "${moduleName} (${version})"
gittag=`git push origin tag "${version}"`
gittag

echo "Git 命令执行完成。。。"