I'm making a gui application which will control my laptop in order to make it act as a wifi hotspot. I may change distributions so I want to find a solution which will work generally for all distributions. I am currently doing this on Fedora 17.
Using NetworkManager's "Use as Hotspot" button works very well to create an ad-hoc hotspot:

I want to recreate this functionality with terminal commands.
Below are two methods I tried to accomplish this but I havn't found a solution yet.
Method 1: iwconfig
Using research from accross the web, I tried the following command:
# ifconfig wlan0 10.42.0.1 netmask 255.555.255.0 broadcast 10.42.0.255 up
# iwconfig wlan0 essid my-lappy mode ad-hoc key 0123456789
# iptables-restore < saved-hotspot-iptables
# echo 1 > /proc/sys/net/ipv4/ip_forward
# dhclient wlan0
where saved-hotspot-iptables is a file which I generated with iptables-save > saved-hotspot-iptables with the iptables set by the "Use as Hotspot" function.
Method 1 Problem
Devices can see the SSID but are unable to connect. Running ip a reveals the following about wlan0:
...
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state DORMANT qlen 1000
...
Notice NO-CARRIER is present, and the state is DORMANT instead of UP.
Running ifconfig wlan0 shows the following:
...
wlan0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
...
Notice that RUNNING is missing as one of the flags.
Method 2: nmcli
If NetworkManager's "Use as Hotspot" button is pressed at least once on the system, /etc/sysconfig/network-scripts/ifcfg-Hotspot is created and I can use the following command to start a working hotspot:
# ifconfig wlan0 up
# nmcli con up id Hotspot
# iwconfig wlan0 essid my-lappy key 0123456789
Method 2 Problem
If the user has never pressed "Use as Hotspot" on NetworkManager before, nmcli con up id Hotspot won't work. I've looked at nm-connection-editor --create but couldn't find much information. I've also looked into manually creating /etc/sysconfig/network-scripts/ifcfg-Hotspot but this method does not seem elegant or extendible to other distributions.