########## Let The Magic Start ###############
#      |||           ___           )))       #
#     (o o)         (o o)         (o o)      #
# ooO--(_)--Ooo-ooO--(_)--Ooo-ooO--(_)--Ooo- #
##############################################
#                By CoolZero                 #
############################################## 

#################################################################################################

# This Tool can be used to install local Apps on your badge. 

# Enter your App Name
# 1. Replace the spaceholder for the name of your app, change the default name: "newapp"

# Add Your App Code:

# 1. First copy this scipt into your editor.
# 2. Pasted your code of your app that you want to install localy into this script. 
# 3. So change the code beween "## !!! REPLACE ME !!! ##" with your code.

# Install App Steps

# 0. Copy the total script.
# 1. Connect your badge to your computer.
# 2. Open a Terminal/Putty and conntect to your Badge.
# 3. Enter into the Python Shell.
# 4. Press Ctrl + E.
# 5. Pasted the just copied script.
# 6. Press Ctrl + D.
# 7. Press Ctrl + D.
# 8. Done.

# Now you can see your new app between your apps.

#################################################################################################
import os
import uos

# App Name
appname='newapp'

# content:
s = """\
## !!! REPLACE ME !!! ##
import rgb
rgb.scrolltext("!!! RTFM !!!", (0,0,255))
## !!! REPLACE ME !!! ##
\
"""
################################################################################################

## FUNCTION: Check Folder ##########################################

def exists(path):
    """Test whether a path exists.  Returns False for broken symbolic links"""
    try:
        os.stat(path)
    except OSError:
        return False
    return True

#########################################################

#########################################################
# check if directory exists
#########################################################

if not exists('/apps/'+appname):
    #print("not exists")

    # make new dir /apps/newapp
    uos.mkdir("/apps/"+appname)

    # create empty file
    open('/apps/'+appname+'/__init__.py', 'a').close()
#else:
#    print("exists")

#########################################################
# check if file exists
#########################################################

# add content:
fo = open('/apps/'+appname+'/__init__.py','w')
fo.write(s)
fo.close()

## show dir: 
uos.listdir("./apps/"+appname)

## show file content:
print(open('./apps/'+appname+'/__init__.py').read())